Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][C#] Wrong type in default constructor for nullable enumerations #5470

Open
5 of 6 tasks
simonhaines opened this issue Feb 28, 2020 · 0 comments
Open
5 of 6 tasks

Comments

@simonhaines
Copy link

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
  1. Download the spec file from the gist
  2. 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 #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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants