OpenAPITools / OpenAPITools/openapi-generator

[BUG][CSharp] Generator does not respect discriminator json casing

Open
#3,308 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: C-Sharp 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)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
Description

When generating a C# api that includes a discriminator that is lowerCamelCase, the generator does not properly configure the JsonSubTypes converter in the resultant code. This causes deserialization to always return the base class and the sub class fields are lost.

openapi-generator version

v4.0.2 - this appears to be a regression as it works properly in v3.2.3. I am not sure about versions in between.

OpenAPI declaration file content or url

Example Swagger Spec

Command line used for generation

generate -g csharp -i /swagger.yaml -o /output --additional-properties 'packageGuid={67035b31-f8e5-41a4-9673-954035084f7d},netCoreProjectFile=true,targetFr amework=v5.0,packageName=DiscriminatorExample'

Steps to reproduce
  1. Take the example swagger spec and run the generator from the command line
  2. Observe the resulting C#. You will see the in the CatalogEntry.cs file that [JsonConverter(typeof(JsonSubtypes), "EntryType")] has the wrong casing for EntryType. It should match the DataMember attribute (and the spec) by using lowerCamelCase.
  3. If you were to use the library and it consumed a response such as:
[
    {
        "productId": 1,
        "productCatalogId": 0,
        "id": 1,
        "entryType": "Sku"
    },
    {
        "name": "Package Name",
        "productCatalogId": 0,
        "id": 1,
        "entryType": "Package"
    }
]

It would process into 2 CatalogEntry instances instead of a Sku and Package instance, and the productId and name fields would be lost respectively.

Suggest a fix

Fix the discriminatorName to use the proper casing based on the spec.

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 by running the provided generate command with the linked Swagger spec, then inspect the generated CatalogEntry.cs file and its JsonSubTypes and DataMember attributes. Done means the discriminator attribute uses the lowerCamelCase name from the spec and the sample response deserializes into the Sku and Package subclasses with their fields preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.