fix(EConnectClient): Behandlung leerer HTTP-Antwortinhalte

- Es wurden Überprüfungen für `ContentLength > 0` hinzugefügt, bevor Antwortinhalte in den Methoden GetAsync, GetListAsAsyncEnumerable und PostAsync deserialisiert werden.
- Stellt sicher, dass null zurückgegeben wird, anstatt zu versuchen, leere Antworten zu deserialisieren, wodurch potenzielle Laufzeitfehler vermieden werden.
This commit is contained in:
tekh 2025-08-18 11:16:16 +02:00
parent 060ba64268
commit 265862d63d

View File

@ -41,22 +41,24 @@ public class EConnectClient<TError> : IEConnectClient<TError> where TError : cl
if (res.IsSuccessStatusCode) if (res.IsSuccessStatusCode)
{ {
var data = await res.Content.ReadFromJsonAsync<TData>(_options.JsonSerializerOptions, cancel);
return new () return new ()
{ {
Ok = true, Ok = true,
StatusCode = res.StatusCode, StatusCode = res.StatusCode,
Data = data Data = (res.Content.Headers.ContentLength > 0)
? await res.Content.ReadFromJsonAsync<TData>(_options.JsonSerializerOptions, cancel)
: null
}; };
} }
else else
{ {
var error = await res.Content.ReadFromJsonAsync<TError>(_options.JsonSerializerOptions, cancel);
return new () return new ()
{ {
Ok = false, Ok = false,
StatusCode = res.StatusCode, StatusCode = res.StatusCode,
Error = error Error = (res.Content.Headers.ContentLength > 0)
? await res.Content.ReadFromJsonAsync<TError>(_options.JsonSerializerOptions, cancel)
: null
}; };
} }
} }
@ -70,22 +72,24 @@ public class EConnectClient<TError> : IEConnectClient<TError> where TError : cl
if (res.IsSuccessStatusCode) if (res.IsSuccessStatusCode)
{ {
var data = res.Content.ReadFromJsonAsAsyncEnumerable<TData>(_options.JsonSerializerOptions, cancel);
return new () return new ()
{ {
Ok = true, Ok = true,
StatusCode = res.StatusCode, StatusCode = res.StatusCode,
Data = data Data = (res.Content.Headers.ContentLength > 0)
? res.Content.ReadFromJsonAsAsyncEnumerable<TData>(_options.JsonSerializerOptions, cancel)
: null
}; };
} }
else else
{ {
var error = await res.Content.ReadFromJsonAsync<TError>(_options.JsonSerializerOptions, cancel);
return new() return new()
{ {
Ok = false, Ok = false,
StatusCode = res.StatusCode, StatusCode = res.StatusCode,
Error = error Error = (res.Content.Headers.ContentLength > 0)
? await res.Content.ReadFromJsonAsync<TError>(_options.JsonSerializerOptions, cancel)
: null
}; };
} }
} }
@ -106,12 +110,13 @@ public class EConnectClient<TError> : IEConnectClient<TError> where TError : cl
} }
else else
{ {
var error = await res.Content.ReadFromJsonAsync<TError>(_options.JsonSerializerOptions, cancel);
return new() return new()
{ {
Ok = false, Ok = false,
StatusCode = res.StatusCode, StatusCode = res.StatusCode,
Error = error Error = (res.Content.Headers.ContentLength > 0)
? await res.Content.ReadFromJsonAsync<TError>(_options.JsonSerializerOptions, cancel)
: null
}; };
} }
} }
@ -132,14 +137,13 @@ public class EConnectClient<TError> : IEConnectClient<TError> where TError : cl
} }
else else
{ {
TError? error = null;
if (res.Content.Headers.ContentLength > 0)
error = await res.Content.ReadFromJsonAsync<TError>(_options.JsonSerializerOptions, cancel);
return new() return new()
{ {
Ok = false, Ok = false,
StatusCode = res.StatusCode, StatusCode = res.StatusCode,
Error = error Error = (res.Content.Headers.ContentLength > 0)
? await res.Content.ReadFromJsonAsync<TError>(_options.JsonSerializerOptions, cancel)
: null
}; };
} }
} }