OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.91k stars 6.58k forks source link

[BUG][CSHARP] Mixing object/array/primitive types in anyOf/oneOf for csharp generators generates client code that fails to deserialize. #16686

Open ForgeRubenAguilar opened 1 year ago

ForgeRubenAguilar commented 1 year ago

Bug Report Checklist

Description

Mixing object/array/primitive types in anyOf/oneOf for csharp generators generates client code that fails to deserialize.

Expect object to deserialize to C# typed object properly. Currently throws:

Unhandled exception. Pets.Client.ApiException: Error reading JObject from JsonReader. Current JsonReader item is not an object: Boolean. Path 'content', line 2, position 17.
   at Pets.Client.CustomJsonCodec.Deserialize(HttpResponseMessage response, Type type) in /Users/rubenaguilar/repo/openapi/openapi-generator/project/generated/csharp/src/Pets/Client/ApiClient.cs:line 151
   at Pets.Client.CustomJsonCodec.Deserialize[T](HttpResponseMessage response) in /Users/rubenaguilar/repo/openapi/openapi-generator/project/generated/csharp/src/Pets/Client/ApiClient.cs:line 86
   at Pets.Client.ApiClient.ExecAsync[T](HttpRequestMessage req, IReadableConfiguration configuration, CancellationToken cancellationToken)
   at Pets.Api.PetTagApi.GetPetAnyOfByIdWithHttpInfoAsync(Int64 petId, CancellationToken cancellationToken) in /Users/rubenaguilar/repo/openapi/openapi-generator/project/generated/csharp/src/Pets/Api/PetTagApi.cs:line 436
   at Pets.Api.PetTagApi.GetPetAnyOfByIdAsync(Int64 petId, CancellationToken cancellationToken) in /Users/rubenaguilar/repo/openapi/openapi-generator/project/generated/csharp/src/Pets/Api/PetTagApi.cs:line 400
openapi-generator version

Using 7.0.1 of the jar.

OpenAPI declaration file content or url

I tacked on a small repro dotnet console app on my fork with run scripts for the broken and fixed template scenarios.

Generating a client with this OpenApi Spec then triggering a deserialization of this Json data will cause deserialization to fail.

Generation Details

This script runs against this config.

Steps to reproduce

Running run-broken.sh to run the dotnet console app with yarn and dotnet 6.0+ installed will reproduce the exception.

Related issues/PRs

I didn't see any for csharp specifically.

Suggest a fix

I have the current fixed templates we're using as a workaround on a branch on my fork. Diffs here.

I'm not entirely sure how this would be tested within this project. I also believe this workaround would generate uncompilable code for situations where one of the anyOf/oneOf types would generate a generic C# Object type argument for the constructor.

wing328 commented 1 year ago

can you please file a PR to start wth?