Skip to content

Commit

Permalink
fix: Parse dot case in path params (#201)
Browse files Browse the repository at this point in the history
Closed #101
  • Loading branch information
fabien0102 committed Sep 17, 2023
1 parent 35a28b5 commit 84b7928
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ export const createOperationFetcherFnNodes = ({
* `pet/{pet_id}` -> `pet/{petId}`
*/
const camelizedPathParams = (url: string) =>
url.replace(/\{[\w\d\-_]*\}/g, (match) => `{${camel(match)}}`);
url.replace(/\{[\w\d\-_.]*\}/g, (match) => `{${camel(match)}}`);
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ describe("generateReactQueryComponents", () => {
`);
});

it("should deal with pathParams (kebab case)", async () => {
it("should deal with pathParams (dash case)", async () => {
const writeFile = jest.fn();
const openAPIDocument: OpenAPIObject = {
openapi: "3.0.0",
Expand Down Expand Up @@ -1101,6 +1101,118 @@ describe("generateReactQueryComponents", () => {
`);
});

it("should deal with pathParams (dot case)", async () => {
const writeFile = jest.fn();
const openAPIDocument: OpenAPIObject = {
openapi: "3.0.0",
info: {
title: "petshop",
version: "1.0.0",
},
components: {
requestBodies: {
UpdatePetRequestBody: {
content: {
"application/json": {
schema: {
type: "object",
properties: {
name: {
type: "string",
},
},
},
},
},
},
},
},
paths: {
"/pet/{pet.id}": {
parameters: [
{
in: "path",
name: "pet.id",
schema: {
type: "string",
},
required: true,
},
],
put: {
operationId: "updatePet",
requestBody: {
$ref: "#/components/requestBodies/UpdatePetRequestBody",
},
responses: {
200: {
content: {
"application/json": {
description: "Successful response",
schema: {
type: "string",
},
},
},
},
},
},
},
},
};
await generateReactQueryComponents(
{
openAPIDocument,
writeFile,
existsFile: () => true,
readFile: async () => "",
},
config
);

expect(writeFile.mock.calls[0][0]).toBe("petstoreComponents.ts");
expect(writeFile.mock.calls[0][1]).toMatchInlineSnapshot(`
"/**
* Generated by @openapi-codegen
*
* @version 1.0.0
*/
import * as reactQuery from \\"@tanstack/react-query\\";
import { usePetstoreContext, PetstoreContext } from \\"./petstoreContext\\";
import type * as Fetcher from \\"./petstoreFetcher\\";
import { petstoreFetch } from \\"./petstoreFetcher\\";
import type * as RequestBodies from \\"./petstoreRequestBodies\\";
export type UpdatePetPathParams = {
petId: string;
};
export type UpdatePetError = Fetcher.ErrorWrapper<undefined>;
export type UpdatePetVariables = {
body?: RequestBodies.UpdatePetRequestBody;
pathParams: UpdatePetPathParams;
} & PetstoreContext[\\"fetcherOptions\\"];
export const fetchUpdatePet = (variables: UpdatePetVariables, signal?: AbortSignal) => petstoreFetch<string, UpdatePetError, RequestBodies.UpdatePetRequestBody, {}, {}, UpdatePetPathParams>({ url: \\"/pet/{petId}\\", method: \\"put\\", ...variables, signal });
export const useUpdatePet = (options?: Omit<reactQuery.UseMutationOptions<string, UpdatePetError, UpdatePetVariables>, \\"mutationFn\\">) => {
const { fetcherOptions } = usePetstoreContext();
return reactQuery.useMutation<string, UpdatePetError, UpdatePetVariables>({
mutationFn: (variables: UpdatePetVariables) => fetchUpdatePet({ ...fetcherOptions, ...variables }),
...options
});
};
export type QueryOperation = {
path: string;
operationId: never;
variables: unknown;
};
"
`);
});

it("should build components without prefix", async () => {
const writeFile = jest.fn();
const openAPIDocument: OpenAPIObject = {
Expand Down

0 comments on commit 84b7928

Please sign in to comment.