OpenAPITools / OpenAPITools/openapi-generator
[BUG][C#] Wrong type in default constructor for nullable enumerations
Nobody has claimed this yet.
- 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?
- [Optional] Bounty to sponsor the fix (example)
Description
When an entity has a nullable enumeration member, the C# code generator creates a default constructor that requires a non-nullable value for the member. This produces an error during deserialization when the serialized entity has a null value: Cannot convert null value to <enumeration type> because the constructor requires a non-null value.
openapi-generator version
openapi-generator-cli-4.2.3
OpenAPI declaration file content or url
https://gist.github.com/simonhaines/3cf34ff70d5bf75e7d3d7009bc6979b6
Command line used for generation
java -jar openapi-generator-cli-4.2.3.jar generate -i spec.json -g csharp-netcore
Steps to reproduce
- Download the spec file from the gist
- Use the above command line to generate models
Actual output (extract from CoffeeEntity.cs)
/// <summary>
/// Gets or Sets Bean
/// </summary>
[DataMember(Name="bean", EmitDefaultValue=true)]
public CoffeeBean? Bean { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="CoffeeEntity" /> class.
/// </summary>
/// <param name="bean">bean.</param>
public CoffeeEntity(CoffeeBean bean = default(CoffeeBean))
{
this.Bean = bean;
}
Expected output (note nullable type in constructor)
/// <summary>
/// Gets or Sets Bean
/// </summary>
[DataMember(Name="bean", EmitDefaultValue=true)]
public CoffeeBean? Bean { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="CoffeeEntity" /> class.
/// </summary>
/// <param name="bean">bean.</param>
public CoffeeEntity(CoffeeBean? bean = default(CoffeeBean?))
{
this.Bean = bean;
}
Related issues/PRs
Maybe https://github.com/OpenAPITools/openapi-generator/issues/4816
Suggest a fix
It looks like the expected output can be achieved by setting the required property of the member metadata to false so that this line of the template appends the ? to the type name.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with modules/openapi-generator/src/main/resources/csharp/modelGeneric.mustache, especially the constructor template line linked in the issue. Generate the C# models from the provided gist with openapi-generator-cli-4.2.3 and inspect CoffeeEntity.cs. Done means a nullable enumeration member produces a nullable constructor parameter and deserialization accepts a null value.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100