OpenAPITools / OpenAPITools/openapi-generator

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

Open
#24,778 0 comments 0 reactions 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

The behavior introduced in https://github.com/OpenAPITools/openapi-generator/pull/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

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 with the allOf_composition_discriminator.json fixture and the test cases introduced by PR 15125, comparing generated Java output when MyPets precedes Pet. Trace how isOverridden is handled for the child model; done means Cat retains the overriding petType fluent setter regardless of component schema order and the relevant generation tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.