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

Path item components are ignored in paths #1725

Open
1 of 2 tasks
diego-aquino opened this issue Jun 24, 2024 · 1 comment · May be fixed by #1734
Open
1 of 2 tasks

Path item components are ignored in paths #1725

diego-aquino opened this issue Jun 24, 2024 · 1 comment · May be fixed by #1734
Labels
bug Something isn't working good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project openapi-ts Relevant to the openapi-typescript library PRs welcome PRs are welcome to solve this issue!

Comments

@diego-aquino
Copy link

Description

Hey! First of all, this lib has been great so far! However, it appears that path item components are being ignored in the type paths. If only path item components are used, paths is generated empty:

export type paths = Record<string, never>;

If I add one or more non-component path items, paths is generated with those, but component path items are still not included.

[No error is shown]

Name Version
openapi-typescript 7.0.0
Node.js 20.15.0
OS + version Linux Ubuntu + 22.04

Note

A bit of context

I'm the creator of Zimic, an HTTP mocking library with extensive support to TypeScript. As part of our effort to provide type safety, I am integrating openapi-typescript with Zimic as part of zimicjs/zimic#66. The idea is to generate the raw types using openapi-typescript and post-process them to follow Zimic's structure.

Reproduction

  1. Create a file and paste the following schema, which uses path item components. This spec passes the Redocly validator and is valid according to editor.swagger.io.

    schema.yaml
    openapi: 3.1.0
    
    info:
      title: Path Items
      version: 1.0.0
      license:
        name: MIT
        url: https://opensource.org/licenses/MIT
    
    servers:
      - url: https://my-example-server
    
    paths:
      /users:
        $ref: '#/components/pathItems/users'
    
    security:
      - bearerAuth: []
    
    components:
      pathItems:
        users:
          post:
            summary: Create user
            operationId: createUser
    
            requestBody:
              description: The user to create
              content:
                application/json:
                  schema:
                    type: object
                    properties:
                      name:
                        type: string
    
            responses:
              200:
                description: The user was created successfully
                content:
                  application/json:
                    schema:
                      type: object
                      properties:
                        id:
                          type: string
                        name:
                          type: string
              400:
                description: Bad request
    
      securitySchemes:
        bearerAuth:
          type: http
          scheme: bearer
          bearerFormat: JWT
  2. Generate the types:

    npx openapi-typescript schema.yaml -o schema.ts

Current result

paths ignores the component:

schema.ts
export type paths = Record<string, never>;
export type webhooks = Record<string, never>;
export interface components {
  schemas: never;
  responses: never;
  parameters: never;
  requestBodies: never;
  headers: never;
  pathItems: {
    users: {
      parameters: {
        query?: never;
        header?: never;
        path?: never;
        cookie?: never;
      };
      get?: never;
      put?: never;
      /** Create user */
      post: operations['createUser'];
      delete?: never;
      options?: never;
      head?: never;
      patch?: never;
      trace?: never;
    };
  };
}
export type $defs = Record<string, never>;
export interface operations {
  createUser: {
    parameters: {
      query?: never;
      header?: never;
      path?: never;
      cookie?: never;
    };
    /** The user to create */
    requestBody?: {
      content: {
        'application/json': {
          name?: string;
        };
      };
    };
    responses: {
      /** The user was created successfully */
      200: {
        headers: {
          [name: string]: unknown;
        };
        content: {
          'application/json': {
            id?: string;
            name?: string;
          };
        };
      };
      /** Bad request */
      400: {
        headers: {
          [name: string]: unknown;
        };
        content?: never;
      };
    };
  };
}

Expected result

paths should contain a reference to the component:

schema.ts
export interface paths {
  '/users': components['pathItems']['users'];
}
export type webhooks = Record<string, never>;
export interface components {
  schemas: never;
  responses: never;
  parameters: never;
  requestBodies: never;
  headers: never;
  pathItems: {
    users: {
      parameters: {
        query?: never;
        header?: never;
        path?: never;
        cookie?: never;
      };
      get?: never;
      put?: never;
      /** Create user */
      post: operations['createUser'];
      delete?: never;
      options?: never;
      head?: never;
      patch?: never;
      trace?: never;
    };
  };
}
export type $defs = Record<string, never>;
export interface operations {
  createUser: {
    parameters: {
      query?: never;
      header?: never;
      path?: never;
      cookie?: never;
    };
    /** The user to create */
    requestBody?: {
      content: {
        'application/json': {
          name?: string;
        };
      };
    };
    responses: {
      /** The user was created successfully */
      200: {
        headers: {
          [name: string]: unknown;
        };
        content: {
          'application/json': {
            id?: string;
            name?: string;
          };
        };
      };
      /** Bad request */
      400: {
        headers: {
          [name: string]: unknown;
        };
        content?: never;
      };
    };
  };
}

Let me know if this is an expected behavior or there's any problem with the schema I'm using.

Checklist

@diego-aquino diego-aquino added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Jun 24, 2024
@drwpow drwpow added good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project PRs welcome PRs are welcome to solve this issue! labels Jun 24, 2024
@drwpow
Copy link
Contributor

drwpow commented Jun 24, 2024

I’m surprised it took this long to catch this issue; this may have been present for a long time! We’re not testing for this. Would welcome a PR fixing this from someone (strong recommendation to add a failing test first since this issue has such a good repeatable example).

@phk422 phk422 linked a pull request Jun 26, 2024 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project openapi-ts Relevant to the openapi-typescript library PRs welcome PRs are welcome to solve this issue!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants