swagger-api / swagger-codegen-generators

Apache License 2.0
284 stars 424 forks source link

Generator fails on null enum choice #1296

Open Tavh opened 5 months ago

Tavh commented 5 months ago

When running the generator on a schema that contains a null enum option, we get an exception:

  Exception: Cannot invoke "java.lang.Integer.toString()" because "i" is null
    at io.swagger.codegen.v3.DefaultGenerator.processOperation(DefaultGenerator.java:989)
    at io.swagger.codegen.v3.DefaultGenerator.processPaths(DefaultGenerator.java:889)
    at io.swagger.codegen.v3.DefaultGenerator.generateApis(DefaultGenerator.java:484)
    at io.swagger.codegen.v3.DefaultGenerator.generate(DefaultGenerator.java:796)
    at io.swagger.codegen.v3.cli.cmd.Generate.run(Generate.java:388)
    at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Cannot invoke "java.lang.Integer.toString()" because "i" is null
    at io.swagger.codegen.v3.generators.DefaultCodegenConfig.processPropertySchemaTypes(DefaultCodegenConfig.java:1666)
    at io.swagger.codegen.v3.generators.DefaultCodegenConfig.fromProperty(DefaultCodegenConfig.java:1587)
    at io.swagger.codegen.v3.generators.DefaultCodegenConfig.fromParameter(DefaultCodegenConfig.java:2530)
    at io.swagger.codegen.v3.generators.DefaultCodegenConfig.fromOperation(DefaultCodegenConfig.java:2237)
    at io.swagger.codegen.v3.DefaultGenerator.processOperation(DefaultGenerator.java:965)
    ... 5 more

This occurs on enums like:

...
        schema:
          type: string
          nullable: true
          enum:
          - ''
          - AWS
          - AZURE
          - GCP
          - KUBE
          - null
          - OCI
          - github
          - gitlab
 ...

The suggested resolution is adding a null check in the problematic function:

DefaultCodegenConfig.java:processPropertySchemaTypes:1663

for(Integer i : _enum) {
    String stringifiedEnum = null;
    if (i != null) {
        stringifiedEnum = i.toString();            
    }
    codegenProperty._enum.add(stringifiedEnum);
}