Skip to content

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

@swpalmer-cl

Description

@swpalmer-cl

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

OAI/OpenAPI-Specification#2424

Suggest a fix

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

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