Skip to content

[BUG] [JAVA][SPRING] Missing @Nullable import for array-type models in 7.19.0 #22788

@reimodaum

Description

@reimodaum

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

[JAVA][SPRING] Missing @nullable import for array-type models in 7.19.0

openapi-generator version

7.19.0

OpenAPI declaration file content or url
    ResultCodes:
      type: array
      items:
        type: object
        properties:
          type:
            type: string
            example: Comment, Match, Warning, Mismatch
          code:
            type: integer
            format: int32
            example: 3013
          override:
            type: string
            description: >-
              If Item Check results have been overriden, this property contains
              any override text supplied for the Force Override operation
          description:
            type: string
Generation Details
   openapi-generator-cli generate \
     -g spring \
     -i spec.yaml \
     -o output \
     --additional-properties=useJakartaEe=true

spec.yaml:

  openapi: 3.0.1
  info:
    title: Test
    version: v1
  components:
    schemas:
      ResultCodes:
        type: array
        items:
          type: object
          properties:
            code:
              type: string
Steps to reproduce
  1. Create an OpenAPI spec with an array-type schema (as above)
  2. Generate with spring generator using version 7.19.0+
  3. Compile the generated code

Expected: Code compiles successfully

Actual: Compilation fails with:

  error: cannot find symbol
    private String toIndentedString(@Nullable Object o) {
                                     ^
    symbol:   class Nullable

The generated ResultCodes.java uses @nullable annotation but is missing import org.springframework.lang.Nullable;

Related issues/PRs
Suggest a fix

The bug is in SpringCodegen.java. The Nullable import is added inside postProcessModelProperty() (line ~974):

  @Override
  public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
      // ...
      if (getName().contains("spring")) {
          model.imports.add("Nullable");
      }
  }

This method is only called for models that have properties. Array-type schemas generate classes extending ArrayList with no properties, so postProcessModelProperty() is never called and the import is never added.

However, pojo.mustache uses {{>nullableAnnotation}} in toIndentedString() for ALL models:

  private String toIndentedString({{>nullableAnnotation}}Object o) {

Fix options:

  1. Move model.imports.add("Nullable") to postProcessModels() or fromModel() so it runs for all models
  2. Or add import org.springframework.lang.Nullable; unconditionally in model.mustache

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