From 26469bc35392ed7fb572eb07901b29398b3efa15 Mon Sep 17 00:00:00 2001 From: phk422 <1769476785@qq.com> Date: Wed, 26 Jun 2024 15:01:11 +0800 Subject: [PATCH 1/3] fix: Ignore path item components in paths --- .../src/transform/paths-object.ts | 1 + .../test/fixtures/path-item-components.yaml | 56 +++++++++++++ packages/openapi-typescript/test/yaml.test.ts | 82 +++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 packages/openapi-typescript/test/fixtures/path-item-components.yaml diff --git a/packages/openapi-typescript/src/transform/paths-object.ts b/packages/openapi-typescript/src/transform/paths-object.ts index 5718a79f..5a2367a9 100644 --- a/packages/openapi-typescript/src/transform/paths-object.ts +++ b/packages/openapi-typescript/src/transform/paths-object.ts @@ -35,6 +35,7 @@ export default function transformPathsObject(pathsObject: PathsObject, ctx: Glob /* type */ oapiRef(pathItemObject.$ref), ); addJSDocComment(pathItemObject, property); + type.push(property); } else { const pathItemType = transformPathItemObject(pathItemObject, { path: createRef(["paths", url]), diff --git a/packages/openapi-typescript/test/fixtures/path-item-components.yaml b/packages/openapi-typescript/test/fixtures/path-item-components.yaml new file mode 100644 index 00000000..9fcae177 --- /dev/null +++ b/packages/openapi-typescript/test/fixtures/path-item-components.yaml @@ -0,0 +1,56 @@ +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 \ No newline at end of file diff --git a/packages/openapi-typescript/test/yaml.test.ts b/packages/openapi-typescript/test/yaml.test.ts index d41d6205..35b684c9 100644 --- a/packages/openapi-typescript/test/yaml.test.ts +++ b/packages/openapi-typescript/test/yaml.test.ts @@ -85,4 +85,86 @@ export interface operations { }; }`); }); + + test("not ignore path item components in paths", async() =>{ + const result = await execa(cmd, ["./test/fixtures/path-item-components.yaml"], { + cwd, + }); + expect(result.stdout).toBe(`/** + * This file was auto-generated by openapi-typescript. + * Do not make direct changes to the file. + */ + +export interface paths { + "/users": components["pathItems"]["users"]; +} +export type webhooks = Record; +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; +export interface operations { + createUser: { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + /** @description The user to create */ + requestBody?: { + content: { + "application/json": { + name?: string; + }; + }; + }; + responses: { + /** @description The user was created successfully */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": { + id?: string; + name?: string; + }; + }; + }; + /** @description Bad request */ + 400: { + headers: { + [name: string]: unknown; + }; + content?: never; + }; + }; + }; +}`) + + }) }); From e2c862c64fbc242f32ee013d68035997404ebc26 Mon Sep 17 00:00:00 2001 From: phk422 <1769476785@qq.com> Date: Wed, 26 Jun 2024 15:15:14 +0800 Subject: [PATCH 2/3] lint code --- packages/openapi-typescript/test/yaml.test.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/openapi-typescript/test/yaml.test.ts b/packages/openapi-typescript/test/yaml.test.ts index 35b684c9..209f3636 100644 --- a/packages/openapi-typescript/test/yaml.test.ts +++ b/packages/openapi-typescript/test/yaml.test.ts @@ -86,10 +86,14 @@ export interface operations { }`); }); - test("not ignore path item components in paths", async() =>{ - const result = await execa(cmd, ["./test/fixtures/path-item-components.yaml"], { - cwd, - }); + test("not ignore path item components in paths", async () => { + const result = await execa( + cmd, + ["./test/fixtures/path-item-components.yaml"], + { + cwd, + } + ); expect(result.stdout).toBe(`/** * This file was auto-generated by openapi-typescript. * Do not make direct changes to the file. @@ -164,7 +168,6 @@ export interface operations { }; }; }; -}`) - - }) +}`); + }); }); From bb51ac0da13db75de3041e2ecec7c591ce8af098 Mon Sep 17 00:00:00 2001 From: phk422 <1769476785@qq.com> Date: Wed, 26 Jun 2024 15:18:12 +0800 Subject: [PATCH 3/3] lint code --- packages/openapi-typescript/test/yaml.test.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/openapi-typescript/test/yaml.test.ts b/packages/openapi-typescript/test/yaml.test.ts index 209f3636..c3f27494 100644 --- a/packages/openapi-typescript/test/yaml.test.ts +++ b/packages/openapi-typescript/test/yaml.test.ts @@ -87,13 +87,9 @@ export interface operations { }); test("not ignore path item components in paths", async () => { - const result = await execa( - cmd, - ["./test/fixtures/path-item-components.yaml"], - { - cwd, - } - ); + const result = await execa(cmd, ["./test/fixtures/path-item-components.yaml"], { + cwd, + }); expect(result.stdout).toBe(`/** * This file was auto-generated by openapi-typescript. * Do not make direct changes to the file.