Skip to content

Commit

Permalink
fix: separated strings are not restored correctly (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
Himenon committed Jun 23, 2023
1 parent 8c5abe6 commit c4b47c8
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/code-templates/_shared/MethodBody/PathParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -111,7 +111,7 @@ export const generateUrlTemplateExpression = (
} else {
urlTemplate.push({
type: "string",
value: value.startsWith("/") ? value : "/" + value,
value: value,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}", [
Expand Down
5 changes: 5 additions & 0 deletions src/code-templates/_shared/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
})
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1378,6 +1387,10 @@ export interface Params$createPublisherV2<T extends RequestContentType$createPub
};
requestBody: RequestBody$createPublisherV2[T];
}
export type ResponseContentType$getBookById = keyof Response$getBookById$Status$200;
export interface Params$getBookById {
parameter: Parameter$getBookById;
}
export type HttpMethod = "GET" | "PUT" | "POST" | "DELETE" | "OPTIONS" | "HEAD" | "PATCH" | "TRACE";
export interface ObjectLike {
[key: string]: any;
Expand All @@ -1390,14 +1403,15 @@ export interface QueryParameter {
export interface QueryParameters {
[key: string]: QueryParameter;
}
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;
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;
export namespace ErrorResponse {
export type getBook = void;
export type getDescription = void;
export type getAuthor = void;
export type getPublisher = void;
export type createPublisher = void;
export type createPublisherV2 = void;
export type getBookById = void;
}
export interface Encoding {
readonly contentType?: string;
Expand Down Expand Up @@ -1514,6 +1528,17 @@ export class Client<RequestOption> {
requestBodyEncoding: requestEncodings[params.headers["Content-Type"]]
}, option);
}
public async getBookById(params: Params$getBookById, option?: RequestOption): Promise<Response$getBookById$Status$200["application/json"]> {
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);
}
}
"
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1389,6 +1398,10 @@ export interface Params$createPublisherV2<T extends RequestContentType$createPub
};
requestBody: RequestBody$createPublisherV2[T];
}
export type ResponseContentType$getBookById = keyof Response$getBookById$Status$200;
export interface Params$getBookById {
parameter: Parameter$getBookById;
}
export type HttpMethod = "GET" | "PUT" | "POST" | "DELETE" | "OPTIONS" | "HEAD" | "PATCH" | "TRACE";
export interface ObjectLike {
[key: string]: any;
Expand All @@ -1401,14 +1414,15 @@ export interface QueryParameter {
export interface QueryParameters {
[key: string]: QueryParameter;
}
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;
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;
export namespace ErrorResponse {
export type getBook = void;
export type getDescription = void;
export type getAuthor = void;
export type getPublisher = void;
export type createPublisher = void;
export type createPublisherV2 = void;
export type getBookById = void;
}
export interface Encoding {
readonly contentType?: string;
Expand Down Expand Up @@ -1524,6 +1538,17 @@ export const createClient = <RequestOption>(apiClient: ApiClient<RequestOption>,
requestBody: params.requestBody,
requestBodyEncoding: requestEncodings[params.headers["Content-Type"]]
}, option);
},
getBookById: (params: Params$getBookById, option?: RequestOption): Promise<Response$getBookById$Status$200["application/json"]> => {
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);
}
};
};
Expand Down
26 changes: 26 additions & 0 deletions test/ref.access/index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit c4b47c8

Please sign in to comment.