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

Relative referenced yaml files results in a never type #1397

Open
1 of 2 tasks
Diokuz opened this issue Oct 19, 2023 · 4 comments
Open
1 of 2 tasks

Relative referenced yaml files results in a never type #1397

Diokuz opened this issue Oct 19, 2023 · 4 comments
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@Diokuz
Copy link

Diokuz commented Oct 19, 2023

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 Diokuz added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Oct 19, 2023
@drwpow
Copy link
Contributor

drwpow commented Oct 19, 2023

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
Copy link
Contributor

drwpow commented Oct 19, 2023

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)?

@omgpiu
Copy link

omgpiu commented Nov 8, 2023

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 */

@drwpow
Copy link
Contributor

drwpow commented Nov 8, 2023

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

No branches or pull requests

3 participants