Skip to content

Commit 4f9914c

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

File tree

4 files changed

+213
-0
lines changed

4 files changed

+213
-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: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2986,3 +2986,109 @@ 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 Parameter$getBook {
3015+
/** Book ID */
3016+
id: string;
3017+
}
3018+
export interface Response$getBook$Status$200 {
3019+
\\"application/json\\": Schemas.Book;
3020+
}
3021+
export interface Parameter$getAuthor {
3022+
/** Author Id */
3023+
id: string;
3024+
}
3025+
export interface Response$getAuthor$Status$200 {
3026+
\\"application/json\\": Schemas.Book.properties.author;
3027+
}
3028+
export interface Parameter$getPublisher {
3029+
/** Publisher ID */
3030+
id: string;
3031+
}
3032+
export interface Response$getPublisher$Status$200 {
3033+
\\"application/json\\": Schemas.Publisher;
3034+
}
3035+
export type ResponseContentType$getBook = keyof Response$getBook$Status$200;
3036+
export interface Params$getBook {
3037+
parameter: Parameter$getBook;
3038+
}
3039+
export type ResponseContentType$getAuthor = keyof Response$getAuthor$Status$200;
3040+
export interface Params$getAuthor {
3041+
parameter: Parameter$getAuthor;
3042+
}
3043+
export type ResponseContentType$getPublisher = keyof Response$getPublisher$Status$200;
3044+
export interface Params$getPublisher {
3045+
parameter: Parameter$getPublisher;
3046+
}
3047+
export type HttpMethod = \\"GET\\" | \\"PUT\\" | \\"POST\\" | \\"DELETE\\" | \\"OPTIONS\\" | \\"HEAD\\" | \\"PATCH\\" | \\"TRACE\\";
3048+
export interface ObjectLike {
3049+
[key: string]: any;
3050+
}
3051+
export interface QueryParameter {
3052+
value: any;
3053+
style?: \\"form\\" | \\"spaceDelimited\\" | \\"pipeDelimited\\" | \\"deepObject\\";
3054+
explode: boolean;
3055+
}
3056+
export interface QueryParameters {
3057+
[key: string]: QueryParameter;
3058+
}
3059+
export type SuccessResponses = Response$getBook$Status$200 | Response$getAuthor$Status$200 | Response$getPublisher$Status$200;
3060+
export namespace ErrorResponse {
3061+
export type getBook = void;
3062+
export type getAuthor = void;
3063+
export type getPublisher = void;
3064+
}
3065+
export interface ApiClient<RequestOption> {
3066+
request: <T = SuccessResponses>(httpMethod: HttpMethod, url: string, headers: ObjectLike | any, requestBody: ObjectLike | any, queryParameters: QueryParameters | undefined, options?: RequestOption) => Promise<T>;
3067+
}
3068+
export class Client<RequestOption> {
3069+
private baseUrl: string;
3070+
constructor(private apiClient: ApiClient<RequestOption>, baseUrl: string) { this.baseUrl = baseUrl.replace(/\\\\/$/, \\"\\"); }
3071+
public async getBook(params: Params$getBook, option?: RequestOption): Promise<Response$getBook$Status$200[\\"application/json\\"]> {
3072+
const url = this.baseUrl + \`/get/book/\${params.parameter.id}\`;
3073+
const headers = {
3074+
Accept: \\"application/json\\"
3075+
};
3076+
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
3077+
}
3078+
public async getAuthor(params: Params$getAuthor, option?: RequestOption): Promise<Response$getAuthor$Status$200[\\"application/json\\"]> {
3079+
const url = this.baseUrl + \`/get/author/\${params.parameter.id}\`;
3080+
const headers = {
3081+
Accept: \\"application/json\\"
3082+
};
3083+
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
3084+
}
3085+
public async getPublisher(params: Params$getPublisher, option?: RequestOption): Promise<Response$getPublisher$Status$200[\\"application/json\\"]> {
3086+
const url = this.baseUrl + \`/get/publisher/\${params.parameter.id}\`;
3087+
const headers = {
3088+
Accept: \\"application/json\\"
3089+
};
3090+
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
3091+
}
3092+
}
3093+
"
3094+
`;

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: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
parameters:
45+
- name: id
46+
in: path
47+
required: true
48+
description: Book ID
49+
schema:
50+
type: string
51+
format: uuid
52+
get:
53+
operationId: getBook
54+
responses:
55+
200:
56+
description: Get Books
57+
content:
58+
application/json:
59+
schema:
60+
$ref: "#/components/schemas/Book"
61+
/get/author/{id}:
62+
parameters:
63+
- name: id
64+
in: path
65+
required: true
66+
description: Author Id
67+
schema:
68+
type: string
69+
format: uuid
70+
get:
71+
operationId: getAuthor
72+
responses:
73+
200:
74+
description: Get Author
75+
content:
76+
application/json:
77+
schema:
78+
$ref: "#/components/schemas/Book/properties/author"
79+
/get/publisher/{id}:
80+
parameters:
81+
- name: id
82+
in: path
83+
required: true
84+
description: Publisher ID
85+
schema:
86+
type: string
87+
format: uuid
88+
get:
89+
operationId: getPublisher
90+
responses:
91+
200:
92+
description: Get Publisher
93+
content:
94+
application/json:
95+
schema:
96+
$ref: "#/components/schemas/Publisher"

0 commit comments

Comments
 (0)