swagger-typescript icon indicating copy to clipboard operation
swagger-typescript copied to clipboard

Discriminator / mapping

Open DarioMagniPLRM opened this issue 1 year ago • 0 comments

Hi, I noticed that the contents of the 'discriminator' field seems to be unused.

Using this example swagger.json, generated using polymorphic classes with discriminator

"components": {
    "schemas": {
      "HotShearBaseDto": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "hotShearType": {
            "enum": [
              "CRANK",
              "ROTARY",
              "COMBI"
            ],
            "type": "string"
          }
        },
        "additionalProperties": false
      },
      "HotShearCOMBIDto": {
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/HotShearDto"
          }
        ],
        "properties": {
          "crankRadius": {
            "type": "number",
            "format": "double",
            "nullable": true
          },
          "bladesRadius": {
            "type": "number",
            "format": "double",
            "nullable": true
          }
        },
        "additionalProperties": false
      },
      "HotShearCRANKDto": {
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/HotShearDto"
          }
        ],
        "properties": {
          "crankRadius": {
            "type": "number",
            "format": "double",
            "nullable": true
          }
        },
        "additionalProperties": false
      },
      "HotShearDto": {
        "required": [
          "hotShearType"
        ],
        "type": "object",
        "properties": {
          "hotShearType": {
            "type": "string"
          },
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string",
            "nullable": true
          }
        },
        "additionalProperties": false,
        "discriminator": {
          "propertyName": "hotShearType",
          "mapping": {
            "CRANK": "#/components/schemas/HotShearCRANKDto",
            "ROTARY": "#/components/schemas/HotShearROTARYDto",
            "COMBI": "#/components/schemas/HotShearCOMBIDto"
          }
        }
      },
      "HotShearROTARYDto": {
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/HotShearDto"
          }
        ],
        "properties": {
          "bladesRadius": {
            "type": "number",
            "format": "double",
            "nullable": true
          }
        },
        "additionalProperties": false
      }
    },
  },

the following code is generated:

export interface HotShearBaseDto {
  description?: string
  hotShearType?: 'CRANK' | 'ROTARY' | 'COMBI'
  /**
   *
   * - Format: uuid
   */
  id?: string
}

export type HotShearCOMBIDto = {
  /**
   *
   * - Format: double
   */
  bladesRadius?: number
  /**
   *
   * - Format: double
   */
  crankRadius?: number
} & HotShearDto

export type HotShearCRANKDto = {
  /**
   *
   * - Format: double
   */
  crankRadius?: number
} & HotShearDto

export interface HotShearDto {
  hotShearType: string
  description?: string
  /**
   *
   * - Format: uuid
   */
  id?: string
  name?: string
}

export type HotShearROTARYDto = {
  /**
   *
   * - Format: double
   */
  bladesRadius?: number
} & HotShearDto

The problem concerns HotShearDto as the swagger contains the following properties:

"discriminator": {
          "propertyName": { "hotShearType",
          "mapping": {
            "CRANK": "#/components/schemas/HotShearCRANKDto",
            "ROTARY": "#/components/schemas/HotShearROTARYDto",
            "COMBI": "#/components/schemas/HotShearCOMBIDto"
          }
        }

which are ignored, as the generated code is as follows:

export interface HotShearDto {
  hotShearType: string

What I would expect is very similar to HotShearBaseDto, without the question mark, e.g.

export interface HotShearBaseDto {
  hotShearType: 'CRANK' | 'ROTARY' | 'COMBI'

It would be perfect if in addition, if possible, for each property in the mapping, its value was given, e.g.

export type HotShearCRANKDto = {
hotShearType: 'CRANK'
  /**
   *
   * - Format: double
   */
  crankRadius?: number
} & HotShearDto

DarioMagniPLRM avatar Feb 14 '24 09:02 DarioMagniPLRM