swagger-api / swagger-api/swagger-codegen

[Java] doesnt generate correct classes for models with same nested properties

Open
#4,883 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java General: Question
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

If there are two models in the swagger definition whose subproperties are the same, the code generator always uses the first one.

e.g. take the following two definitions

  house:
    type: object
    properties:
      has_walls:
        type: boolean
      attributes:
        type: object
        properties:
          name:
            type: string
  
  person:
    type: object
    properties:
      has_legs:
        type: boolean
      attributes:
        type: object
        properties:
          name:
            type: string

Swagger doesnt generate a PersonAttributes class at all, and instead generates:

public class Person   {
  @JsonProperty("has_legs")
  private Boolean hasLegs = null;

  @JsonProperty("attributes")
  private HouseAttributes attributes = null;
Swagger-codegen version

Latest snapshot 2.2.2

Swagger declaration file content or url
swagger: '2.0'
info:
  version: 0.0.0
  title: Simple API
paths:
  /:
    get:
      responses:
        200:
          description: OK
          schema:
            $ref: "#/definitions/house"
        400:
          description: OK
          schema:
            $ref: "#/definitions/person"

definitions:
  
  house:
    type: object
    properties:
      has_walls:
        type: boolean
      attributes:
        type: object
        properties:
          name:
            type: string
  
  person:
    type: object
    properties:
      has_legs:
        type: boolean
      attributes:
        type: object
        properties:
          name:
            type: string

Command line used for generation

java -jar bin/swagger-codegen-cli.jar generate -v -i ./swagger.yaml -l spring -Dmodels -o .

Steps to reproduce

Copy the above swagger.yaml into http://editor.swagger.io/#!/ or use the latest CLI to build as above.

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

Reproduce the issue with the supplied swagger.yaml using bin/swagger-codegen-cli.jar and the shown generate command, or the Swagger editor. Inspect the generated House and Person models and the generator entry points reached by that command. Done means distinct nested classes are generated for both models, with Person referring to PersonAttributes rather than HouseAttributes.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.