OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] Wrong return types generated for OpenAPI spec with more than one endpoint returning an array type

Open
#12,546 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

The two endpoints in the partial yaml file below should have their CodegenOperation#returnType variables contain List<String> and List<SomeObject>, respectively. However the first endpoint that gets processed has it's return type leaking into the second return type. Therefore the return types of their CodegenOperation objects become the same. This results in getting APIs with the same return types: List<String> getListOfStrings() and List<String> getListOfSomeObjects() instead of List<SomeObject> getListOfSomeObjects().

So far this issue affects only array types.

openapi-generator version

OpenAPI generator: 6.0.0

This problem doesn't occur in 4.2.0

OpenAPI declaration file content or url
paths:
  /somestrings:
    get:
      tags:
        - Test
      operationId: getListOfStrings
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  type: string
  /someobjects:
    get:
      tags:
        - Test
      operationId: getListOfSomeObjects
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  '#/components/schemas/SomeObject'

-->

Steps to reproduce

Run the JavaClientCodegen generator.

Related issues/PRs

I don't see any issues related to this.

Suggest a fix/enhancement

The problem has to do with the Schema class' equal/hash method. Schema's equals/hash methods use properties of the Schema except the items property and the type of the items is one of the ways that can be used to distinguish between arrays. So the two array schemas in the yaml snippet above, let's label them a1 and a2, are the same (equals(a1, a2) == true) because the items of the arrays are ignored. This issue affects the keys put into the cache: schemaCodegenPropertyCache(NamedSchema, CodegenProperty) in DefaultCodegen because instead of having two arrays in the cache, the first array that gets put in the cache ends up representing all arrays.

Please add items property to the equals and hash methods in Schema or override the equals and hash methods in ArraySchema.

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 reproducing the issue with the YAML declaration and JavaClientCodegen. Read Schema's equals and hash methods, then inspect DefaultCodegen's schemaCodegenPropertyCache(NamedSchema, CodegenProperty) handling. Done means the two array schemas remain distinct and the generated operations use List and List respectively.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.