Skip to content

[BUG] [csharp-netcore] Generating invalid null default values for enum parameters in API #4343

@kimsey0

Description

@kimsey0

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 using the csharp-netcore generator, OpenAPI Generator will generate code that uses null as the default value for enum parameters. null is not a valid value for enums in C#.

It should instead generate default operator applied to the enum type, e.g., default(MyEnum). This is the behavior when using the csharp generator. (The default literal could be used, but is only available from C# 7.1.)

openapi-generator version

Version 4.2.0. (Also recreated in 4.1.3, 4.1.0, 4.0.3, and 4.0.0.)

OpenAPI declaration file content or url

https://gist.github.com/kimsey0/fe44536ff332ded4cf1346dfc0449888

Command line used for generation

npx -p @openapitools/openapi-generator-cli@cli-4.2.0 openapi-generator generate -i ./nullenumbug.json -g csharp-netcore -o ./nullenumbug

Steps to reproduce
  1. Create a OpenAPI declaration file containing an endpoint with a parameter of type string with enum values.
  2. Run OpenAPI Generator with this file as input and using the csharp-netcore generator.
  3. Try to compile the output.
  4. Observe the compile errors of the type:

Error CS1750: A value of type '<null>' cannot be used as a default parameter because there are no standard conversions to type 'MyEnum' (39, 31)

Related issues/PRs
Suggest a fix

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions