Skip to content

Commit

Permalink
fix: Fixed the logic for deleting duplicate TypeAlias. (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
Himenon authored Aug 13, 2021
1 parent 7ebcdf7 commit 7d48202
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 2 deletions.
1 change: 1 addition & 0 deletions scripts/testCodeGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
4 changes: 2 additions & 2 deletions src/internal/OpenApiTools/Walker/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
63 changes: 63 additions & 0 deletions test/__tests__/__snapshots__/typedef-with-template-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,69 @@ export class Client<RequestOption> {
"
`;
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<RequestOption> {
request: <T = SuccessResponses>(httpMethod: HttpMethod, url: string, headers: ObjectLike | any, requestBody: ObjectLike | any, queryParameters: QueryParameters | undefined, options?: RequestOption) => Promise<T>;
}
export class Client<RequestOption> {
private baseUrl: string;
constructor(private apiClient: ApiClient<RequestOption>, baseUrl: string) { this.baseUrl = baseUrl.replace(/\\\\/$/, \\"\\"); }
public async getHelloWorld(params: Params$getHelloWorld, option?: RequestOption): Promise<Response$getHelloWorld$Status$200[\\"application/json\\"]> {
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
Expand Down
5 changes: 5 additions & 0 deletions test/__tests__/typedef-with-template-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions test/api.v2.domain/components/schemas/Message.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type: string
enum:
- "hello"
- "world"
6 changes: 6 additions & 0 deletions test/api.v2.domain/components/schemas/QueryParams.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: string
enum:
- "a"
- "b"
- "c"

44 changes: 44 additions & 0 deletions test/api.v2.domain/index.yml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 7d48202

Please sign in to comment.