diff --git a/src/code-templates/_shared/MethodBody/PathParameter.ts b/src/code-templates/_shared/MethodBody/PathParameter.ts index 0a7b041a..6b6cd0f8 100644 --- a/src/code-templates/_shared/MethodBody/PathParameter.ts +++ b/src/code-templates/_shared/MethodBody/PathParameter.ts @@ -71,8 +71,8 @@ export const generateUrlTemplateExpression = ( Object.keys(patternMap).forEach(pathParameterName => { if (new RegExp(pathParameterName).test(replacedText)) { const { text, escaped } = escapeText(patternMap[pathParameterName]); - const variableDeclaraText = escaped ? `params.parameter[${text}]` : `params.parameter.${text}`; - replacedText = replacedText.replace(new RegExp(pathParameterName, "g"), variableDeclaraText); + const variableDeclareText = escaped ? `params.parameter[${text}]` : `params.parameter.${text}`; + replacedText = replacedText.replace(new RegExp(pathParameterName, "g"), variableDeclareText); } }); return replacedText === text ? undefined : replacedText; @@ -111,7 +111,7 @@ export const generateUrlTemplateExpression = ( } else { urlTemplate.push({ type: "string", - value: value.startsWith("/") ? value : "/" + value, + value: value, }); } } diff --git a/src/code-templates/_shared/MethodBody/__tests__/PathParameter-test.ts b/src/code-templates/_shared/MethodBody/__tests__/PathParameter-test.ts index d313a7d0..40ee763e 100644 --- a/src/code-templates/_shared/MethodBody/__tests__/PathParameter-test.ts +++ b/src/code-templates/_shared/MethodBody/__tests__/PathParameter-test.ts @@ -53,6 +53,9 @@ describe("PathParameter Test", () => { expect(generate("/a/{b}/c/", [{ in: "path", name: "b", required: true }])).toBe("`/a/${params.parameter.b}/c/`;" + EOL); expect(generate("/a/b/{c}", [{ in: "path", name: "c", required: true }])).toBe("`/a/b/${params.parameter.c}`;" + EOL); expect(generate("/a/b/{c}/", [{ in: "path", name: "c", required: true }])).toBe("`/a/b/${params.parameter.c}/`;" + EOL); + expect(generate("/a/b/{c}.json", [{ in: "path", name: "c", required: true }])).toBe("`/a/b/${params.parameter.c}.json`;" + EOL); + 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); + 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); expect( generate("/{a}/{b}", [ diff --git a/src/code-templates/_shared/__tests__/utils.test.ts b/src/code-templates/_shared/__tests__/utils.test.ts index df343ae5..1f635c76 100644 --- a/src/code-templates/_shared/__tests__/utils.test.ts +++ b/src/code-templates/_shared/__tests__/utils.test.ts @@ -83,4 +83,9 @@ describe("Utils", () => { }, ]); }); + + test("multiSplitStringToArray", () => { + expect(Utils.multiSplitStringToArray("/{a}/b/{a}/c{a}/", ["{a}"])).toStrictEqual(["/", "{a}", "/b/", "{a}", "/c", "{a}", "/"]); + expect(Utils.multiSplitStringToArray("/a/b/{c}.json", ["{c}"])).toStrictEqual(["/a/b/", "{c}", ".json"]); + }) }); diff --git a/test/__tests__/class/__snapshots__/typedef-with-template-test.ts.snap b/test/__tests__/class/__snapshots__/typedef-with-template-test.ts.snap index b4d53b67..34a12452 100644 --- a/test/__tests__/class/__snapshots__/typedef-with-template-test.ts.snap +++ b/test/__tests__/class/__snapshots__/typedef-with-template-test.ts.snap @@ -1349,6 +1349,15 @@ export interface Response$createPublisherV2$Status$200 { status?: string; }; } +export interface Parameter$getBookById { + /** Author ID */ + authorId: string; + /** Book ID */ + bookId: string; +} +export interface Response$getBookById$Status$200 { + "application/json": Schemas.Book; +} export type ResponseContentType$getBook = keyof Response$getBook$Status$200; export interface Params$getBook { parameter: Parameter$getBook; @@ -1378,6 +1387,10 @@ export interface Params$createPublisherV2 { requestBodyEncoding: requestEncodings[params.headers["Content-Type"]] }, option); } + public async getBookById(params: Params$getBookById, option?: RequestOption): Promise { + const url = this.baseUrl + \`/author/author-\${params.parameter.authorId}.a.\${params.parameter.bookId}.b/book/\${params.parameter.bookId}.json\`; + const headers = { + Accept: "application/json" + }; + return this.apiClient.request({ + httpMethod: "GET", + url, + headers + }, option); + } } " `; diff --git a/test/__tests__/functional/__snapshots__/typedef-with-template-test.ts.snap b/test/__tests__/functional/__snapshots__/typedef-with-template-test.ts.snap index bd95e7ed..7eded101 100644 --- a/test/__tests__/functional/__snapshots__/typedef-with-template-test.ts.snap +++ b/test/__tests__/functional/__snapshots__/typedef-with-template-test.ts.snap @@ -1360,6 +1360,15 @@ export interface Response$createPublisherV2$Status$200 { status?: string; }; } +export interface Parameter$getBookById { + /** Author ID */ + authorId: string; + /** Book ID */ + bookId: string; +} +export interface Response$getBookById$Status$200 { + "application/json": Schemas.Book; +} export type ResponseContentType$getBook = keyof Response$getBook$Status$200; export interface Params$getBook { parameter: Parameter$getBook; @@ -1389,6 +1398,10 @@ export interface Params$createPublisherV2(apiClient: ApiClient, requestBody: params.requestBody, requestBodyEncoding: requestEncodings[params.headers["Content-Type"]] }, option); + }, + getBookById: (params: Params$getBookById, option?: RequestOption): Promise => { + const url = _baseUrl + \`/author/author-\${params.parameter.authorId}.a.\${params.parameter.bookId}.b/book/\${params.parameter.bookId}.json\`; + const headers = { + Accept: "application/json" + }; + return apiClient.request({ + httpMethod: "GET", + url, + headers + }, option); } }; }; diff --git a/test/ref.access/index.yml b/test/ref.access/index.yml index ef224265..b320cd27 100644 --- a/test/ref.access/index.yml +++ b/test/ref.access/index.yml @@ -188,3 +188,29 @@ paths: properties: status: type: string + + /author/author-{authorId}.a.{bookId}.b/book/{bookId}.json: + parameters: + - name: authorId + in: path + required: true + description: Author ID + schema: + type: string + format: uuid + - name: bookId + in: path + required: true + description: Book ID + schema: + type: string + format: uuid + get: + operationId: getBookById + responses: + 200: + description: Get Books + content: + application/json: + schema: + $ref: "#/components/schemas/Book"