Skip to content

Commit 5d02c48

Browse files
committed
test: add property access test via reference object
1 parent 2361e5b commit 5d02c48

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed

scripts/testCodeGen.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,16 @@ const main = () => {
118118
sync: false,
119119
});
120120

121+
generateTypedefWithTemplateCode("test/ref.access/index.yml", "test/code/typedef-with-template/ref-access.ts", false, {
122+
sync: false,
123+
});
124+
121125
generateSplitCode("test/api.test.domain/index.yml", "test/code/split");
122126

123127
generateParameter("test/api.test.domain/index.yml", "test/code/parameter/api.test.domain.json");
124128
generateParameter("test/infer.domain/index.yml", "test/code/parameter/infer.domain.json");
129+
130+
125131
};
126132

127133
main();

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2986,3 +2986,88 @@ export class Client<RequestOption> {
29862986
}
29872987
"
29882988
`;
2989+
2990+
exports[`Typedef with template ref-access 1`] = `
2991+
"//
2992+
// Generated by @himenon/openapi-typescript-code-generator
2993+
//
2994+
// OpenApi : 3.1.0
2995+
//
2996+
// License : MIT
2997+
//
2998+
2999+
3000+
export namespace Schemas {
3001+
export interface Book {
3002+
author?: {
3003+
name?: string;
3004+
age?: string;
3005+
};
3006+
publisher?: {
3007+
name?: any;
3008+
address?: string;
3009+
};
3010+
}
3011+
export type Author = Schemas.Book.properties.author;
3012+
export type Publisher = Schemas.Book.properties.publisher;
3013+
}
3014+
export interface Response$getBook$Status$200 {
3015+
\\"application/json\\": Schemas.Book;
3016+
}
3017+
export interface Response$getAuthor$Status$200 {
3018+
\\"application/json\\": Schemas.Book.properties.author;
3019+
}
3020+
export interface Response$getPublisher$Status$200 {
3021+
\\"application/json\\": Schemas.Publisher;
3022+
}
3023+
export type ResponseContentType$getBook = keyof Response$getBook$Status$200;
3024+
export type ResponseContentType$getAuthor = keyof Response$getAuthor$Status$200;
3025+
export type ResponseContentType$getPublisher = keyof Response$getPublisher$Status$200;
3026+
export type HttpMethod = \\"GET\\" | \\"PUT\\" | \\"POST\\" | \\"DELETE\\" | \\"OPTIONS\\" | \\"HEAD\\" | \\"PATCH\\" | \\"TRACE\\";
3027+
export interface ObjectLike {
3028+
[key: string]: any;
3029+
}
3030+
export interface QueryParameter {
3031+
value: any;
3032+
style?: \\"form\\" | \\"spaceDelimited\\" | \\"pipeDelimited\\" | \\"deepObject\\";
3033+
explode: boolean;
3034+
}
3035+
export interface QueryParameters {
3036+
[key: string]: QueryParameter;
3037+
}
3038+
export type SuccessResponses = Response$getBook$Status$200 | Response$getAuthor$Status$200 | Response$getPublisher$Status$200;
3039+
export namespace ErrorResponse {
3040+
export type getBook = void;
3041+
export type getAuthor = void;
3042+
export type getPublisher = void;
3043+
}
3044+
export interface ApiClient<RequestOption> {
3045+
request: <T = SuccessResponses>(httpMethod: HttpMethod, url: string, headers: ObjectLike | any, requestBody: ObjectLike | any, queryParameters: QueryParameters | undefined, options?: RequestOption) => Promise<T>;
3046+
}
3047+
export class Client<RequestOption> {
3048+
private baseUrl: string;
3049+
constructor(private apiClient: ApiClient<RequestOption>, baseUrl: string) { this.baseUrl = baseUrl.replace(/\\\\/$/, \\"\\"); }
3050+
public async getBook(option?: RequestOption): Promise<Response$getBook$Status$200[\\"application/json\\"]> {
3051+
const url = this.baseUrl + \`/get/book/{id}\`;
3052+
const headers = {
3053+
Accept: \\"application/json\\"
3054+
};
3055+
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
3056+
}
3057+
public async getAuthor(option?: RequestOption): Promise<Response$getAuthor$Status$200[\\"application/json\\"]> {
3058+
const url = this.baseUrl + \`/get/author/{id}\`;
3059+
const headers = {
3060+
Accept: \\"application/json\\"
3061+
};
3062+
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
3063+
}
3064+
public async getPublisher(option?: RequestOption): Promise<Response$getPublisher$Status$200[\\"application/json\\"]> {
3065+
const url = this.baseUrl + \`/get/publisher/{id}\`;
3066+
const headers = {
3067+
Accept: \\"application/json\\"
3068+
};
3069+
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
3070+
}
3071+
}
3072+
"
3073+
`;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ describe("Typedef with template", () => {
2929
const text = Utils.replaceVersionInfo(generateCode);
3030
expect(text).toMatchSnapshot();
3131
});
32+
test("ref-access", () => {
33+
const generateCode = fs.readFileSync(path.join(__dirname, "../code/typedef-with-template/ref-access.ts"), { encoding: "utf-8" });
34+
const text = Utils.replaceVersionInfo(generateCode);
35+
expect(text).toMatchSnapshot();
36+
});
3237
});

test/ref.access/index.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
Book:
21+
type: object
22+
properties:
23+
author:
24+
type: object
25+
properties:
26+
name:
27+
type: string
28+
age:
29+
type: string
30+
publisher:
31+
type: object
32+
properties:
33+
name:
34+
type: String
35+
address:
36+
type: string
37+
Author:
38+
$ref: "#/components/schemas/Book/properties/author"
39+
Publisher:
40+
$ref: "#/components/schemas/Book/properties/publisher"
41+
42+
paths:
43+
/get/book/{id}:
44+
get:
45+
operationId: getBook
46+
responses:
47+
200:
48+
description: Get Books
49+
content:
50+
application/json:
51+
schema:
52+
$ref: "#/components/schemas/Book"
53+
/get/author/{id}:
54+
get:
55+
operationId: getAuthor
56+
responses:
57+
200:
58+
description: Get Author
59+
content:
60+
application/json:
61+
schema:
62+
$ref: "#/components/schemas/Book/properties/author"
63+
/get/publisher/{id}:
64+
get:
65+
operationId: getPublisher
66+
responses:
67+
200:
68+
description: Get Publisher
69+
content:
70+
application/json:
71+
schema:
72+
$ref: "#/components/schemas/Publisher"

0 commit comments

Comments
 (0)