Skip to content

[BUG] [Java] Fluent builder through allOf is lost if the child is read before the parent #24778

Description

@Mattias-Sehlstedt

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

The behavior introduced in #15125 is dependent on the order of the component schemas.

If the child models comes before the parent the isOverridden is not set to true properly, and thus the child will not define a fluent setter that overrides the inherited property.

This can be seen by changing the allOf_composition_discriminator.json so that MyPets

MyPets:
  oneOf:
    - $ref: '#/components/schemas/Cat'
    - $ref: '#/components/schemas/Dog'

comes before the definition of the parent and the children

Pet:
  type: object
  required:
    - petType
  properties:
    petType:
      type: string
  discriminator:
    propertyName: petType
Cat:
  allOf:
    - $ref: '#/components/schemas/Pet'
    - type: object
      properties:
        name:
          type: string
        characteristics:
          $ref: '#/components/schemas/Characteristics'
Dog:
  allOf:
    - $ref: '#/components/schemas/Pet'
    - type: object
      properties:
        bark:
          type: string

I would expect Cat to be

public class Cat extends Pet {
  public static final String JSON_PROPERTY_NAME = "name";
  @jakarta.annotation.Nullable
  private String name;

  public static final String JSON_PROPERTY_CHARACTERISTICS = "characteristics";
  @jakarta.annotation.Nullable
  private Characteristics characteristics;

  public Cat() {

  }

  // Other fluent setters...

  @Override
  public Cat petType(@jakarta.annotation.Nonnull String petType) {
    this.setPetType(petType);
    return this;
  }
}

and have the petType as the test also asserts. But if the order is changed the fluent builder method is lost entirely from Cat.

openapi-generator version

7.25

OpenAPI declaration file content or url
openapi: 3.0.2
info:
  title: OAI Specification example for Polymorphism
  version: 1.0.0
paths:
  /pet:
    get:
      responses:
        '200':
          description: desc
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
components:
  schemas:
    MyPets:
      oneOf:
        - $ref: '#/components/schemas/Cat'
        - $ref: '#/components/schemas/Dog'
      discriminator:
        propertyName: petType
        # per https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#discriminator-object
        # this discriminator must be included to use it as a hint to pick a schema
    Pet:
      type: object
      required:
        - petType
      properties:
        petType:
          type: string
      discriminator:
        propertyName: petType
    Cat:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            name:
              type: string
            characteristics:
              $ref: '#/components/schemas/Characteristics'
    Dog:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            bark:
              type: string
    Characteristics:
      type: object
      properties:
        canHunt:
          type: boolean
Generation Details

See test cases introduced for the PR shared above.

Steps to reproduce

See test cases introduced for the PR shared above.

Related issues/PRs

See test cases introduced for the PR shared above.

Suggest a fix

I will investigate how to handle isOverridden to get this to work as expected

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