OpenAPITools / OpenAPITools/openapi-generator

[BUG] Problem with setting example in field from parent object when using allOf

Open
#7,980 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

There is a problem with java generator, when there is object that has couple of properties and then there is another object that extends with allOf and providing example values for parent properties java generator generates getters for parent and child properties. The type of id from child should have the type from parent.

The generated code is:

public class CaseEvent {
  public static final String JSON_PROPERTY_ID = "id";
  private Integer id;

...

  public Integer getId() {
    return id;
  }


  public void setId(Integer id) {
    this.id = id;
  }
}

public class CaseStatusEvent extends CaseEvent {
  public static final String JSON_PROPERTY_ID = "id";
  private Object id;

  public static final String JSON_PROPERTY_TYPE = "type";
  protected Object type;

  public static final String JSON_PROPERTY_OLD_STATUS = "oldStatus";
  private CaseStatus oldStatus;

  public static final String JSON_PROPERTY_NEW_STATUS = "newStatus";
  private CaseStatus newStatus;

  public Object getId() {
    return id;
  }


  public void setId(Object id) {
    this.id = id;
  }

  public Object getType() {
    return type;
  }


  public void setType(Object type) {
    this.type = type;
  }

 }

Reported error when compiling: return type java.lang.Object is not compatible with java.lang.Integer

Swagger editor correctly resolves it as:


 id, integer, example: 1 id of the caseEvent
 date, string, example: 2019-01-10T10:25:13.000Z format is ISO 8601 "YYYY-MM-DDTHH:mm:ss.sssZ"
 caseId, integer, example: 1 id of the case
 user, User{...}
 type*, stringEnum:Array [ 6 ]

openapi-generator version

Using openapi-generator-maven-plugin:5.0.0-beta2 (the same issue with 4.3.0).

OpenAPI declaration file content or url

From:
https://ethereum.scorechain.com/swagger.json
https://bitcoin.scorechain.com/api_doc/ -> search for CaseStatusEvent

  CaseEvent:
    type: object
    properties:
      id:
        type: integer
        description: id of the caseEvent
        example: 1
      date:
        type: string
        description: 'format is ISO 8601 "YYYY-MM-DDTHH:mm:ss.sssZ"'
        example: '2019-01-10T10:25:13.000Z'
      caseId:
        type: integer
        description: id of the case
        example: 1
      user:
        $ref: '#/definitions/User'
      type:
        type: string
        enum:
          - CaseNoteEvent
          - CaseAddressEvent
          - CaseTxEvent
          - CaseStatusEvent
          - CaseAssignmentEvent
          - CaseReportEvent
    discriminator: type
    required:
      - type
  CaseStatusEvent:
    allOf:
      - $ref: '#/definitions/CaseEvent'
      - type: object
        properties:
          id:
            example: 50
          type:
            example: CaseStatusEvent
          oldStatus:
            $ref: '#/definitions/CaseStatus'
          newStatus:
            $ref: '#/definitions/CaseStatus'

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 Java generator and the OpenAPI declaration for CaseEvent and CaseStatusEvent linked in the report, focusing on their allOf relationship and example values. Reproduce generation with openapi-generator-maven-plugin 5.0.0-beta2 or 4.3.0, then inspect the generated classes. Done means CaseStatusEvent does not redeclare id with an incompatible type and the generated code compiles.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.