[BUG][C#] Wrong type in default constructor for nullable enumerations
Created by: simonhaines
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.