Skip to content

[BUG] [Java] [spring] Properties that are both required and nullable are marked NotNull #24686

Description

@mdaul-tgtg

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

When a nullable property is required, the generated class has a non-nullable field and getter.

openapi-generator version

7.24.0

OpenAPI declaration file content or url

This is an example spec that I used to reproduce:

{
  "openapi": "3.1.0",
  "info": {
    "title": "Reproduce",
    "version": "1.0.0"
  },
  "paths": {
    "/thing": {
      "get": {
        "operationId": "getThing",
        "responses": {
          "200": {
            "description": "ok",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Thing"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Thing": {
        "type": "object",
        "required": [
          "name",
          "description"
        ],
        "properties": {
          "name": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ]
          },
          "description": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ]
          }
        }
      }
    }
  }
}
Generation Details

Java 17

JAR=openapi-generator/modules/openapi-generator-cli/target/openapi-generator-cli.jar

"$JAVA_HOME/bin/java" -jar "$JAR" generate \
  -i spec.json \
  -g spring \
  -o out \
  --additional-properties=useJspecify=true,hideGenerationTimestamp=true,openApiNullable=false
Steps to reproduce

Run the generator tool on the spec above with the provided options. Observe that the resulting class does not have the right nullability marks:

public class Thing {

  private String name = null; // field should be @Nullable

  /**
   * Get name
   * @return name
   */
  @NotNull // wrong annotation, should be @Nullable
  @Schema(name = "name", requiredMode = Schema.RequiredMode.REQUIRED)
  @JsonProperty("name")
  public String getName() {
    return name;
  }

  @JsonProperty("name")
  public void setName(String name) { // parameter should be @Nullable
    this.name = name;
  }
Related issues/PRs

#23866
#20827

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