Skip to content

[BUG][JAVA] NullPointerException in getEnumValueForProperty when generating model with oneOf and discriminator #22177

@Radenixlol

Description

@Radenixlol
Bug Report Checklist

openapi.yaml

[x] Have you provided a full/minimal spec to reproduce the issue?

[x] Have you validated the input using an OpenAPI validator?

[x] Have you tested with the latest master to confirm the issue still exists?

[x] Have you searched for related issues/PRs?

[x] What's the actual output vs expected output? (Actual: Crash. Expected: Generated code)

[ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)

When generating a Java client, the generator fails with a NullPointerException while processing a specific model named 'Node'. This model uses a oneOf list combined with a discriminator (see spec below).

The error seems to originate in DefaultCodegen.getEnumValueForProperty (line ~3221), which is called by setEnumDiscriminatorDefaultValue. The root cause is a NullPointerException because var.allowableValues is null when the code attempts to call .get(Object) on it. This blocks the entire code generation process.

Stack Trace:
[main] WARN  o.o.codegen.DefaultCodegen - OpenAPI 3.1 support is still in beta. To report an issue related to 3.1 spec, please kindly open an issue in the Github repo: https://github.com/openAPITools/openapi-generator.
[main] INFO  o.o.codegen.DefaultGenerator - Model AnnotationsTrait not generated since it's a free-form object
Exception in thread "main" java.lang.RuntimeException: Could not process model 'Node'.Please make sure that your schema is correct!
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:518)
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:443)
        at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:1291)
        at org.openapitools.codegen.cmd.Generate.execute(Generate.java:535)
        at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.NullPointerException: Cannot invoke "java.util.Map.get(Object)" because "var.allowableValues" is null
        at org.openapitools.codegen.DefaultCodegen.getEnumValueForProperty(DefaultCodegen.java:3221)
        at org.openapitools.codegen.DefaultCodegen.lambda$setEnumDiscriminatorDefaultValue$17(DefaultCodegen.java:3193)
        at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
        at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
        at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1708)
        at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:762)
        at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:276)
        at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:1024)
        at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
        at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
        at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
        at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
        at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
        at org.openapitools.codegen.DefaultCodegen.setEnumDiscriminatorDefaultValue(DefaultCodegen.java:3193)
        at org.openapitools.codegen.languages.AbstractJavaCodegen.fromModel(AbstractJavaCodegen.java:1842)
        at org.openapitools.codegen.languages.JavaClientCodegen.fromModel(JavaClientCodegen.java:1070)
        at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1753)
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:513)
        ... 5 more
openapi-generator version

7.16.0 (and also confirmed on the latest master branch).

OpenAPI declaration file content or url

The error occurs when processing the following Node definition (from openapi.yaml, starting at line 3543):

YAML


    Node:
      oneOf:
        - $ref: "#/components/schemas/BooleanOperationNode"
        - $ref: "#/components/schemas/ComponentNode"
        - $ref: "#/components/schemas/ComponentSetNode"
        - $ref: "#/components/schemas/ConnectorNode"
        - $ref: "#/components/schemas/EllipseNode"
        - $ref: "#/components/schemas/EmbedNode"
        - $ref: "#/components/schemas/FrameNode"
        - $ref: "#/components/schemas/GroupNode"
        - $ref: "#/components/schemas/InstanceNode"
        - $ref: "#/components/schemas/LineNode"
        - $ref: "#/components/schemas/LinkUnfurlNode"
        - $ref: "#/components/schemas/RectangleNode"
        - $ref: "#/components/schemas/RegularPolygonNode"
        - $ref: "#/components/schemas/SectionNode"
        - $ref: "#/components/schemas/ShapeWithTextNode"
        - $ref: "#/components/schemas/SliceNode"
        - $ref: "#/components/schemas/StarNode"
        - $ref: "#/components/schemas/StickyNode"
        - $ref: "#/components/schemas/TableNode"
        - $ref: "#/components/schemas/TableCellNode"
        - $ref: "#/components/schemas/TextNode"
        - $ref: "#/components/schemas/TextPathNode"
        - $ref: "#/components/schemas/TransformGroupNode"
        - $ref: "#/components/schemas/VectorNode"
        - $ref: "#/components/schemas/WashiTapeNode"
        - $ref: "#/components/schemas/WidgetNode"
        - $ref: "#/components/schemas/DocumentNode"
        - $ref: "#/components/schemas/CanvasNode"
      discriminator:
        propertyName: type
        mapping:
          BOOLEAN_OPERATION: "#/components/schemas/BooleanOperationNode"
          COMPONENT: "#/components/schemas/ComponentNode"
          COMPONENT_SET: "#/components/schemas/ComponentSetNode"
          CONNECTOR: "#/components/schemas/ConnectorNode"
          ELLIPSE: "#/components/schemas/EllipseNode"
          EMBED: "#/components/schemas/EmbedNode"
          FRAME: "#/components/schemas/FrameNode"
          GROUP: "#/components/schemas/GroupNode"
          INSTANCE: "#/components/schemas/InstanceNode"
          LINE: "#/components/schemas/LineNode"
          LINK_UNFURL: "#/components/schemas/LinkUnfurlNode"
          RECTANGLE: "#/components/schemas/RectangleNode"
          REGULAR_POLYGON: "#/components/schemas/RegularPolygonNode"
          SECTION: "#/components/schemas/SectionNode"
          SHAPE_WITH_TEXT: "#/components/schemas/ShapeWithTextNode"
          SLICE: "#/components/schemas/SliceNode"
          STAR: "#/components/schemas/StarNode"
          STICKY: "#/components/schemas/StickyNode"
          TABLE: "#/components/schemas/TableNode"
          TABLE_CELL: "#/components/schemas/TableCellNode"
          TEXT: "#/components/schemas/TextNode"
          TEXT_PATH: "#/components/schemas/TextPathNode"
          TRANSFORM_GROUP: "#/components/schemas/TransformGroupNode"
          VECTOR: "#/components/schemas/VectorNode"
          WASHI_TAPE: "#/components/schemas/WashiTapeNode"
          WIDGET: "#/components/schemas/WidgetNode"
          DOCUMENT: "#/components/schemas/DocumentNode"
          CANVAS: "#/components/schemas/CanvasNode"
Generation Details

The java generator is being used (likely with default libraries).

Command to reproduce (using CLI Node.js)

openapi-generator-cli generate -i openapi.yaml -g java -o ./types/java

Steps to reproduce
  1. Use a valid OpenAPI 3.1 spec (e.g., openapi.yaml) that contains the Node schema provided above.
  2. Run the java generator against this spec using either the Node.js CLI or the Java JAR
  3. The generation process will crash when attempting to process the Node model.
  4. Observe the RuntimeException caused by the NullPointerException in the console output.
Related issues/PRs

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions