swagger-api / swagger-api/swagger-codegen

[JAVA/okhttp-gson] Bug generating noncompilable code when discriminator is used

Open
#10,012 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Generating swagger API for okhttp-gson with usage of discriminator in model definitions for inheritance and polymorphism will output non compilable code in the parent classes.

Swagger-codegen version
swaggerCodegen 'io.swagger:swagger-codegen-cli:2.4.12'
Swagger declaration file content or url
...
  SomeStatus:
    type: object
    discriminator: classType
    properties:
      statusText:
        description: The status as text
        type: string
      statusDetail:
        description: Additional information to given status.
        type: string
...
Possible fix

Changing the pojo.mustache solved the problem in my case. But I am not sure about side effects:

...
  {{#gson}}
  {{#discriminator}}
  @SerializedName("{{discriminator}}")
  protected String {{discriminator}};
  {{/discriminator}}
  {{/gson}}

  {{#parcelableModel}}
  public {{classname}}() {
  {{#parent}}
    super();
  {{/parent}}
  {{#gson}}
  {{#discriminator}}
    this.{{discriminator}} = this.getClass().getSimpleName();
  {{/discriminator}}
  {{/gson}}
  }
  {{/parcelableModel}}
  {{^parcelableModel}}
  {{#gson}}
  {{#discriminator}}
  public {{classname}}() {
    this.{{discriminator}} = this.getClass().getSimpleName();
  }
  {{/discriminator}}
  {{/gson}}
  {{/parcelableModel}}
...

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 with the affected pojo.mustache template and inspect how discriminator fields and constructors are rendered for okhttp-gson parent classes. Compare the generated output with the declaration using SomeStatus and verify that the resulting parent classes compile without introducing side effects.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.