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.81k stars 6.58k forks source link

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

Open gbrown-ce opened 5 years ago

gbrown-ce commented 5 years ago

Bug Report Checklist

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.

auto-labeler[bot] commented 5 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.