Skip to content

[BUG][Java][Spring] useOneOfInterfaces=false: a merged oneOf class marks the required properties of every branch @NotNull #25789

Open

Description

@Arc-E-Tect

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? (tested with 7.27.0)
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
Description

With the spring generator, useOneOfInterfaces: false and useBeanValidation: true, a oneOf without a discriminator becomes one class that holds the properties of every branch.
Each property that its own branch requires is annotated @NotNull in that merged class, so the class requires the properties of all branches at once.
Bean validation then refuses every valid request body, which only carries the properties of one branch.

openapi-generator version

7.27.0 (Gradle plugin org.openapi.generator).

OpenAPI declaration file content or url
openapi: 3.1.0
info: {title: Example, version: 1.0.0}
paths:
  /deliveries:
    post:
      operationId: CreateDelivery
      requestBody:
        required: true
        content:
          application/json:
            schema: {$ref: '#/components/schemas/DeliveryRequest'}
      responses:
        '201': {description: created}
components:
  schemas:
    DeliveryRequest:
      oneOf:
        - $ref: '#/components/schemas/ToAddress'
        - $ref: '#/components/schemas/ToPickUpPoint'
    ToAddress:
      type: object
      additionalProperties: false
      required: [street]
      properties:
        street: {type: string, minLength: 1}
    ToPickUpPoint:
      type: object
      additionalProperties: false
      required: [pointId]
      properties:
        pointId: {type: string, pattern: '^[0-9]{6}$'}
Generation Details

generatorName: spring, configOptions: interfaceOnly: true, useBeanValidation: true, useOneOfInterfaces: false, openApiNullable: false, useSpringBoot4: true, useJackson3: true.

Steps to reproduce

Generate, then look at DeliveryRequest:

@NotNull @Size(min = 1)
public String getStreet() { ... }

@NotNull @Pattern(regexp = "^[0-9]{6}$")
public String getPointId() { ... }

A valid body such as {"street": "Main Street 1"} fails validation with "pointId must not be null", and {"pointId": "123456"} fails with "street must not be null".

Expected: the properties of a merged oneOf class carry their own constraints (@Size, @Pattern) but not @NotNull, since no single property is required by the oneOf as a whole; checking that exactly one branch is present is left to the application, or to a generated check.

Related issues/PRs

#22150 is about a wrong @NotNull for a property received through allOf; this one is about oneOf.

Suggest a fix

In JavaSpring/notNull.mustache, skip @NotNull for the properties of a model that is a merged oneOf.
As a workaround, wrapping the partial in {{^oneOf}}…{{/oneOf}} in a templateDir override does this: inside a property the lookup reaches the model's non-empty oneOf set.

Activity

  1. jpfinne commented on Nov 23, 2026

    @jpfinne
    Contributor

    This is a duplicate of #25727, #23667, #17938, #20513 (kotlin), and probably other.

    I think this can be resolved by a simple rule in OpenApiNormalizer.

  2. Mattias-Sehlstedt commented on Nov 24, 2026

    @Mattias-Sehlstedt
    Contributor

    I recognize that it is a wide and regression-filled task, but isn't this something that should reside in the Codegen when construction an internal composed model?

    Placing it in the Normalizer feels like "hiding" the issue and coercing the specification structure into something suitable for the generator makes it very difficult to keep track of what functionality one gets with a certain Normalizer configuration (i.e., what do we get when Y and X is on but Z is off).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions