From 7d48202ca2d3e3416f27f5c995492d4e5e4a65e2 Mon Sep 17 00:00:00 2001 From: "K.Himeno" Date: Fri, 13 Aug 2021 15:02:49 +0900 Subject: [PATCH] fix: Fixed the logic for deleting duplicate TypeAlias. (#58) --- scripts/testCodeGen.ts | 1 + src/internal/OpenApiTools/Walker/Store.ts | 4 +- .../typedef-with-template-test.ts.snap | 63 +++++++++++++++++++ test/__tests__/typedef-with-template-test.ts | 5 ++ .../components/schemas/Message.yml | 4 ++ .../components/schemas/QueryParams.yml | 6 ++ test/api.v2.domain/index.yml | 44 +++++++++++++ 7 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 test/api.v2.domain/components/schemas/Message.yml create mode 100644 test/api.v2.domain/components/schemas/QueryParams.yml create mode 100644 test/api.v2.domain/index.yml diff --git a/scripts/testCodeGen.ts b/scripts/testCodeGen.ts index 0affc842..8cf8b1b5 100644 --- a/scripts/testCodeGen.ts +++ b/scripts/testCodeGen.ts @@ -105,6 +105,7 @@ const main = () => { generateTemplateCodeOnly("test/api.test.domain/index.yml", "test/code/template-only/sync-api.test.domain.ts", true, { sync: true }); generateTemplateCodeOnly("test/infer.domain/index.yml", "test/code/template-only/infer.domain.ts", false, { sync: true }); + generateTypedefWithTemplateCode("test/api.v2.domain/index.yml", "test/code/typedef-with-template/api.v2.domain.ts", false, { sync: false }); generateTypedefWithTemplateCode("test/api.test.domain/index.yml", "test/code/typedef-with-template/api.test.domain.ts", true, { sync: false, }); diff --git a/src/internal/OpenApiTools/Walker/Store.ts b/src/internal/OpenApiTools/Walker/Store.ts index 8c8c77c1..3e7cae26 100644 --- a/src/internal/OpenApiTools/Walker/Store.ts +++ b/src/internal/OpenApiTools/Walker/Store.ts @@ -83,8 +83,8 @@ class Store { if (this.hasStatement(targetPath, ["interface"])) { return; } - // もしTypeAlias同じスコープに登録されている場合、既存のTypeAliasを削除する - if (this.hasStatement(targetPath, ["typeAlias"])) { + // もしTypeAliasが同じスコープに登録されているかつ、interfaceが新しく追加しようとしている場合、既存のstatementを削除する + if (this.hasStatement(targetPath, ["typeAlias"]) && statement.kind === "interface") { this.operator.remove(targetPath, "typeAlias"); } this.operator.set(targetPath, Structure.createInstance(statement)); diff --git a/test/__tests__/__snapshots__/typedef-with-template-test.ts.snap b/test/__tests__/__snapshots__/typedef-with-template-test.ts.snap index e146f40e..31b8eee4 100644 --- a/test/__tests__/__snapshots__/typedef-with-template-test.ts.snap +++ b/test/__tests__/__snapshots__/typedef-with-template-test.ts.snap @@ -438,6 +438,69 @@ export class Client { " `; +exports[`Typedef with template api.v2.domain 1`] = ` +"// +// Generated by @himenon/openapi-typescript-code-generator +// +// OpenApi : 3.1.0 +// +// License : MIT +// + + +export namespace Schemas { + export type Message = \\"hello\\" | \\"world\\"; + export type QueryParams = \\"a\\" | \\"b\\" | \\"c\\"; +} +export interface Parameter$getHelloWorld { + /** query word */ + word: Schemas.QueryParams; +} +export interface Response$getHelloWorld$Status$200 { + \\"application/json\\": { + message?: Schemas.Message; + }; +} +export type ResponseContentType$getHelloWorld = keyof Response$getHelloWorld$Status$200; +export interface Params$getHelloWorld { + parameter: Parameter$getHelloWorld; +} +export type HttpMethod = \\"GET\\" | \\"PUT\\" | \\"POST\\" | \\"DELETE\\" | \\"OPTIONS\\" | \\"HEAD\\" | \\"PATCH\\" | \\"TRACE\\"; +export interface ObjectLike { + [key: string]: any; +} +export interface QueryParameter { + value: any; + style?: \\"form\\" | \\"spaceDelimited\\" | \\"pipeDelimited\\" | \\"deepObject\\"; + explode: boolean; +} +export interface QueryParameters { + [key: string]: QueryParameter; +} +export type SuccessResponses = Response$getHelloWorld$Status$200; +export namespace ErrorResponse { + export type getHelloWorld = void; +} +export interface ApiClient { + request: (httpMethod: HttpMethod, url: string, headers: ObjectLike | any, requestBody: ObjectLike | any, queryParameters: QueryParameters | undefined, options?: RequestOption) => Promise; +} +export class Client { + private baseUrl: string; + constructor(private apiClient: ApiClient, baseUrl: string) { this.baseUrl = baseUrl.replace(/\\\\/$/, \\"\\"); } + public async getHelloWorld(params: Params$getHelloWorld, option?: RequestOption): Promise { + const url = this.baseUrl + \`/hello/world\`; + const headers = { + Accept: \\"application/json\\" + }; + const queryParameters: QueryParameters = { + word: { value: params.parameter.word, explode: false } + }; + return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option); + } +} +" +`; + exports[`Typedef with template argo-rollout 1`] = ` "// // Generated by @himenon/openapi-typescript-code-generator diff --git a/test/__tests__/typedef-with-template-test.ts b/test/__tests__/typedef-with-template-test.ts index 8db97434..dda84844 100644 --- a/test/__tests__/typedef-with-template-test.ts +++ b/test/__tests__/typedef-with-template-test.ts @@ -9,6 +9,11 @@ describe("Typedef with template", () => { const text = Utils.replaceVersionInfo(generateCode); expect(text).toMatchSnapshot(); }); + test("api.v2.domain", () => { + const generateCode = fs.readFileSync(path.join(__dirname, "../code/typedef-with-template/api.v2.domain.ts"), { encoding: "utf-8" }); + const text = Utils.replaceVersionInfo(generateCode); + expect(text).toMatchSnapshot(); + }); test("async-api.test.domain", () => { const generateCode = fs.readFileSync(path.join(__dirname, "../code/typedef-with-template/sync-api.test.domain.ts"), { encoding: "utf-8" }); const text = Utils.replaceVersionInfo(generateCode); diff --git a/test/api.v2.domain/components/schemas/Message.yml b/test/api.v2.domain/components/schemas/Message.yml new file mode 100644 index 00000000..843e0d47 --- /dev/null +++ b/test/api.v2.domain/components/schemas/Message.yml @@ -0,0 +1,4 @@ +type: string +enum: + - "hello" + - "world" diff --git a/test/api.v2.domain/components/schemas/QueryParams.yml b/test/api.v2.domain/components/schemas/QueryParams.yml new file mode 100644 index 00000000..2b9a9179 --- /dev/null +++ b/test/api.v2.domain/components/schemas/QueryParams.yml @@ -0,0 +1,6 @@ +type: string +enum: + - "a" + - "b" + - "c" + diff --git a/test/api.v2.domain/index.yml b/test/api.v2.domain/index.yml new file mode 100644 index 00000000..86ed80b9 --- /dev/null +++ b/test/api.v2.domain/index.yml @@ -0,0 +1,44 @@ +openapi: 3.1.0 +info: + version: 1.0.0 + title: api.test.domain + description: Library test schema + license: + name: MIT + +servers: + - url: "http://dev.api.test.domain/" + description: Development Environment + - url: "https://api.test.domain/" + description: Production Environment + +tags: + - name: test + +components: + schemas: + Message: + $ref: "./components/schemas/Message.yml" + QueryParams: + $ref: "./components/schemas/QueryParams.yml" +paths: + /hello/world: + get: + operationId: getHelloWorld + parameters: + - in: query + name: word + description: query word + schema: + $ref: "#/components/schemas/QueryParams" + required: true + responses: + 200: + description: 成功 + content: + application/json: + schema: + type: object + properties: + message: + $ref: "#/components/schemas/Message"