Skip to content

Commit b7ee885

Browse files
authored
[C#][netcore] fix binary response (#8593)
* fix binary response * update test file hash
1 parent a5ac2ee commit b7ee885

7 files changed

Lines changed: 99 additions & 1 deletion

File tree

bin/utils/test_file_list.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
# csharp-netcore test files and image for upload
33
- filename: "samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/Api/PetApiTests.cs"
4-
sha256: b74ef9eefa4b41fd3233e083fe2355babf25a77f9073d28e1aa81ae2e0a5f1d0
4+
sha256: aceebba316148a2a803a15ef4e13bbd0b0a1b8d15006cd88adb9b39a620ee451
55
- filename: "samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/linux-logo.png"
66
sha256: 0a67c32728197e942b13bdda064b73793f12f5c795f1e5cf35a3adf69c973230

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,19 @@ namespace {{packageName}}.Client
472472
response = client.Execute<T>(req);
473473
}
474474
475+
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
476+
if (typeof({{{packageName}}}.{{modelPackage}}.AbstractOpenAPISchema).IsAssignableFrom(typeof(T)))
477+
{
478+
T instance = (T)Activator.CreateInstance(typeof(T));
479+
MethodInfo method = typeof(T).GetMethod("FromJson");
480+
method.Invoke(instance, new object[] { response.Content });
481+
response.Data = instance;
482+
}
483+
else if (typeof(T).Name == "Stream") // for binary response
484+
{
485+
response.Data = (T)(object)new MemoryStream(response.RawBytes);
486+
}
487+
475488
InterceptResponse(req, response);
476489
477490
var result = ToApiResponse(response);
@@ -583,6 +596,10 @@ namespace {{packageName}}.Client
583596
method.Invoke(instance, new object[] { response.Content });
584597
response.Data = instance;
585598
}
599+
else if (typeof(T).Name == "Stream") // for binary response
600+
{
601+
response.Data = (T)(object)new MemoryStream(response.RawBytes);
602+
}
586603
587604
InterceptResponse(req, response);
588605

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,19 @@ private ApiResponse<T> Exec<T>(RestRequest req, IReadableConfiguration configura
477477
response = client.Execute<T>(req);
478478
}
479479

480+
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
481+
if (typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T)))
482+
{
483+
T instance = (T)Activator.CreateInstance(typeof(T));
484+
MethodInfo method = typeof(T).GetMethod("FromJson");
485+
method.Invoke(instance, new object[] { response.Content });
486+
response.Data = instance;
487+
}
488+
else if (typeof(T).Name == "Stream") // for binary response
489+
{
490+
response.Data = (T)(object)new MemoryStream(response.RawBytes);
491+
}
492+
480493
InterceptResponse(req, response);
481494

482495
var result = ToApiResponse(response);
@@ -587,6 +600,10 @@ private ApiResponse<T> Exec<T>(RestRequest req, IReadableConfiguration configura
587600
method.Invoke(instance, new object[] { response.Content });
588601
response.Data = instance;
589602
}
603+
else if (typeof(T).Name == "Stream") // for binary response
604+
{
605+
response.Data = (T)(object)new MemoryStream(response.RawBytes);
606+
}
590607

591608
InterceptResponse(req, response);
592609

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,19 @@ private ApiResponse<T> Exec<T>(RestRequest req, IReadableConfiguration configura
477477
response = client.Execute<T>(req);
478478
}
479479

480+
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
481+
if (typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T)))
482+
{
483+
T instance = (T)Activator.CreateInstance(typeof(T));
484+
MethodInfo method = typeof(T).GetMethod("FromJson");
485+
method.Invoke(instance, new object[] { response.Content });
486+
response.Data = instance;
487+
}
488+
else if (typeof(T).Name == "Stream") // for binary response
489+
{
490+
response.Data = (T)(object)new MemoryStream(response.RawBytes);
491+
}
492+
480493
InterceptResponse(req, response);
481494

482495
var result = ToApiResponse(response);
@@ -587,6 +600,10 @@ private ApiResponse<T> Exec<T>(RestRequest req, IReadableConfiguration configura
587600
method.Invoke(instance, new object[] { response.Content });
588601
response.Data = instance;
589602
}
603+
else if (typeof(T).Name == "Stream") // for binary response
604+
{
605+
response.Data = (T)(object)new MemoryStream(response.RawBytes);
606+
}
590607

591608
InterceptResponse(req, response);
592609

samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/Api/PetApiTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,19 @@ public void TestGetPetByIdAsync()
199199
Assert.Equal("sample category name2", response.Category.Name);
200200
}
201201

202+
/* a simple test for binary response. no longer in use.
203+
[Fact]
204+
public void TestGetByIdBinaryResponse()
205+
{
206+
PetApi petApi = new PetApi(c1);
207+
Stream response = petApi.GetPetByIdBinaryResponse(petId);
208+
Assert.IsType<System.IO.MemoryStream>(response);
209+
StreamReader reader = new StreamReader(response);
210+
// the following will fail for sure
211+
//Assert.Equal("someting", reader.ReadToEnd());
212+
}
213+
*/
214+
202215
/// <summary>
203216
/// Test GetPetByIdWithHttpInfoAsync
204217
/// </summary>

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,19 @@ private ApiResponse<T> Exec<T>(RestRequest req, IReadableConfiguration configura
476476
response = client.Execute<T>(req);
477477
}
478478

479+
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
480+
if (typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T)))
481+
{
482+
T instance = (T)Activator.CreateInstance(typeof(T));
483+
MethodInfo method = typeof(T).GetMethod("FromJson");
484+
method.Invoke(instance, new object[] { response.Content });
485+
response.Data = instance;
486+
}
487+
else if (typeof(T).Name == "Stream") // for binary response
488+
{
489+
response.Data = (T)(object)new MemoryStream(response.RawBytes);
490+
}
491+
479492
InterceptResponse(req, response);
480493

481494
var result = ToApiResponse(response);
@@ -586,6 +599,10 @@ private ApiResponse<T> Exec<T>(RestRequest req, IReadableConfiguration configura
586599
method.Invoke(instance, new object[] { response.Content });
587600
response.Data = instance;
588601
}
602+
else if (typeof(T).Name == "Stream") // for binary response
603+
{
604+
response.Data = (T)(object)new MemoryStream(response.RawBytes);
605+
}
589606

590607
InterceptResponse(req, response);
591608

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,19 @@ private ApiResponse<T> Exec<T>(RestRequest req, IReadableConfiguration configura
477477
response = client.Execute<T>(req);
478478
}
479479

480+
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
481+
if (typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T)))
482+
{
483+
T instance = (T)Activator.CreateInstance(typeof(T));
484+
MethodInfo method = typeof(T).GetMethod("FromJson");
485+
method.Invoke(instance, new object[] { response.Content });
486+
response.Data = instance;
487+
}
488+
else if (typeof(T).Name == "Stream") // for binary response
489+
{
490+
response.Data = (T)(object)new MemoryStream(response.RawBytes);
491+
}
492+
480493
InterceptResponse(req, response);
481494

482495
var result = ToApiResponse(response);
@@ -587,6 +600,10 @@ private ApiResponse<T> Exec<T>(RestRequest req, IReadableConfiguration configura
587600
method.Invoke(instance, new object[] { response.Content });
588601
response.Data = instance;
589602
}
603+
else if (typeof(T).Name == "Stream") // for binary response
604+
{
605+
response.Data = (T)(object)new MemoryStream(response.RawBytes);
606+
}
590607

591608
InterceptResponse(req, response);
592609

0 commit comments

Comments
 (0)