swagger-api / swagger-api/swagger-codegen

[Java] [3.0.0]Generate same field both in Parent and SubClass when using allOf

Open
#7,458 2 comments 1 reaction 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

Generated model:

public class CommonResponse implements Serializable {

  private static final long serialVersionUID = 1L;
@SerializedName("errcode")
  private Integer errcode = null;
  @SerializedName("errmsg")
  private String errmsg = null;

The subclass should not contain "errcode" and "errmsg". This will cause gson error.

public class AccessToken extends CommonResponse implements Serializable {

  private static final long serialVersionUID = 1L;
@SerializedName("errcode")
  private Integer errcode = null;
  @SerializedName("errmsg")
  private String errmsg = null;
  @SerializedName("access_token")
  private String accessToken = null;
  @SerializedName("expires_in")
  private Integer expiresIn = null;
Swagger-codegen version

61c2b6e4aac269c49b4e5d045d87531e69709be1

Swagger declaration file content or url
openapi: 3.0.1
info:
  title: wechatApi
  license:
    name: Apache 2.0
    url: https://www.apache.org/licenses/LICENSE-2.0.html
  version: 0.0.1
servers:
  - url: https://qyapi.weixin.qq.com/cgi-bin
paths:
  /gettoken:
    get:
      tags:
        - get AccessToken
      summary: access_token
        
      responses:
        '200':
          description: 返回 AccessToken
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AccessToken'
components:
  schemas:
    CommonResponse:
      type: object
      required:
        - errcode
      properties:
        errcode: 
          type: integer
          format: int32
        errmsg:
          type: string
    AccessToken:
      allOf:
        - $ref: '#/components/schemas/CommonResponse'
        - type: object
          required:
            - access_token
            - expires_in
          properties:
            access_token:
              type: string
            expires_in:
              type: integer
              format: int32
Suggest a fix/enhancement

This line also adds properties from parent to the subModel.

io.swagger.codegen.languages.DefaultCodegenConfig
addProperties(properties, required, composed, allDefinitions);

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 at DefaultCodegenConfig.java around the addProperties(properties, required, composed, allDefinitions) call referenced in the issue. Use the supplied OpenAPI allOf schema to inspect generated CommonResponse and AccessToken models. Done means inherited errcode and errmsg appear only in the parent model and the generated Java output no longer triggers the reported Gson error.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
backend-api-design, devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.