|
| 1 | +/* |
| 2 | + * OpenAPI Petstore |
| 3 | + * |
| 4 | + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ |
| 5 | + * |
| 6 | + * The version of the OpenAPI document: 1.0.0 |
| 7 | + * Generated by: https://github.com/openapitools/openapi-generator.git |
| 8 | + */ |
| 9 | + |
| 10 | +using System.Net; |
| 11 | +using System.Net.Http; |
| 12 | +using System.Text; |
| 13 | +using System.Threading; |
| 14 | +using System.Threading.Tasks; |
| 15 | +using Org.OpenAPITools.Client; |
| 16 | +using Org.OpenAPITools.Model; |
| 17 | +using Xunit; |
| 18 | + |
| 19 | +namespace Org.OpenAPITools.Test.Client |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// Regression tests for deserializing oneOf/anyOf responses (types derived from |
| 23 | + /// AbstractOpenAPISchema) in the httpclient library. |
| 24 | + /// See https://github.com/OpenAPITools/openapi-generator/issues/25079 |
| 25 | + /// </summary> |
| 26 | + public class OneOfResponseDeserializationTests |
| 27 | + { |
| 28 | + /// <summary> |
| 29 | + /// Returns a canned JSON body for every request, so ExecAsync can be exercised |
| 30 | + /// without a live server. |
| 31 | + /// </summary> |
| 32 | + private sealed class StubHttpMessageHandler : HttpMessageHandler |
| 33 | + { |
| 34 | + private readonly string _json; |
| 35 | + |
| 36 | + public StubHttpMessageHandler(string json) |
| 37 | + { |
| 38 | + _json = json; |
| 39 | + } |
| 40 | + |
| 41 | + protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| 42 | + { |
| 43 | + var response = new HttpResponseMessage(HttpStatusCode.OK) |
| 44 | + { |
| 45 | + Content = new StringContent(_json, Encoding.UTF8, "application/json") |
| 46 | + }; |
| 47 | + return Task.FromResult(response); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// A oneOf/anyOf response must be deserialized via the model's static |
| 53 | + /// FromJson(string) method. Previously ApiClient.ExecAsync passed the |
| 54 | + /// HttpContent instead of the body string, which threw an ArgumentException |
| 55 | + /// at runtime for every oneOf/anyOf response. |
| 56 | + /// </summary> |
| 57 | + [Fact] |
| 58 | + public async Task OneOfResponseIsDeserializedToConcreteType() |
| 59 | + { |
| 60 | + using var httpClient = new HttpClient(new StubHttpMessageHandler("{\"className\":\"BasquePig\"}")); |
| 61 | + using var apiClient = new ApiClient(httpClient, "http://localhost"); |
| 62 | + |
| 63 | + ApiResponse<Pig> response = await apiClient.GetAsync<Pig>("/fake", new RequestOptions()); |
| 64 | + |
| 65 | + Assert.NotNull(response.Data); |
| 66 | + Assert.IsType<BasquePig>(response.Data.ActualInstance); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments