Skip to content

fix: Fixed the logic for deleting duplicate TypeAlias #58

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

Merged
merged 1 commit into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"