Skip to content

Commit e09409f

Browse files
authored
Handle Deserialization Asynchronously (#9424)
1 parent ab11acd commit e09409f

File tree

2 files changed

+18
-18
lines changed
  • modules/openapi-generator/src/main/resources/csharp-netcore/libraries/httpclient
  • samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client

2 files changed

+18
-18
lines changed

modules/openapi-generator/src/main/resources/csharp-netcore/libraries/httpclient/ApiClient.mustache

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ namespace {{packageName}}.Client
8080
}
8181
}
8282

83-
public T Deserialize<T>(HttpResponseMessage response)
83+
public async Task<T> Deserialize<T>(HttpResponseMessage response)
8484
{
85-
var result = (T)Deserialize(response, typeof(T));
85+
var result = (T) await Deserialize(response, typeof(T));
8686
return result;
8787
}
8888

@@ -92,19 +92,19 @@ namespace {{packageName}}.Client
9292
/// <param name="response">The HTTP response.</param>
9393
/// <param name="type">Object type.</param>
9494
/// <returns>Object representation of the JSON string.</returns>
95-
internal object Deserialize(HttpResponseMessage response, Type type)
95+
internal async Task<object> Deserialize(HttpResponseMessage response, Type type)
9696
{
9797
IList<string> headers = response.Headers.Select(x => x.Key + "=" + x.Value).ToList();
9898
9999
if (type == typeof(byte[])) // return byte array
100100
{
101-
return response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();
101+
return await response.Content.ReadAsByteArrayAsync();
102102
}
103103

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

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

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

139139
// at this point, it must be a model (json)
140140
try
141141
{
142-
return JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), type, _serializerSettings);
142+
return JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync(), type, _serializerSettings);
143143
}
144144
catch (Exception e)
145145
{
@@ -510,7 +510,7 @@ namespace {{packageName}}.Client
510510
return await ToApiResponse<T>(response, default(T), req.RequestUri);
511511
}
512512

513-
object responseData = deserializer.Deserialize<T>(response);
513+
object responseData = await deserializer.Deserialize<T>(response);
514514

515515
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
516516
if (typeof({{{packageName}}}.{{modelPackage}}.AbstractOpenAPISchema).IsAssignableFrom(typeof(T)))

samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/ApiClient.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ public string Serialize(object obj)
8080
}
8181
}
8282

83-
public T Deserialize<T>(HttpResponseMessage response)
83+
public async Task<T> Deserialize<T>(HttpResponseMessage response)
8484
{
85-
var result = (T)Deserialize(response, typeof(T));
85+
var result = (T) await Deserialize(response, typeof(T));
8686
return result;
8787
}
8888

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

9999
if (type == typeof(byte[])) // return byte array
100100
{
101-
return response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();
101+
return await response.Content.ReadAsByteArrayAsync();
102102
}
103103

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

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

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

139139
// at this point, it must be a model (json)
140140
try
141141
{
142-
return JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), type, _serializerSettings);
142+
return JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync(), type, _serializerSettings);
143143
}
144144
catch (Exception e)
145145
{
@@ -504,7 +504,7 @@ private async Task<ApiResponse<T>> ExecAsync<T>(HttpRequestMessage req,
504504
return await ToApiResponse<T>(response, default(T), req.RequestUri);
505505
}
506506

507-
object responseData = deserializer.Deserialize<T>(response);
507+
object responseData = await deserializer.Deserialize<T>(response);
508508

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

0 commit comments

Comments
 (0)