Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #5470
Closed
Open
Issue created Feb 28, 2020 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[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
  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 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.

Assignee
Assign to
Time tracking