OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] Java generator puts parent properties from allOf last instead of first and skips properties entirely for "middle" references

Open
#17,542 5 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 (example)?
  • 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

Properties should be evaluated by the templates in the order they are declared. For objects that reference a parent with allOf the properties from the parent should appear first. Instead they are processed last. This is of course very wrong. You might argue that the order of fields as they are declared in a Java class doesn't matter, but this makes the order wrong for JSON serialization (if you care), and when modifying the pojo template to produce a Java record (which works well as the properties are flattened without a descriminator) instead of a regular class the parameter order will be non-sensical. The common parts should be first so they are always at the same position.

openapi-generator version

7.2.0

OpenAPI declaration file content or url

For object schemas like this"

components:
    schemas:
        BaseObject:
            type: object
            required: 
                -  id
            properties:
                id:
                    type: string
                    description: Object ID
                name:
                    type: string
                    description: Optional name

        BigObject:
            allOf: 
                -  $ref: "#/components/schemas/BaseObject"
            type: object
            required:
                - value
            properties:
                value:
                    type: string

        BiggerObject:
            allOf: 
                -  $ref: "#/components/schemas/BigObject"
            type: object
            properties:
            weight:
                type: integer
            children:
                type: array
                items: string

This produces fields in the following order:

  private Integer weight;
  private List<String> children;
  private String id;
  private String name;

Note that the value field is missing entirely - that's another bug

instead of the correct order (and including the value field):

  private String id;
  private String name;
  private String value;
  private Integer weight;
  private List<String> children;
Generation Details

I used the Gradle plugin to generate models.

Steps to reproduce

Run the Java code generation on a model that uses allOf, note the order that the vars are processed by the template.

Related issues/PRs

https://github.com/OAI/OpenAPI-Specification/issues/2424

Suggest a fix

I am not equipped to build and test modifications to the Java code generator at this time.

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 Java model generation through the Gradle plugin on the provided allOf schemas and inspect how the templates process vars. Done means BiggerObject includes value and orders inherited fields first, followed by value, weight, and children.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.