Skip to content

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

Open
@bcampolo

Description

@bcampolo
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:
swagger-api/swagger-codegen#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.

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