Skip to content

Check whether to generate JsonSubTypes annotations based on discriminator values #1022

@ricardozanini

Description

@ricardozanini

Relates to #830

My initial assumption was our generator's lack of legacyDiscriminatorBehavior property support. It turns out that this property is not related to the original problem.

Given:

    "schemas": {
      "OtherThing": {
        "description": "Other thing",
        "required": [
          "other"
        ],
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/Thing"
          }
        ],
        "properties": {
          "other": {
            "type": "string"
          }
        }
      },
      "SomeThing": {
        "description": "Some thing",
        "required": [
          "some"
        ],
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/Thing"
          }
        ],
        "properties": {
          "some": {
            "type": "string"
          }
        }
      },
      "Thing": {
        "description": "Thing",
        "required": [
          "thing"
        ],
        "type": "object",
        "properties": {
          "thing": {
            "type": "string"
          }
        },
        "discriminator": {
          "propertyName": "@type",
          "mapping": {
            "SomeThing": "#/components/schemas/SomeThing",
            "OtherThing": "#/components/schemas/OtherThing"
          }
        }
      }
    }

Thing is parent of SomeThing and OtherThing, as seen in discriminator. Each subclass has allOf defined, which tells our inner generator that they are all children of Thing. In this context, it makes sense to add JsonSubTypes based on the discriminator property.

In this other scenario:

paths:
  /user:
    get:
      operationId: users/get-authenticated
      responses:
        "200":
          content:
            application/json:
              schema:
                discriminator:
                  mapping:
                    private: '#/components/schemas/private-user'
                    public: '#/components/schemas/public-user'
                  propertyName: user_view_type
                oneOf:
                  - $ref: '#/components/schemas/private-user'
                  - $ref: '#/components/schemas/public-user'
          description: Response
components:
  schemas:
    private-user:
      description: Private User
      properties:
        avatar_url:
          format: uri
          type: string
      title: Private User
      type: object
    public-user:
      properties:
        avatar_url:
          format: uri
          type: string
      title: Public User
      type: object

The spec is not strictly telling the generator that there is an inheritance. In this case, PrivateUser and PublicUser are two objects encapsulated to a new class generated named UsersGetAuthenticated200Response with every property from both classes.

It could be smarter and generate an interface with the correct Jackson annotations instead. But since we don't have enough control of the underlying generator, the fix will avoid adding these annotations mistakenly.

Metadata

Metadata

Assignees

Labels

area:clientThis item is related to the client extension

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions