OpenAPITools / OpenAPITools/openapi-generator

[BUG] [REGRESSION] [JAVA] [SPRING] allOf no longer generating correct annotations in model getter method code for properties marked as Required and NotNull

Open
#21,790 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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 running the generator with a setup like

components:
  schemas:
    Mailbox:
      type: object
      allOf:
        - $ref: '#/components/schemas/MailboxProperties'
        - required:
          - name
          - userIdentifier
    MailboxProperties:
      type: object
      properties:
        name:
          type: string
          minLength: 1
          maxLength: 255
          nullable: false
        userIdentifier:
          type: string
          minLength: 1
          maxLength: 255
          nullable: false

the output in 7.13.0 and earlier looked like the following for the model of Mailbox for the name getter

  @NotNull @Size(min = 1, max = 255) 
  @Schema(name = "name", requiredMode = Schema.RequiredMode.REQUIRED)
  @JsonProperty("name")
  public String getName() {
    return name;
  }

with the upgrade to 7.14.0, this has lost the @NotNull annotation and has the incorrect value for the requiredMode

  @Size(min = 1, max = 255) 
  @Schema(name = "name", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  @JsonProperty("name")
  public @Nullable String getName() {
    return name;
  }

This is breaking alot of our services which rely on these to perform validation

openapi-generator version

I'm upgrading from 7.10.0 to 7.14.0 but verified the regression occurs in 7.14.0 as it doesnt occur with 7.13.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: mailbox
  version: 1.0.0
paths:
  /mailboxes:
    post:
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Mailbox'
      responses:
        '201':
          description: created.
components:
  schemas:
    Mailbox:
      type: object
      allOf:
        - $ref: '#/components/schemas/MailboxProperties'
        - required:
          - name
          - userIdentifier
    MailboxProperties:
      type: object
      properties:
        name:
          type: string
          minLength: 1
          maxLength: 255
          nullable: false
        emailAddress:
          type: string
          format: email
          minLength: 1
          maxLength: 255
          nullable: false
        userIdentifier:
          type: string
          minLength: 1
          maxLength: 255
          nullable: false

Generation Details

Run via the gradle kotlin build.gradle.kts file the task openApiGenerate

plugins {
    id("java")
    id("org.openapi.generator") version "7.14.0"
}

group = "com.test"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

openApiGenerate {
    generatorName.set("spring")
    inputSpec.set("${projectDir}/openapi.yaml")
    outputDir.set("${buildDir}/generated/openapi")
    apiPackage.set("com.test.bob.api")
    modelPackage.set("com.test.bob.model")
    invokerPackage.set("com.test.bob.invoker")
    configOptions.set(
        mapOf(
            "configPackage" to "com.test.bob.configuration",
            "useSpringBoot3" to "true",
            "delegatePattern" to "true",
            "generatedConstructorWithRequiredArgs" to "false"
        )
    )
}
sourceSets["main"].java.srcDir("${buildDir}/generated/openapi/src/main/java")
Steps to reproduce

Generate the java code from the spec, inspect the model class for Mailbox, note the getter for name has been generated as

  @Size(min = 1, max = 255) 
  @Schema(name = "name", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  @JsonProperty("name")
  public @Nullable String getName() {
    return name;
  }

when this should be

  @NotNull @Size(min = 1, max = 255) 
  @Schema(name = "name", requiredMode = Schema.RequiredMode.REQUIRED)
  @JsonProperty("name")
  public String getName() {
    return name;
  }

Related issues/PRs

I think it maybe related to the fix for https://github.com/OpenAPITools/openapi-generator/pull/21342 in 7.1.4.0 version @wing328

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the Gradle openApiGenerate task with the provided OpenAPI spec and Spring settings, comparing generated Mailbox getters between 7.13.0 and 7.14.0. Trace the Spring generator’s model-property annotation handling and use the shown name getter output as the acceptance check: required allOf properties should retain @NotNull, Required, and a non-null return type.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.