OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] OpenAPI 3.1.0: Wrong generation of Model-Classes when object has multiple object arrays

Open
#17,742 2 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

Description

In my API I have an object with two object arrays. When I generate the java classes the first list has the correct list type,
the second has the type "Object", which is wrong.

openapi-generator version

7.2.0

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  title: Example
  version: 0.0.1
components:
  schemas:
    ParentObject:
      type: object
      properties:
        listOfChildren1:
          type: array
          items:
            $ref: '#/components/schemas/ChildObject'
        listOfChildren2:
          type: array
          items:
            $ref: '#/components/schemas/ChildObject'
        listOfChildren3:
          type: array
          items:
            $ref: '#/components/schemas/ChildObject'
    ChildObject:
      type: object
      properties:
        testProp:
          type: string
Generation Details

Classes are generated with openapi-generator generate -g java -i openapi3_1.yaml -o output

Steps to reproduce

Just generate the classes with the command above.

Suggest a fix

The properties of class "ParentObject" should be a list type, not an object.
This is what's being generated:

public class ParentObject {
  public static final String SERIALIZED_NAME_LIST_OF_CHILDREN1 = "listOfChildren1";
  @SerializedName(SERIALIZED_NAME_LIST_OF_CHILDREN1)
  private List<ChildObject> listOfChildren1;

  public static final String SERIALIZED_NAME_LIST_OF_CHILDREN2 = "listOfChildren2";
  @SerializedName(SERIALIZED_NAME_LIST_OF_CHILDREN2)
  private Object listOfChildren2 = null; //should be of type List<ChildObject>

  public static final String SERIALIZED_NAME_LIST_OF_CHILDREN3 = "listOfChildren3";
  @SerializedName(SERIALIZED_NAME_LIST_OF_CHILDREN3)
  private Object listOfChildren3 = null; //should be of type List<ChildObject>
  (...)

but it should be

public class ParentObject {
  public static final String SERIALIZED_NAME_LIST_OF_CHILDREN1 = "listOfChildren1";
  @SerializedName(SERIALIZED_NAME_LIST_OF_CHILDREN1)
  private List<ChildObject> listOfChildren1;

  public static final String SERIALIZED_NAME_LIST_OF_CHILDREN2 = "listOfChildren2";
  @SerializedName(SERIALIZED_NAME_LIST_OF_CHILDREN2)
  private List<ChildObject> listOfChildren2; //correct type

  public static final String SERIALIZED_NAME_LIST_OF_CHILDREN3 = "listOfChildren3";
  @SerializedName(SERIALIZED_NAME_LIST_OF_CHILDREN3)
  private List<ChildObject> listOfChildren3; //correct type
  (...)

of course with the right setter and getter methods as these from "listOfChildren1".

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 openapi-generator generate -g java -i openapi3_1.yaml -o output with the provided OpenAPI 3.1 schema and inspect the generated ParentObject class. Trace how the Java generator resolves the three array properties, then add a regression test showing that all are generated as List<ChildObject> with matching accessors.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.