OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.58k stars 6.52k forks source link

[BUG] nullable schema is not allowed to contain `null` #5615

Open domenkozar opened 4 years ago

domenkozar commented 4 years ago

Bug Report Checklist

Description

Generator crashes if nullable enum schema contains null value.

This is allowed by jsonschema and thus openapi spec.

openapi-generator version

5.0.0 branch, 4th Feb 2020

OpenAPI declaration file content or url
rating:
      type: string
      nullable: true
      enum:
        - null 
        - Gibberish
Command line used for generation
openapi-generator-cli  generate --input-spec openapi.yaml --enable-post-process-file --generator-name elm --config openapi-generator.yml  --output openapi-client
Steps to reproduce

There's an error:

Exception in thread "main" java.lang.RuntimeException: Could not process model 'rating'.Please make sure that your schema is correct!
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:473)
        at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:938)
        at org.openapitools.codegen.cmd.Generate.run(Generate.java:423)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:61)
Caused by: java.lang.NullPointerException
        at org.openapitools.codegen.DefaultCodegen.postProcessModelsEnum(DefaultCodegen.java:490)
        at org.openapitools.codegen.languages.ElmClientCodegen.postProcessModels(ElmClientCodegen.java:321)
        at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1214)
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:468)
        ... 3 more
domenkozar commented 4 years ago

cc @eriktim

eriktim commented 4 years ago

Thanks for opening this issue @domenkozar. The error seems to be misleading. I think the issue is you're using null as an enum value here which seems to be unsupported. I'm not sure what you're trying to achieve here, but I think there are two options for now:

  1. You want null to be one of the values of the enum. In that case you should write "null" in the yaml instead, or

  2. You want the enum to be nullable. In that case I would recommend providing the non-nullable enum as a type and whereever you use it you can use nullable/[not-]required. For example:

    optionA:
    required:
    - rating
    properties:
    rating:
      $ref: '#/components/schemas/rating'
      nullable: true
    optionB:
    properties:
    rating:
      $ref: '#/components/schemas/rating'
    rating:
      type: string
      enum:
        - Gibberish

@wing328 can you clarify what the desired behavior is here?

domenkozar commented 4 years ago

OpenAPI specification says schemas respect jsonschema, which does allow for my example.

See https://github.com/OAI/OpenAPI-Specification/issues/1900#issuecomment-552945475

eriktim commented 4 years ago

Yes I've seen that one but it seems the OpenAPI generator does not support this (yet?). It's not the Elm generator breaking here.

domenkozar commented 4 years ago

Thanks - I've updated issue title to reflect that.

zupo commented 4 years ago

Hey @eriktim, where should I report this so that it gets fixed in the "OpenAPI generator", so that I can then help getting it fixed for Elm?

eriktim commented 4 years ago

@zupo this should be the right location. I was actually hoping for @wing328 to give some clarity on this issue.