OpenAPITools / OpenAPITools/openapi-generator

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

Open
#16,686 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with project/run-broken.sh, the OpenApi/openapi_spec.yml declaration, and the Pets-string.json response data to reproduce the failure. Compare the current C# generator templates with the fixed-template diff on the linked fork, then verify that the mixed anyOf/oneOf case deserializes successfully without generating uncompilable client code.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.