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

Relative referenced yaml files results in a never type

Open Diokuz opened this issue 2 years ago • 4 comments

Description

Relative referenced yaml files results in a never type.

export interface external {

  "bar.yaml": Record<string, never>
}
Name Version
openapi-typescript latest (npx)
Node.js 18.12.1
OS + version macOS 13.4.1

Reproduction

Screenshot 2023-10-19 at 12 11 55

foo.yaml

openapi: '3.0.2'

info:
  title: title
  license:
    name: MIT
    url: http://example.com
  version: v1

servers:
  - url: http://example1.com
    description: 'description'

paths:
  /foo:
    get:
      security:
        - api_key: []
      summary: 'summary'
      operationId: 'operationId'
      responses:
        '200':
          description: 'description'
          content:
            application/json:
              schema:
                allOf:
                  - $ref: 'bar.yaml#/components/schemas/User'
        '404':
          description: 'description'
          content:
            application/json:
              schema:
                allOf:
                  - $ref: 'bar.yaml#/components/schemas/User'

components:
  securitySchemes:
    api_key:
      type: apiKey
      name: api_key
      description: 'description'
      in: query

bar.yaml

openapi: '3.0.2'

info:
  title: title
  license:
    name: MIT
    url: http://example.com
  version: v1

servers:
  - url: http://example1.com
    description: 'description'

paths:
  /foo:
    get:
      security:
        - api_key: []
      summary: 'summary'
      operationId: 'operationId'
      responses:
        '200':
          description: 'description'
          content:
            application/json:
              schema:
                type: object
                title: 'title'
                properties:
                  countryCode:
                    allOf:
                      - $ref: '#/components/schemas/User'

        '404':
          description: 'description'
          content:
            application/json:
              schema:
                type: object
                title: 'title'
                properties:
                  countryCode:
                    allOf:
                      - $ref: '#/components/schemas/User'

components:
  securitySchemes:
    api_key:
      type: apiKey
      name: api_key
      description: 'description'
      in: query
  schemas:
    User:
      type: object
      properties:
        id:
          type: integer
          example: 4
        name:
          type: string
          example: Arthur Dent
      # Both properties are required
      required:
        - id
        - name

cli command

npx openapi-typescript ./schema2/foo.yaml -o ./schema.d.ts

Expected result

schema.d.ts

...
export interface operations {

  operationId: {
    /** summary */
    responses: {
      /** @description description */
      200: {
        content: {
          "application/json": external["bar.yaml"]["components"]["schemas"]["User"];
        };
      };
      ...
    };
  };
}

where external["bar.yaml"]["components"]["schemas"]["User"] is a normal type.

Actual result

schema.d.ts

...
export interface external {

  "bar.yaml": Record<string, never>
}
...

Checklist

So, is it possible to handle types in bar.yml?

Diokuz avatar Oct 19 '23 09:10 Diokuz

v6 supports remote schemas; this may just be a small bug in this specific scenario. Will investigate.

Also give v7 (openapi-typescript@next) a try and see if that works for you. Schema resolution has been greatly improved (and would love any feedback / bug reports / encountered issues if you do try that)

drwpow avatar Oct 19 '23 14:10 drwpow

Strange—I copied & pasted your exact schemas and I was not able to reproduce your error (on Node 18.12 as well as Node 20). I was seeing the file generate correctly. Maybe there’s something else going on that’s not captured in your reproduction (which was very helpful by the way, but I think there’s another issue here)?

drwpow avatar Oct 19 '23 15:10 drwpow

It's similar for my problem. I have remote root yaml, but referenced yaml doesn't show their types. Bumped to 6.7.0 export interface paths { '/foo': external['paths/foo-api/foo-api.yaml'] }

export interface external { 'paths/foo-api/foo-api.yaml': { /** Search for foo */ get: unknown }

In v7 next.2 it gives me types.Buy have strange things like '/foo': { parameters: { query?: never header?: never path?: never cookie?: never } /** Search for Foo */

omgpiu avatar Nov 08 '23 13:11 omgpiu

@omgpiu sometimes the unknown can stem from a hard-to-find syntax error, such as accidentally omitting the schema key in the response. And 6.x does not validate your schema, so it won’t tell you if there are any issues.

7.x generates more nevers on purpose (explanation). The fact that both versions fail seem like it’s a syntax issue. But maybe it’s a bug. Would have to see your full schema to be sure.

drwpow avatar Nov 08 '23 14:11 drwpow

This issue is stale because it has been open for 90 days with no activity. If there is no activity in the next 7 days, the issue will be closed.

github-actions[bot] avatar Aug 06 '24 12:08 github-actions[bot]

This issue was closed because it has been inactive for 7 days since being marked as stale. Please open a new issue if you believe you are encountering a related problem.

github-actions[bot] avatar Aug 14 '24 02:08 github-actions[bot]