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

[TYPESCRIPT] Alias support for typescript-fetch templates

Open celleb opened this issue 5 years ago • 1 comments

Description

The current typescript-fetch templates do not generate type aliases for anyOf or oneOf. It currently only generates an empty interface.

Swagger-codegen version

3.0.0

Swagger declaration file content or url
openapi: 3.0.0
info:
  version: 2.0.0
  title: Test
  description: API documentation testing
servers:
  - url: 'https://localhost:{port}'
    variables:
      port:
        default: '8080'
  - url: 'https://testing.app'
    description: Staging server
  - url: 'https://prod.testing.app'
    description: Production server
paths:
  /trips:
    get:
      tags:
        - trips
      summary: Return a collection of trips
      operationId: getTrips
      responses:
        '200':
          description: Trips successfully returned
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Trip'
        '404':
          $ref: '#/components/responses/NotFoundError'
components:
  schemas:
    TripBase:
      type: object
      properties:
        id:
          type: string
          example: 1
        date:
          type: string
          example: '2017-11-01T09:00:00.000Z'
        departFrom:
          type: integer
          example: 3
        destination:
          type: integer
          example: 2
        type:
          $ref: '#/components/schemas/TripType'
        price:
          type: number
          example: 200
        entry_date:
          type: string
          example: '2017-11-01T09:00:00.000Z'
        user_id:
          type: string
          readOnly: true
        status:
          $ref: '#/components/schemas/TripStatus'
      required:
        - date
        - departFrom
        - destination
        - type
        - price
        - id
        - user_id
        - status
        - entry_date
    TripDriver:
      allOf:
        - $ref: '#/components/schemas/TripBase'
        - type: object
          properties:
            vehicle:
              type: integer
              example: 3
            passengers:
              type: integer
              example: 3
          required:
            - passengers
            - vehicle
    TripParcel:
      allOf:
        - $ref: '#/components/schemas/TripBase'
        - type: object
          properties:
            vehicle:
              type: integer
              example: 3
            capacity:
              $ref: '#/components/schemas/TripParcelCapacity'
          required:
            - capacity
            - vehicle
    TripParcelCapacity:
      type: string
      enum:
        - small
        - medium
        - large
      example: small
    TripPassenger:
      allOf:
        - $ref: '#/components/schemas/TripBase'
        - type: object
          properties:
            vehicle:
              type: integer
              nullable: true
            passengers:
              type: integer
              example: 3
          required:
            - passengers
            - vehicle
    TripStatus:
      type: string
      enum:
        - active
        - cancelled
    TripType:
      type: string
      enum:
        - driver
        - passenger
        - parcel
    Trip:
      oneOf:
        - $ref: '#/components/schemas/TripPassenger'
        - $ref: '#/components/schemas/TripDriver'
        - $ref: '#/components/schemas/TripParcel'
      discriminator:
        propertyName: type
  responses:
    NotFoundError:
      description: The items(s) you are looking for could not be found
      content:
        application/json:
          schema:
            type: object
            properties:
              code:
                type: integer
                example: 404
              message:
                type: string
                example: The requested resource could not be found.
              name:
                type: string
                enum:
                  - NotFoundError
                example: NotFoundError
            required:
              - code
              - message
              - name
Command line used for generation

java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i ~/dev/swagger/build/latest.json --template-engine mustache -l typescript-fetch -o ~/Desktop/generated

Steps to reproduce
  1. Use as swagger file that makes use of anyOf or oneOf.
  2. Use swagger codegen to generate a typescript-fetch api client.
  3. Open the generated api file and look for the types you expected to be aliases. In this case trip should be export type Trip = TripPassenger | TripDriver | TripParcel;
Related issues/PRs
Suggest a fix/enhancement

Added a condition in the template to check for aliases.

celleb avatar Jun 19 '20 19:06 celleb

still not implemented :(

ddontsov-b2broker avatar Dec 02 '25 12:12 ddontsov-b2broker