-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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#] Not possible to generate nullable enum in an API model #9190
Comments
👍 Thanks for opening this issue! The team will review the labels and make any necessary changes. |
It's not possible to have a nullable in enum is C#. Rather than create a new issue, I am recording my observations here since it's closely related. Bug Report Checklist
DescriptionThe null-coalescing operator "??" can't be used on an enum. Hence, the following line in the generated code will have a compilation error:
Actual error messages are:
openapi-generator versionVersion used is 6.2.1. Did not test against the latest commit on the master branch. OpenAPI declaration file content or urlopenapi: 3.0.0
info:
version: '1.0.0'
title: 'Enum with null'
paths: {}
components:
schemas:
Entity:
type: object
properties:
status:
$ref: '#/components/schemas/Status'
Status:
anyOf:
- type: string
enum:
- ACTIVE
- PASSIVE
- $ref: '#/components/schemas/NullValue'
- type: string
NullValue:
enum:
- null Generation Details
Steps to reproduceJust run the command above and compile the generated model file. We will see the error. Related issues/PRsNone Suggest a fixNone |
If I understand you correctly, you're saying that it is not possible to have a nullable enum in C#? Well - it is... |
@DavidJeppesen-Seges I am no expert in C#. I read that "An enum value cannot be null. It is a value type like an int." at https://www.dotnetperls.com/enum |
I've filed #14898 to workaround the issue using the openapi-normalizer.
|
Workaround no longer works now. Command line:
Getting tons of errors like The only "workaround" is manually going through every case this error occurs and making the parameter nullable. i.e.: |
Bug Report Checklist
Description
I am implementing a REST API for an international standard on dairy data. The standard is expressed in JSON Schema and we use the Open API Generator to generate the model.
Unfortunately - no matter what I do, I can't get the generator to generate a nullable enum on the model. I have tried adding the "nullable": true property, adding null to the list of enum values, changed the type on the property to an array with null included as a type and finally I tried changing the reference to the type on the parent to be "oneOf" and adding null. Nothing works.
According to other definitions and bugs I've seen, it should be enough that the enum is not required in the specs to make it nullable.
Under the assumption that it should be possible to generate a nullable enum, I would like to know how it is done and if it is not possible, then I think it is a bug.
openapi-generator version
5.1.0
OpenAPI declaration file content or url
Root JSON Schema file
Generation Details
PowerShell command:
npx openapi-generator-cli generate -i exampleUrlScheme.json -g aspnetcore -o c:\temp\generated
Steps to reproduce
Clone the above GIT repo, navigate to the folder where the exampleUrlScheme.json file resides and run the above command.
Try also the changes I mentioned in the description on any enum type.
I expect a nullable enum on enum types that are not required or at least when the type in any of the above mentioned ways is defined as nullable.
Related issues/PRs
I found a couple of open issues that might be related to this bug:
#4816
#9022
Suggest a fix
Sorry - my Java is very rusty!
The text was updated successfully, but these errors were encountered: