Skip to content

Commit 7d48202

Browse files
authored
fix: Fixed the logic for deleting duplicate TypeAlias. (#58)
1 parent 7ebcdf7 commit 7d48202

File tree

7 files changed

+125
-2
lines changed

7 files changed

+125
-2
lines changed

scripts/testCodeGen.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ const main = () => {
105105
generateTemplateCodeOnly("test/api.test.domain/index.yml", "test/code/template-only/sync-api.test.domain.ts", true, { sync: true });
106106
generateTemplateCodeOnly("test/infer.domain/index.yml", "test/code/template-only/infer.domain.ts", false, { sync: true });
107107

108+
generateTypedefWithTemplateCode("test/api.v2.domain/index.yml", "test/code/typedef-with-template/api.v2.domain.ts", false, { sync: false });
108109
generateTypedefWithTemplateCode("test/api.test.domain/index.yml", "test/code/typedef-with-template/api.test.domain.ts", true, {
109110
sync: false,
110111
});

src/internal/OpenApiTools/Walker/Store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ class Store {
8383
if (this.hasStatement(targetPath, ["interface"])) {
8484
return;
8585
}
86-
// もしTypeAlias同じスコープに登録されている場合、既存のTypeAliasを削除する
87-
if (this.hasStatement(targetPath, ["typeAlias"])) {
86+
// もしTypeAliasが同じスコープに登録されているかつ、interfaceが新しく追加しようとしている場合、既存のstatementを削除する
87+
if (this.hasStatement(targetPath, ["typeAlias"]) && statement.kind === "interface") {
8888
this.operator.remove(targetPath, "typeAlias");
8989
}
9090
this.operator.set(targetPath, Structure.createInstance(statement));

test/__tests__/__snapshots__/typedef-with-template-test.ts.snap

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,69 @@ export class Client<RequestOption> {
438438
"
439439
`;
440440
441+
exports[`Typedef with template api.v2.domain 1`] = `
442+
"//
443+
// Generated by @himenon/openapi-typescript-code-generator
444+
//
445+
// OpenApi : 3.1.0
446+
//
447+
// License : MIT
448+
//
449+
450+
451+
export namespace Schemas {
452+
export type Message = \\"hello\\" | \\"world\\";
453+
export type QueryParams = \\"a\\" | \\"b\\" | \\"c\\";
454+
}
455+
export interface Parameter$getHelloWorld {
456+
/** query word */
457+
word: Schemas.QueryParams;
458+
}
459+
export interface Response$getHelloWorld$Status$200 {
460+
\\"application/json\\": {
461+
message?: Schemas.Message;
462+
};
463+
}
464+
export type ResponseContentType$getHelloWorld = keyof Response$getHelloWorld$Status$200;
465+
export interface Params$getHelloWorld {
466+
parameter: Parameter$getHelloWorld;
467+
}
468+
export type HttpMethod = \\"GET\\" | \\"PUT\\" | \\"POST\\" | \\"DELETE\\" | \\"OPTIONS\\" | \\"HEAD\\" | \\"PATCH\\" | \\"TRACE\\";
469+
export interface ObjectLike {
470+
[key: string]: any;
471+
}
472+
export interface QueryParameter {
473+
value: any;
474+
style?: \\"form\\" | \\"spaceDelimited\\" | \\"pipeDelimited\\" | \\"deepObject\\";
475+
explode: boolean;
476+
}
477+
export interface QueryParameters {
478+
[key: string]: QueryParameter;
479+
}
480+
export type SuccessResponses = Response$getHelloWorld$Status$200;
481+
export namespace ErrorResponse {
482+
export type getHelloWorld = void;
483+
}
484+
export interface ApiClient<RequestOption> {
485+
request: <T = SuccessResponses>(httpMethod: HttpMethod, url: string, headers: ObjectLike | any, requestBody: ObjectLike | any, queryParameters: QueryParameters | undefined, options?: RequestOption) => Promise<T>;
486+
}
487+
export class Client<RequestOption> {
488+
private baseUrl: string;
489+
constructor(private apiClient: ApiClient<RequestOption>, baseUrl: string) { this.baseUrl = baseUrl.replace(/\\\\/$/, \\"\\"); }
490+
public async getHelloWorld(params: Params$getHelloWorld, option?: RequestOption): Promise<Response$getHelloWorld$Status$200[\\"application/json\\"]> {
491+
const url = this.baseUrl + \`/hello/world\`;
492+
const headers = {
493+
Accept: \\"application/json\\"
494+
};
495+
const queryParameters: QueryParameters = {
496+
word: { value: params.parameter.word, explode: false }
497+
};
498+
return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option);
499+
}
500+
}
501+
"
502+
`;
503+
441504
exports[`Typedef with template argo-rollout 1`] = `
442505
"//
443506
// Generated by @himenon/openapi-typescript-code-generator

test/__tests__/typedef-with-template-test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ describe("Typedef with template", () => {
99
const text = Utils.replaceVersionInfo(generateCode);
1010
expect(text).toMatchSnapshot();
1111
});
12+
test("api.v2.domain", () => {
13+
const generateCode = fs.readFileSync(path.join(__dirname, "../code/typedef-with-template/api.v2.domain.ts"), { encoding: "utf-8" });
14+
const text = Utils.replaceVersionInfo(generateCode);
15+
expect(text).toMatchSnapshot();
16+
});
1217
test("async-api.test.domain", () => {
1318
const generateCode = fs.readFileSync(path.join(__dirname, "../code/typedef-with-template/sync-api.test.domain.ts"), { encoding: "utf-8" });
1419
const text = Utils.replaceVersionInfo(generateCode);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type: string
2+
enum:
3+
- "hello"
4+
- "world"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type: string
2+
enum:
3+
- "a"
4+
- "b"
5+
- "c"
6+

test/api.v2.domain/index.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
openapi: 3.1.0
2+
info:
3+
version: 1.0.0
4+
title: api.test.domain
5+
description: Library test schema
6+
license:
7+
name: MIT
8+
9+
servers:
10+
- url: "http://dev.api.test.domain/"
11+
description: Development Environment
12+
- url: "https://api.test.domain/"
13+
description: Production Environment
14+
15+
tags:
16+
- name: test
17+
18+
components:
19+
schemas:
20+
Message:
21+
$ref: "./components/schemas/Message.yml"
22+
QueryParams:
23+
$ref: "./components/schemas/QueryParams.yml"
24+
paths:
25+
/hello/world:
26+
get:
27+
operationId: getHelloWorld
28+
parameters:
29+
- in: query
30+
name: word
31+
description: query word
32+
schema:
33+
$ref: "#/components/schemas/QueryParams"
34+
required: true
35+
responses:
36+
200:
37+
description: 成功
38+
content:
39+
application/json:
40+
schema:
41+
type: object
42+
properties:
43+
message:
44+
$ref: "#/components/schemas/Message"

0 commit comments

Comments
 (0)