Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
exoego opened this issue Dec 6, 2019 · 2 comments
Closed

Comments

@exoego
Copy link

exoego commented Dec 6, 2019

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.

@anttiviljami
Copy link
Member

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.

@exoego
Copy link
Author

exoego commented May 30, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants