swagger-api / swagger-api/swagger-codegen-generators
[CSHARP] Optional reusable enums are missing null-conditional operator
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 299
- Forks
- 439
- PR merge metrics
- No merged PRs in 30d
Description
Description
Optional reusable enums are missing null-conditional operator whilst an optional enum defined on the model correctly has the null-conditional operator.
Swagger-codegen version
3.0.35
Swagger declaration file content or url
Very simple model where the Title enum is declared directly on the Person model whilst Gender is declared as a reusable enum. Only Name is marked as required so both Title and Gender should be generated as nullable properties on the C# Person class.
openapi: 3.0.0
info:
version: '1.0'
title: Test
components:
schemas:
person:
type: object
required:
- name
properties:
name:
description: Persons name.
type: string
gender:
$ref: '#/components/schemas/gender'
title:
type: string
enum:
- Mr
- Mrs
- Miss
gender:
type: string
enum:
- Male
- Female
Command line used for generation
java -jar swagger-codegen-cli-3.0.35.jar generate -l csharp -i "<path-to-spec>"
Steps to reproduce
- Generate the C# files using the command line above
- Open the file \src\IO.Swagger\Model\Person.cs
Actual result
Note that the Title enum is generated as nullable property but the Gender enum is not. Additionally the constructor parameters for title is nullable but gender is not.
namespace IO.Swagger.Model
{
/// <summary>
/// Initializes a new instance of the <see cref="Person" /> class.
/// </summary>
/// <param name="name">Persons name. (required).</param>
/// <param name="gender">gender.</param>
/// <param name="title">title.</param>
public Person(string name = default(string), Gender gender = default(Gender), TitleEnum? title = default(TitleEnum?))
{
// to ensure "name" is required (not null)
if (name == null)
{
throw new InvalidDataException("name is a required property for Person and cannot be null");
}
else
{
this.Name = name;
}
this.Gender = gender;
this.Title = title;
}
/// <summary>
/// Persons name.
/// </summary>
/// <value>Persons name.</value>
[DataMember(Name="name", EmitDefaultValue=false)]
public string Name { get; set; }
/// <summary>
/// Gets or Sets Gender
/// </summary>
[DataMember(Name="gender", EmitDefaultValue=false)]
public Gender Gender { get; set; }
/// Gets or Sets Title
/// </summary>
[DataMember(Name="title", EmitDefaultValue=false)]
public TitleEnum? Title { get; set; }
}
Expected result
See added null-conditional operator ? for the Gender property. Additionally the constructor parameters for gender is now also nullable.
namespace IO.Swagger.Model
{
/// <summary>
/// Initializes a new instance of the <see cref="Person" /> class.
/// </summary>
/// <param name="name">Persons name. (required).</param>
/// <param name="gender">gender.</param>
/// <param name="title">title.</param>
public Person(string name = default(string), Gender? gender = default(Gender?), TitleEnum? title = default(TitleEnum?))
{
// to ensure "name" is required (not null)
if (name == null)
{
throw new InvalidDataException("name is a required property for Person and cannot be null");
}
else
{
this.Name = name;
}
this.Gender = gender;
this.Title = title;
}
/// <summary>
/// Persons name.
/// </summary>
/// <value>Persons name.</value>
[DataMember(Name="name", EmitDefaultValue=false)]
public string Name { get; set; }
/// <summary>
/// Gets or Sets Gender
/// </summary>
[DataMember(Name="gender", EmitDefaultValue=false)]
public Gender? Gender { get; set; }
/// Gets or Sets Title
/// </summary>
[DataMember(Name="title", EmitDefaultValue=false)]
public TitleEnum? Title { get; set; }
}
Related issues/PRs
https://github.com/swagger-api/swagger-codegen/pull/6851
Suggest a fix/enhancement
Treat reusable nullable enums the same way as enums that are not reusable.
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
Reproduce the issue with the documented swagger-codegen CLI command and inspect src/IO.Swagger/Model/Person.cs. Compare the generated reusable Gender enum with the inline Title enum; done when Gender and its constructor parameter are nullable in the generated C# output, as shown in the expected result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100