Skip to content

Commit 84b7928

Browse files
authored
fix: Parse dot case in path params (#201)
Closed #101
1 parent 35a28b5 commit 84b7928

File tree

2 files changed

+114
-2
lines changed

2 files changed

+114
-2
lines changed

plugins/typescript/src/core/createOperationFetcherFnNodes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,4 @@ export const createOperationFetcherFnNodes = ({
141141
* `pet/{pet_id}` -> `pet/{petId}`
142142
*/
143143
const camelizedPathParams = (url: string) =>
144-
url.replace(/\{[\w\d\-_]*\}/g, (match) => `{${camel(match)}}`);
144+
url.replace(/\{[\w\d\-_.]*\}/g, (match) => `{${camel(match)}}`);

plugins/typescript/src/generators/generateReactQueryComponents.test.ts

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ describe("generateReactQueryComponents", () => {
989989
`);
990990
});
991991

992-
it("should deal with pathParams (kebab case)", async () => {
992+
it("should deal with pathParams (dash case)", async () => {
993993
const writeFile = jest.fn();
994994
const openAPIDocument: OpenAPIObject = {
995995
openapi: "3.0.0",
@@ -1101,6 +1101,118 @@ describe("generateReactQueryComponents", () => {
11011101
`);
11021102
});
11031103

1104+
it("should deal with pathParams (dot case)", async () => {
1105+
const writeFile = jest.fn();
1106+
const openAPIDocument: OpenAPIObject = {
1107+
openapi: "3.0.0",
1108+
info: {
1109+
title: "petshop",
1110+
version: "1.0.0",
1111+
},
1112+
components: {
1113+
requestBodies: {
1114+
UpdatePetRequestBody: {
1115+
content: {
1116+
"application/json": {
1117+
schema: {
1118+
type: "object",
1119+
properties: {
1120+
name: {
1121+
type: "string",
1122+
},
1123+
},
1124+
},
1125+
},
1126+
},
1127+
},
1128+
},
1129+
},
1130+
paths: {
1131+
"/pet/{pet.id}": {
1132+
parameters: [
1133+
{
1134+
in: "path",
1135+
name: "pet.id",
1136+
schema: {
1137+
type: "string",
1138+
},
1139+
required: true,
1140+
},
1141+
],
1142+
put: {
1143+
operationId: "updatePet",
1144+
requestBody: {
1145+
$ref: "#/components/requestBodies/UpdatePetRequestBody",
1146+
},
1147+
responses: {
1148+
200: {
1149+
content: {
1150+
"application/json": {
1151+
description: "Successful response",
1152+
schema: {
1153+
type: "string",
1154+
},
1155+
},
1156+
},
1157+
},
1158+
},
1159+
},
1160+
},
1161+
},
1162+
};
1163+
await generateReactQueryComponents(
1164+
{
1165+
openAPIDocument,
1166+
writeFile,
1167+
existsFile: () => true,
1168+
readFile: async () => "",
1169+
},
1170+
config
1171+
);
1172+
1173+
expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts");
1174+
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
1175+
"/**
1176+
* Generated by @openapi-codegen
1177+
*
1178+
* @version 1.0.0
1179+
*/
1180+
import * as reactQuery from \\"@tanstack/react-query\\";
1181+
import { usePetstoreContext, PetstoreContext } from \\"./petstoreContext\\";
1182+
import type * as Fetcher from \\"./petstoreFetcher\\";
1183+
import { petstoreFetch } from \\"./petstoreFetcher\\";
1184+
import type * as RequestBodies from \\"./petstoreRequestBodies\\";
1185+
1186+
export type UpdatePetPathParams = {
1187+
petId: string;
1188+
};
1189+
1190+
export type UpdatePetError = Fetcher.ErrorWrapper<undefined>;
1191+
1192+
export type UpdatePetVariables = {
1193+
body?: RequestBodies.UpdatePetRequestBody;
1194+
pathParams: UpdatePetPathParams;
1195+
} & PetstoreContext[\\"fetcherOptions\\"];
1196+
1197+
export const fetchUpdatePet = (variables: UpdatePetVariables, signal?: AbortSignal) => petstoreFetch<string, UpdatePetError, RequestBodies.UpdatePetRequestBody, {}, {}, UpdatePetPathParams>({ url: \\"/pet/{petId}\\", method: \\"put\\", ...variables, signal });
1198+
1199+
export const useUpdatePet = (options?: Omit<reactQuery.UseMutationOptions<string, UpdatePetError, UpdatePetVariables>, \\"mutationFn\\">) => {
1200+
const { fetcherOptions } = usePetstoreContext();
1201+
return reactQuery.useMutation<string, UpdatePetError, UpdatePetVariables>({
1202+
mutationFn: (variables: UpdatePetVariables) => fetchUpdatePet({ ...fetcherOptions, ...variables }),
1203+
...options
1204+
});
1205+
};
1206+
1207+
export type QueryOperation = {
1208+
path: string;
1209+
operationId: never;
1210+
variables: unknown;
1211+
};
1212+
"
1213+
`);
1214+
});
1215+
11041216
it("should build components without prefix", async () => {
11051217
const writeFile = jest.fn();
11061218
const openAPIDocument: OpenAPIObject = {

0 commit comments

Comments
 (0)