openapi-client-axios icon indicating copy to clipboard operation
openapi-client-axios copied to clipboard

typegen fails if multiple remote/local references are used in "responses"

Open exoego opened this issue 5 years ago • 2 comments

This is a bug report.

Thanks for creating great library 👍

I am trying to use typegen on some swagger.yaml files. Those API definitions leverage remote/local reference (e.g. $ref: "path/to/entity") a lot, to minimize duplication of codes.

As far as I tested, typegen works finely with such remote/local references, ... except references in responses fields 😭 Those API definitions work finely with other Swagger-based tools (e.g. Swagger editor and Redoc), so I suspect this is an issue in openali-client-axios-typegen.

versions

  • typegen: 1.0.7

What happend

The below is a reproducible swagger file definitions.

We prepare 2 files.

  • swagger.yaml
  • shared-api-components.yaml

swagger.yaml

openapi: "3.0.0"
info:
  version: 0.66.7
  title: My API
servers: []
paths:
  /login:
    post:
      summary: login
      security: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: string
      responses:
        200:
          $ref: "./shared-components.yaml#/responses/LoginSuccess"
        503:
          $ref: "./shared-api-components.yaml#/responses/ServiceUnavailable"

shared-api-components.yaml

schemas:
  ErrorCode:
    type: string
    enum:
      - Unauthorized
      - UnderMaintenance
  ErrorUnauthorizedApi:
    type: object
    properties:
      errors:
        type: array
        items:
          $ref: "#/schemas/ErrorCode"
  ErrorUnderMaintenance:
    type: object
    properties:
      errors:
        type: array
        items:
          $ref: "#/schemas/ErrorCode"
responses:
  ErrorUnauthorizedApi:
    content:
      application/json:
        schema:
          $ref: "#/schemas/ErrorUnauthorizedApi"
  ErrorUnderMaintenance:
    content:
      application/json:
        schema:
          $ref: "#/schemas/ErrorUnderMaintenance"

Then, run typegen swagger.yaml > foo.d.ts and we will got

(node:630) UnhandledPromiseRejectionWarning: Error: The $ref targets root is not found: #/paths/~1login/post/responses/503/content/application~1json/schema/properties/errors/items
    at ReferenceResolver.<anonymous> (/usr/local/lib/node_modules/openapi-client-axios-typegen/node_modules/@anttiviljami/dtsgenerator/dist/core/referenceResolver.js:105:35)
    at step (/usr/local/lib/node_modules/openapi-client-axios-typegen/node_modules/tslib/tslib.js:136:27)
    at Object.next (/usr/local/lib/node_modules/openapi-client-axios-typegen/node_modules/tslib/tslib.js:117:57)
    at /usr/local/lib/node_modules/openapi-client-axios-typegen/node_modules/tslib/tslib.js:110:75
    at new Promise (<anonymous>)
    at Object.__awaiter (/usr/local/lib/node_modules/openapi-client-axios-typegen/node_modules/tslib/tslib.js:106:16)
    at ReferenceResolver.resolve (/usr/local/lib/node_modules/openapi-client-axios-typegen/node_modules/@anttiviljami/dtsgenerator/dist/core/referenceResolver.js:26:24)
    at DtsGenerator.<anonymous> (/usr/local/lib/node_modules/openapi-client-axios-typegen/node_modules/@anttiviljami/dtsgenerator/dist/core/dtsGenerator.js:22:50)
    at step (/usr/local/lib/node_modules/openapi-client-axios-typegen/node_modules/tslib/tslib.js:136:27)
    at Object.next (/usr/local/lib/node_modules/openapi-client-axios-typegen/node_modules/tslib/tslib.js:117:57)

Note

I observed some cases where typegen runs successfully with references on some cases.

Case 1. Limit the occurence of remote reference in responses to 1.

In swagger.yaml,

      responses:
        # 401:
        #  $ref: "swagger-api-components.yaml#/responses/ErrorUnauthorizedApi"
        503:
          $ref: "swagger-api-components.yaml#/responses/ErrorUnderMaintenance"

But this is not desirable, since I want to use remote reference/local reference to minimize code duplication.

Case 2. Limit the occurence of same local reference in external files to 1.

In shared-api-components.yaml, changed like below

schemas:
  ErrorCode:
    type: string
    enum:
      - Unauthorized
      - UnderMaintenance
  ErrorUnauthorizedApi:
    type: object
    properties:
      errors:
        type: array
        items:
          $ref: "#/schemas/ErrorCode"
  ErrorUnderMaintenance:
    type: object
    properties:
      errors:
        type: array
        items:
          type: string
          # HERE:  Removed local reference that already used above.
          # $ref: "#/schemas/ErrorCode"
responses:
  ErrorUnauthorizedApi:
    content:
      application/json:
        schema:
          $ref: "#/schemas/ErrorUnauthorizedApi"
  ErrorUnderMaintenance:
    content:
      application/json:
        schema:
          $ref: "#/schemas/ErrorUnderMaintenance"

But this is not desirable, since I want to use remote reference/local reference to minimize code duplication.

exoego avatar Dec 06 '19 04:12 exoego

Hi @exoego. Sorry for such a late response. This seems like a bug in the underlying https://github.com/horiuchi/dtsgenerator library. Unfortunately there's not much we can do on typegen side.

anttiviljami avatar May 30 '20 15:05 anttiviljami

Thanks for investigating. Grad to see someone is working on dtsgenearator. https://github.com/horiuchi/dtsgenerator/pull/414 I will suggest to keep this issue open until dtsgenerator support that.

exoego avatar May 30 '20 21:05 exoego