OpenAPITools / OpenAPITools/openapi-generator

[Java] Bug - Doesn't generate correct classes for models with same nested properties

Open
#8,948 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

When an inline model has two or more properties that are inline objects (not using $ref) and those objects have the same set of properties the generator combines them in to a single Java class and references that class for both children properties even though these objects may not be conceptually related at all (see YAML example below)

openapi-generator version

5.0.1

OpenAPI declaration file content or url
components:
  schemas:
    Receipt:
      type: object
      properties:
        Order:
          type: object
          properties:
            id:
              type: integer
              format: int64
            name:
              type: string
        Customer:
          type: object
          properties:
            id:
              type: integer
              format: int64
            name:
              type: string
Command line used for generation

generate -i -g java --additional-properties=withXml=false,java8=true,library=native,serializationLibrary=jackson,dateLibrary=java8,hideGenerationTimestamp=true -o ./client

Steps to reproduce

Run generator using the above command line on a YAML file that includes the above snippet and you will get code that looks like this for Receipt:

public class Receipt {
  @SerializedName("Order")
  private ReceiptOrder order = null;

  @SerializedName("Customer")
  private ReceiptOrder customer = null;

Note both the "order" property and the "customer" property are of type ReceiptOrder which while functional seems wrong or at the very least confusing to anyone using the client, especially for methods with signatures like: setCustomer(ReceiptOrder customer)

Related issues/PRs

Not for openapi-generator but in swagger-codegen:
https://github.com/swagger-api/swagger-codegen/issues/4883

Suggest a fix/enhancement

I would assume the default would not combine objects and would only combine objects when a $ref was used explicitly. At the very least an option should be provided to turn this combining logic off.

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 provided OpenAPI YAML, Java generator, and command-line options. Trace how the generator handles the inline Order and Customer objects, then verify that the generated Receipt uses distinct types for those properties and add a regression test covering the example.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.