Skip to content

[BUG] [c-sharp] Inconsistency in default values discriminator #6225

@tomelfring

Description

@tomelfring

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?
  • [Optional] Bounty to sponsor the fix (example)
Description

Using the C# generator I found some inconsistencies in the default values for classes with a discriminator with a mapping.

When the discriminator propertyName is in camelCase, I notice the following:

  • JsonConverter uses a camelCase value
  • The property of the discriminator has a DataMemberAttribute with a camelCase name
  • In subclasses, the default value of the discriminator is default(string)

When the discriminator propertyname is in PascalCase, I notice the following:

  • JsonConverter uses a camelCase value
  • The property of the discriminator has a DataMemberAttribute with a PascalCase name
  • In subclasses, the default value of the discriminator is matching the spec

Expected:

  • The JsonConverter converterParameters and Datamember name to match
  • The subclasses having meaningful default values for discriminator parameters when mapping is provided in the spec
openapi-generator version

openapi-generator-cli-4.3.1.jar

OpenAPI declaration file content or url

Two files, one with camelCase, one with PascalCase:
https://gist.github.com/thommy101/39b65b3a365a09d3738f26edc3e1682b

Command line used for generation

java -jar openapi-generator-cli-4.3.1.jar generate -i camelCase.yaml -g csharp -o camelCase
java -jar openapi-generator-cli-4.3.1.jar generate -i pascalCase.yaml -g csharp -o pascalCase

Steps to reproduce
  • Execute above two command lines
  • Compare the camelCase and pascalCase variants of src/Org.OpenAPITools/Models/Animal.cs and src/Org.OpenAPITools/Models/Cat.cs
  • See the CamelCase variant of Animal having consistent JsonConverter-attribute on the class and DataMember-attribute on the property ClassName, but not having a default value for Cat's constructor parameter className (default(string))
  • See the PascalCase variant of Animal having inconsistent JsonConverter-attribute on the class and Datamember-attribute on the property ClassName, but having a correct default value for Cat's constructor parameter className ("Cat")
Related issues/PRs

#3308

#5680: Related because forcing the JsonConverter attribute to camelCase and because of the comment:

Right now if this discriminator isn't camelCased it causes things to not deserialize properly when receiving them from an API (since the json is expected to come back camelcased)

Suggest a fix

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions