Skip to content

Commit c4b47c8

Browse files
authored
fix: separated strings are not restored correctly (#112)
1 parent 8c5abe6 commit c4b47c8

File tree

6 files changed

+89
-5
lines changed

6 files changed

+89
-5
lines changed

src/code-templates/_shared/MethodBody/PathParameter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ export const generateUrlTemplateExpression = (
7171
Object.keys(patternMap).forEach(pathParameterName => {
7272
if (new RegExp(pathParameterName).test(replacedText)) {
7373
const { text, escaped } = escapeText(patternMap[pathParameterName]);
74-
const variableDeclaraText = escaped ? `params.parameter[${text}]` : `params.parameter.${text}`;
75-
replacedText = replacedText.replace(new RegExp(pathParameterName, "g"), variableDeclaraText);
74+
const variableDeclareText = escaped ? `params.parameter[${text}]` : `params.parameter.${text}`;
75+
replacedText = replacedText.replace(new RegExp(pathParameterName, "g"), variableDeclareText);
7676
}
7777
});
7878
return replacedText === text ? undefined : replacedText;
@@ -111,7 +111,7 @@ export const generateUrlTemplateExpression = (
111111
} else {
112112
urlTemplate.push({
113113
type: "string",
114-
value: value.startsWith("/") ? value : "/" + value,
114+
value: value,
115115
});
116116
}
117117
}

src/code-templates/_shared/MethodBody/__tests__/PathParameter-test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ describe("PathParameter Test", () => {
5353
expect(generate("/a/{b}/c/", [{ in: "path", name: "b", required: true }])).toBe("`/a/${params.parameter.b}/c/`;" + EOL);
5454
expect(generate("/a/b/{c}", [{ in: "path", name: "c", required: true }])).toBe("`/a/b/${params.parameter.c}`;" + EOL);
5555
expect(generate("/a/b/{c}/", [{ in: "path", name: "c", required: true }])).toBe("`/a/b/${params.parameter.c}/`;" + EOL);
56+
expect(generate("/a/b/{c}.json", [{ in: "path", name: "c", required: true }])).toBe("`/a/b/${params.parameter.c}.json`;" + EOL);
57+
expect(generate("/{a}.json/{a}.json/{a}.json", [{ in: "path", name: "a", required: true }])).toBe("`/${params.parameter.a}.json/${params.parameter.a}.json/${params.parameter.a}.json`;" + EOL);
58+
expect(generate("/.json.{a}.json/{a}.json.{a}", [{ in: "path", name: "a", required: true }])).toBe("`/.json.${params.parameter.a}.json/${params.parameter.a}.json.${params.parameter.a}`;" + EOL);
5659

5760
expect(
5861
generate("/{a}/{b}", [

src/code-templates/_shared/__tests__/utils.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,9 @@ describe("Utils", () => {
8383
},
8484
]);
8585
});
86+
87+
test("multiSplitStringToArray", () => {
88+
expect(Utils.multiSplitStringToArray("/{a}/b/{a}/c{a}/", ["{a}"])).toStrictEqual(["/", "{a}", "/b/", "{a}", "/c", "{a}", "/"]);
89+
expect(Utils.multiSplitStringToArray("/a/b/{c}.json", ["{c}"])).toStrictEqual(["/a/b/", "{c}", ".json"]);
90+
})
8691
});

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,15 @@ export interface Response$createPublisherV2$Status$200 {
13491349
status?: string;
13501350
};
13511351
}
1352+
export interface Parameter$getBookById {
1353+
/** Author ID */
1354+
authorId: string;
1355+
/** Book ID */
1356+
bookId: string;
1357+
}
1358+
export interface Response$getBookById$Status$200 {
1359+
"application/json": Schemas.Book;
1360+
}
13521361
export type ResponseContentType$getBook = keyof Response$getBook$Status$200;
13531362
export interface Params$getBook {
13541363
parameter: Parameter$getBook;
@@ -1378,6 +1387,10 @@ export interface Params$createPublisherV2<T extends RequestContentType$createPub
13781387
};
13791388
requestBody: RequestBody$createPublisherV2[T];
13801389
}
1390+
export type ResponseContentType$getBookById = keyof Response$getBookById$Status$200;
1391+
export interface Params$getBookById {
1392+
parameter: Parameter$getBookById;
1393+
}
13811394
export type HttpMethod = "GET" | "PUT" | "POST" | "DELETE" | "OPTIONS" | "HEAD" | "PATCH" | "TRACE";
13821395
export interface ObjectLike {
13831396
[key: string]: any;
@@ -1390,14 +1403,15 @@ export interface QueryParameter {
13901403
export interface QueryParameters {
13911404
[key: string]: QueryParameter;
13921405
}
1393-
export type SuccessResponses = Response$getBook$Status$200 | Response$getDescription$Status$200 | Response$getAuthor$Status$200 | Response$getPublisher$Status$200 | Response$createPublisher$Status$200 | Response$createPublisherV2$Status$200;
1406+
export type SuccessResponses = Response$getBook$Status$200 | Response$getDescription$Status$200 | Response$getAuthor$Status$200 | Response$getPublisher$Status$200 | Response$createPublisher$Status$200 | Response$createPublisherV2$Status$200 | Response$getBookById$Status$200;
13941407
export namespace ErrorResponse {
13951408
export type getBook = void;
13961409
export type getDescription = void;
13971410
export type getAuthor = void;
13981411
export type getPublisher = void;
13991412
export type createPublisher = void;
14001413
export type createPublisherV2 = void;
1414+
export type getBookById = void;
14011415
}
14021416
export interface Encoding {
14031417
readonly contentType?: string;
@@ -1514,6 +1528,17 @@ export class Client<RequestOption> {
15141528
requestBodyEncoding: requestEncodings[params.headers["Content-Type"]]
15151529
}, option);
15161530
}
1531+
public async getBookById(params: Params$getBookById, option?: RequestOption): Promise<Response$getBookById$Status$200["application/json"]> {
1532+
const url = this.baseUrl + \`/author/author-\${params.parameter.authorId}.a.\${params.parameter.bookId}.b/book/\${params.parameter.bookId}.json\`;
1533+
const headers = {
1534+
Accept: "application/json"
1535+
};
1536+
return this.apiClient.request({
1537+
httpMethod: "GET",
1538+
url,
1539+
headers
1540+
}, option);
1541+
}
15171542
}
15181543
"
15191544
`;

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,15 @@ export interface Response$createPublisherV2$Status$200 {
13601360
status?: string;
13611361
};
13621362
}
1363+
export interface Parameter$getBookById {
1364+
/** Author ID */
1365+
authorId: string;
1366+
/** Book ID */
1367+
bookId: string;
1368+
}
1369+
export interface Response$getBookById$Status$200 {
1370+
"application/json": Schemas.Book;
1371+
}
13631372
export type ResponseContentType$getBook = keyof Response$getBook$Status$200;
13641373
export interface Params$getBook {
13651374
parameter: Parameter$getBook;
@@ -1389,6 +1398,10 @@ export interface Params$createPublisherV2<T extends RequestContentType$createPub
13891398
};
13901399
requestBody: RequestBody$createPublisherV2[T];
13911400
}
1401+
export type ResponseContentType$getBookById = keyof Response$getBookById$Status$200;
1402+
export interface Params$getBookById {
1403+
parameter: Parameter$getBookById;
1404+
}
13921405
export type HttpMethod = "GET" | "PUT" | "POST" | "DELETE" | "OPTIONS" | "HEAD" | "PATCH" | "TRACE";
13931406
export interface ObjectLike {
13941407
[key: string]: any;
@@ -1401,14 +1414,15 @@ export interface QueryParameter {
14011414
export interface QueryParameters {
14021415
[key: string]: QueryParameter;
14031416
}
1404-
export type SuccessResponses = Response$getBook$Status$200 | Response$getDescription$Status$200 | Response$getAuthor$Status$200 | Response$getPublisher$Status$200 | Response$createPublisher$Status$200 | Response$createPublisherV2$Status$200;
1417+
export type SuccessResponses = Response$getBook$Status$200 | Response$getDescription$Status$200 | Response$getAuthor$Status$200 | Response$getPublisher$Status$200 | Response$createPublisher$Status$200 | Response$createPublisherV2$Status$200 | Response$getBookById$Status$200;
14051418
export namespace ErrorResponse {
14061419
export type getBook = void;
14071420
export type getDescription = void;
14081421
export type getAuthor = void;
14091422
export type getPublisher = void;
14101423
export type createPublisher = void;
14111424
export type createPublisherV2 = void;
1425+
export type getBookById = void;
14121426
}
14131427
export interface Encoding {
14141428
readonly contentType?: string;
@@ -1524,6 +1538,17 @@ export const createClient = <RequestOption>(apiClient: ApiClient<RequestOption>,
15241538
requestBody: params.requestBody,
15251539
requestBodyEncoding: requestEncodings[params.headers["Content-Type"]]
15261540
}, option);
1541+
},
1542+
getBookById: (params: Params$getBookById, option?: RequestOption): Promise<Response$getBookById$Status$200["application/json"]> => {
1543+
const url = _baseUrl + \`/author/author-\${params.parameter.authorId}.a.\${params.parameter.bookId}.b/book/\${params.parameter.bookId}.json\`;
1544+
const headers = {
1545+
Accept: "application/json"
1546+
};
1547+
return apiClient.request({
1548+
httpMethod: "GET",
1549+
url,
1550+
headers
1551+
}, option);
15271552
}
15281553
};
15291554
};

test/ref.access/index.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,29 @@ paths:
188188
properties:
189189
status:
190190
type: string
191+
192+
/author/author-{authorId}.a.{bookId}.b/book/{bookId}.json:
193+
parameters:
194+
- name: authorId
195+
in: path
196+
required: true
197+
description: Author ID
198+
schema:
199+
type: string
200+
format: uuid
201+
- name: bookId
202+
in: path
203+
required: true
204+
description: Book ID
205+
schema:
206+
type: string
207+
format: uuid
208+
get:
209+
operationId: getBookById
210+
responses:
211+
200:
212+
description: Get Books
213+
content:
214+
application/json:
215+
schema:
216+
$ref: "#/components/schemas/Book"

0 commit comments

Comments
 (0)