Skip to content

[csharp][netcore] Handle Deserialization Asynchronously #9424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ namespace {{packageName}}.Client
}
}

public T Deserialize<T>(HttpResponseMessage response)
public async Task<T> Deserialize<T>(HttpResponseMessage response)
{
var result = (T)Deserialize(response, typeof(T));
var result = (T) await Deserialize(response, typeof(T));
return result;
}

Expand All @@ -92,19 +92,19 @@ namespace {{packageName}}.Client
/// <param name="response">The HTTP response.</param>
/// <param name="type">Object type.</param>
/// <returns>Object representation of the JSON string.</returns>
internal object Deserialize(HttpResponseMessage response, Type type)
internal async Task<object> Deserialize(HttpResponseMessage response, Type type)
{
IList<string> headers = response.Headers.Select(x => x.Key + "=" + x.Value).ToList();

if (type == typeof(byte[])) // return byte array
{
return response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();
return await response.Content.ReadAsByteArrayAsync();
}

// TODO: ? if (type.IsAssignableFrom(typeof(Stream)))
if (type == typeof(Stream))
{
var bytes = response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();
var bytes = await response.Content.ReadAsByteArrayAsync();
if (headers != null)
{
var filePath = String.IsNullOrEmpty(_configuration.TempFolderPath)
Expand All @@ -128,18 +128,18 @@ namespace {{packageName}}.Client

if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) // return a datetime object
{
return DateTime.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), null, System.Globalization.DateTimeStyles.RoundtripKind);
return DateTime.Parse(await response.Content.ReadAsStringAsync(), null, System.Globalization.DateTimeStyles.RoundtripKind);
}

if (type == typeof(String) || type.Name.StartsWith("System.Nullable")) // return primitive type
{
return Convert.ChangeType(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), type);
return Convert.ChangeType(await response.Content.ReadAsStringAsync(), type);
}

// at this point, it must be a model (json)
try
{
return JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), type, _serializerSettings);
return JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync(), type, _serializerSettings);
}
catch (Exception e)
{
Expand Down Expand Up @@ -516,7 +516,7 @@ namespace {{packageName}}.Client
return await ToApiResponse<T>(response, default(T), req.RequestUri);
}

object responseData = deserializer.Deserialize<T>(response);
object responseData = await deserializer.Deserialize<T>(response);

// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
if (typeof({{{packageName}}}.{{modelPackage}}.AbstractOpenAPISchema).IsAssignableFrom(typeof(T)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public string Serialize(object obj)
}
}

public T Deserialize<T>(HttpResponseMessage response)
public async Task<T> Deserialize<T>(HttpResponseMessage response)
{
var result = (T)Deserialize(response, typeof(T));
var result = (T) await Deserialize(response, typeof(T));
return result;
}

Expand All @@ -92,19 +92,19 @@ public T Deserialize<T>(HttpResponseMessage response)
/// <param name="response">The HTTP response.</param>
/// <param name="type">Object type.</param>
/// <returns>Object representation of the JSON string.</returns>
internal object Deserialize(HttpResponseMessage response, Type type)
internal async Task<object> Deserialize(HttpResponseMessage response, Type type)
{
IList<string> headers = response.Headers.Select(x => x.Key + "=" + x.Value).ToList();

if (type == typeof(byte[])) // return byte array
{
return response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();
return await response.Content.ReadAsByteArrayAsync();
}

// TODO: ? if (type.IsAssignableFrom(typeof(Stream)))
if (type == typeof(Stream))
{
var bytes = response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();
var bytes = await response.Content.ReadAsByteArrayAsync();
if (headers != null)
{
var filePath = String.IsNullOrEmpty(_configuration.TempFolderPath)
Expand All @@ -128,18 +128,18 @@ internal object Deserialize(HttpResponseMessage response, Type type)

if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) // return a datetime object
{
return DateTime.Parse(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), null, System.Globalization.DateTimeStyles.RoundtripKind);
return DateTime.Parse(await response.Content.ReadAsStringAsync(), null, System.Globalization.DateTimeStyles.RoundtripKind);
}

if (type == typeof(String) || type.Name.StartsWith("System.Nullable")) // return primitive type
{
return Convert.ChangeType(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), type);
return Convert.ChangeType(await response.Content.ReadAsStringAsync(), type);
}

// at this point, it must be a model (json)
try
{
return JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), type, _serializerSettings);
return JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync(), type, _serializerSettings);
}
catch (Exception e)
{
Expand Down Expand Up @@ -510,7 +510,7 @@ private async Task<ApiResponse<T>> ExecAsync<T>(HttpRequestMessage req,
return await ToApiResponse<T>(response, default(T), req.RequestUri);
}

object responseData = deserializer.Deserialize<T>(response);
object responseData = await deserializer.Deserialize<T>(response);

// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
if (typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T)))
Expand Down