openapi-ts icon indicating copy to clipboard operation
openapi-ts copied to clipboard

Handle discriminator for transformers

Open amarant opened this issue 3 months ago • 4 comments

Description

Hi, here is a pretty common openapi.yaml using a simple discriminator, and it would be great if the transformer could handle the getPets that can output cats and dogs with each one their own date fields.

for now it output a transformer for the endpoint getCats of cats only, but not the pets one for cats or dogs

openapi.yaml
openapi: 3.0.0
info:
  title: (title)
  version: 0.0.0
tags: []
paths:
  /cats:
    get:
      operationId: getCats
      parameters: []
      responses:
        '200':
          description: The request has succeeded.
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Cat'
  /pets:
    get:
      operationId: getPets
      parameters: []
      responses:
        '200':
          description: The request has succeeded.
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Pet'
components:
  schemas:
    Cat:
      type: object
      required:
        - type
        - napDate
      properties:
        type:
          type: string
          enum:
            - cat
        napDate:
          type: string
          format: date-time
      allOf:
        - type: object
          required:
            - type
            - name
            - birthDate
          properties:
            type:
              type: string
              enum:
                - cat
            name:
              type: string
            birthDate:
              type: string
              format: date-time
    Dog:
      type: object
      required:
        - type
        - walkDate
      properties:
        type:
          type: string
          enum:
            - dog
        walkDate:
          type: string
          format: date-time
      allOf:
        - type: object
          required:
            - type
            - name
            - birthDate
          properties:
            type:
              type: string
              enum:
                - dog
            name:
              type: string
            birthDate:
              type: string
              format: date-time
    Pet:
      type: object
      oneOf:
        - $ref: '#/components/schemas/Cat'
        - $ref: '#/components/schemas/Dog'
      discriminator:
        propertyName: type
        mapping:
          cat: '#/components/schemas/Cat'
          dog: '#/components/schemas/Dog'
    PetType:
      type: string
      enum:
        - cat
        - dog

amarant avatar Oct 20 '25 10:10 amarant

This is a duplicate of #2808

AnderssonPeter avatar Oct 22 '25 06:10 AnderssonPeter

Technically he was first, but interesting you had the same idea few minutes apart

mrlubos avatar Oct 22 '25 06:10 mrlubos

@mrlubos both are duplicates of each other :) It doesn't matter which stays open :), all that matters is that here is one :)

AnderssonPeter avatar Oct 22 '25 06:10 AnderssonPeter

Kinda crazy we had the same idea in matters of minutes, and all about cats and dogs endpoints too.

amarant avatar Oct 22 '25 07:10 amarant