swagger-api / swagger-api/swagger-codegen

[TYPESCRIPT] Alias support for typescript-fetch templates

Open
#10,334 1 comment 1 reaction 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

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.

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 with the typescript-fetch templates and reproduce the issue using the documented swagger-codegen command and the supplied OpenAPI YAML. Inspect the generated API file for the Trip schema. Done means anyOf or oneOf schemas generate TypeScript aliases such as export type Trip = TripPassenger | TripDriver | TripParcel; instead of empty interfaces.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.