swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.93k stars 6.03k forks source link

NullPointerException running swagger-codegen-cli for model with nullable integer enum property in schema definition #12371

Open jenskreidler opened 5 months ago

jenskreidler commented 5 months ago
Description

As a user I want to generate JAVA client data types with swagger-codegen-cli using a valid OpenAPI 3 document.

When Swagger CodeGen encounters components schemas (models) with a nullable enum property of type integer, the generation fails unexpectedly with a NullPointerException (invoking java.lang.Integer.toString() on a null object, but that is intentionally because the property is nullable). Removing the potential null value in the enum's schema elements list within the yaml source will sanitize the issue (see yaml snippet below).

Swagger-codegen version

Tested using swagger-code-gen 3.0.54

Swagger declaration file content or url

Issue can be verified using the Yaml OpenAPI 3 schema file of Netbox 3.7 under https://demo.netbox.dev/api/schema/

Command line used for generation

shell: java -DmaxYamlCodePoints=40000000 -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i https://demo.netbox.dev/api/schema/ -l java -o ~/tmp/swagger-netbox

Steps to reproduce
  1. install swagger-codegen-ui 3.0.54
  2. run java generation of Netbox 3.7 OpenAPI3:
    • java -DmaxYamlCodePoints=40000000 -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i https://demo.netbox.dev/api/schema/ -l java -o /tmp/swagger-netbox
Related issues/PRs
Suggest a fix/enhancement

The fix should respect a possible null integer enum value as specified with an appropriate OpenAPI3 schema property part (like it does with nullable string enums):

        speed:
          enum:
          - 1200
          - 2400
          - 4800
          - 9600
          - 19200
          - 38400
          - 57600
          - 115200
          - null // <-- THIS IS THE CLASHING ELEMENT (removing it will sanitize generation run)
          type: integer // <-- switching this to string will also sanitize the issue, null enum valie is OK, generation runs well
          description: |-
            * `1200` - 1200 bps
            * `2400` - 2400 bps
            * `4800` - 4800 bps
            * `9600` - 9600 bps
            * `19200` - 19.2 kbps
            * `38400` - 38.4 kbps
            * `57600` - 57.6 kbps
            * `115200` - 115.2 kbps
          x-spec-enum-id: ab6d9635c131a378
          nullable: true