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] nullable enum property #1997

Closed
5 tasks done
rienafairefr opened this issue Jan 27, 2019 · 4 comments · Fixed by #2034
Closed
5 tasks done

[BUG] nullable enum property #1997

rienafairefr opened this issue Jan 27, 2019 · 4 comments · Fixed by #2034

Comments

@rienafairefr
Copy link
Contributor

rienafairefr commented Jan 27, 2019

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? 3.3.0
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
Description

If encountered this using the python but I feel like this is the case in a lot of cases
I encountered an API that was publishing a 2.0 spec, but I'm getting resultst from the API that are out-of-spec and not possible to be parsed by a generate openapi-generator client, mostly sending null on non-nullable properties. But the issue is for enum values, I'm not sure how a client should validate:

a small spec that is apparently valid according to 3.0.2

openapi: 3.0.1
paths:
  /test:
    summary: test
info:
  description: test
  version: 1.0.0
  title: test
components:
  schemas:
    Type:
      properties:
        prop:
          nullable: true
          enum: [A, B, C]
          type: string

if the enum is nullable, then getting a null as a result data from an API call should be valid and we should get a property typed like Enum? in C#, or a None` value in Python, and that should be valid. But the code in the case of an enum lists the allowable values excluding null, even if the enum property is specified nullable

Suggest a fix
  • explicitely adding None in the allowed_values list in Python seems to at least make the client accept what the API sends, not sure if that's the full extent that a fix would take
@wing328
Copy link
Member

wing328 commented Jan 30, 2019

explicitely adding None in the allowed_values list in Python seems to at least make the client accept what the API sends, not sure if that's the full extent that a fix would take

@rienafairefr thanks for reporting the issue and suggesting the fix. What about doing that in the template with {{#isNullable}} ... {{/isNulalble}} instead of updating the allowed_values?

@rienafairefr
Copy link
Contributor Author

rienafairefr commented Jan 31, 2019

Hmm, I might have actually misunderstood the problem with the API spec. For complete reference, the spec, and we see some enum properties that have a list of allowed values containing null, like:

"flag_color": {
    "type": "string",
    "enum": ["red", "orange", "yellow", "green", "blue", "purple", null],
    "description": "The transaction flag"
},

This is parsed as ["red", "orange", "yellow", "green", "blue", "purple", "null"] by OpenAPI, and this generates an incorrect client that doesn't allow for an actual null value.
Example with python:

allowed_values = ["red", "orange", "yellow", "green", "blue", "purple", "null"]

which won't allow a value of None (which can be received in the json)

Apparently an actual null is an allowed value in a jsonschema (doc), so the parsing of null as "null" is incorrect.

This is deeper than I thought and is a bug in swagger-parser itself in my opinion.

@Deitsch
Copy link

Deitsch commented Jul 15, 2021

I'm having the same issue described by @rienafairefr.

"reason": {
    "type": "string",
        "enum": [
            "one",
            "two",
            null
        ],
    "nullable": true
}, 

is getting turned into

export type ReasonEnum = 'one' | 'two' | 'null';
export const ReasonEnum = {
        One: 'one' as ReasonEnum,
        Two: 'two' as ReasonEnum,
        Null: 'null' as ReasonEnum
};

Imo null should be converted to null not 'null'. When i select this value and send it to my backend i actually send the string null instead of just null.

@aaronabf
Copy link

I am also having issues even when following the examples as outlined in #7754

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

Successfully merging a pull request may close this issue.

4 participants