swagger-api / swagger-api/swagger-codegen

Add a vendor extension to specify the discriminator value for a model definition

Open
#4,244 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

Follow-on to
https://github.com/swagger-api/swagger-codegen/pull/4085
https://github.com/swagger-api/swagger-codegen/issues/4226

pr 4085 creates Jackson annotations that use the name of the model. In some cases, it is not desired to use the model name in the annotation because the name specified there is used in the json payload.

Swagger-codegen version

2.2.2-SNAPSHOT

Swagger declaration file content or url
definitions:
BaseObj:
    type: object
    discriminator: object_type
    required:
      - id
      - object_type
    properties:
      id:
        type: integer
        format: int64
      object_type:
        type: string
        readOnly: true
  SubObjType:
    type: string
    enum: 
    - daily
    - monthly
    - quarterly
    - yearly

  SubObj:
    allOf:
      - $ref: '#/definitions/BaseObj'
      - type: object
        discriminator: sub_obj_type
        required:
          - sub_obj_type
        properties:          
          sub_obj_type:
            $ref: '#/definitions/SubObjType'
          name:
            type: string
  DailySubObj:
    allOf:
      - $ref: '#/definitions/SubObj'
      - type: object
         properties:
           day_of_month:
             type: integer
             format: int32
Related issues

https://github.com/swagger-api/swagger-codegen/pull/4085
https://github.com/swagger-api/swagger-codegen/issues/4226

Suggest a Fix

Currently (with pr 4226), with the Java language, this will generate the following annotations for BaseObj and SubObj.

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "object_type")
@JsonSubTypes({ @Type(value = SubObj.class, name = "SubObj") })
public class BaseObj {
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "sub_obj_type")
@JsonSubTypes({ @Type(value = DailySubObj.class, name = "DailySubObj") })
public class SubObj {
}

In order to allow for a customized name in the @Type annotation, I suggest providing a vendor extension that can be used to specify the value.

This would allow for the following annotations to be generated instead. I suggest "x-discriminator-value" as the name of the vendor extension.

Here is an example of the spec from above with the vendor extension and the code that would be generated.

definitions:
BaseObj:
    type: object
    discriminator: object_type
    required:
      - id
      - object_type
    properties:
      id:
        type: integer
        format: int64
      object_type:
        type: string
        readOnly: true
  SubObjType:
    type: string
    enum: 
    - daily
    - monthly
    - quarterly
    - yearly

  SubObj:
    x-discriminator-value: sub-obj
    allOf:
      - $ref: '#/definitions/BaseObj'
      - type: object
        discriminator: sub_obj_type
        required:
          - sub_obj_type
        properties:          
          sub_obj_type:
            $ref: '#/definitions/SubObjType'
          name:
            type: string
  DailySubObj:
    x-discriminator-value: daily
    allOf:
      - $ref: '#/definitions/SubObj'
      - type: object
         properties:
           day_of_month:
             type: integer
             format: int32
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "object_type")
@JsonSubTypes({ @Type(value = SubObj.class, name = "sub-obj") })
public class BaseObj {
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "sub_obj_type")
@JsonSubTypes({ @Type(value = DailySubObj.class, name = "daily") })
public class SubObj {
}

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 by tracing swagger-codegen's Java discriminator annotation generation and the vendor-extension handling related to PR 4085 and issue 4226. Confirm that x-discriminator-value changes the generated @Type name while the existing model-name behavior remains the fallback, using the BaseObj, SubObj, and DailySubObj examples as the expected result.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Feature
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.