Skip to content

Commit

Permalink
fix: Fix path param regex (#166)
Browse files Browse the repository at this point in the history
* fix: Fix path param regex

* Add bug reports to @antoniel
  • Loading branch information
fabien0102 committed May 9, 2023
1 parent 6e2aa7b commit 59bae62
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
"avatar_url": "https://avatars.githubusercontent.com/u/17225358?v=4",
"profile": "https://github.com/antoniel",
"contributions": [
"example"
"example",
"bug"
]
},
{
Expand Down
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,22 +269,20 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tbody>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://fabien0102.com/"><img src="https://avatars.githubusercontent.com/u/1761469?v=4?s=100" width="100px;" alt="Fabien BERNARD"/><br /><sub><b>Fabien BERNARD</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=fabien0102" title="Code">💻</a> <a href="#design-fabien0102" title="Design">🎨</a> <a href="https://github.com/fabien0102/openapi-codegen/commits?author=fabien0102" title="Documentation">📖</a> <a href="#ideas-fabien0102" title="Ideas, Planning, & Feedback">🤔</a> <a href="#projectManagement-fabien0102" title="Project Management">📆</a> <a href="https://github.com/fabien0102/openapi-codegen/pulls?q=is%3Apr+reviewed-by%3Afabien0102" title="Reviewed Pull Requests">👀</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/mpotomin"><img src="https://avatars.githubusercontent.com/u/639406?v=4?s=100" width="100px;" alt="mpotomin"/><br /><sub><b>mpotomin</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=mpotomin" title="Code">💻</a> <a href="#ideas-mpotomin" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/fabien0102/openapi-codegen/pulls?q=is%3Apr+reviewed-by%3Ampotomin" title="Reviewed Pull Requests">👀</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/micha-f"><img src="https://avatars.githubusercontent.com/u/200647?v=4?s=100" width="100px;" alt="Michael Franzkowiak"/><br /><sub><b>Michael Franzkowiak</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=micha-f" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/SferaDev"><img src="https://avatars.githubusercontent.com/u/2181866?v=4?s=100" width="100px;" alt="Alexis Rico"/><br /><sub><b>Alexis Rico</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=SferaDev" title="Code">💻</a> <a href="#ideas-SferaDev" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://ned.im/"><img src="https://avatars.githubusercontent.com/u/271912?v=4?s=100" width="100px;" alt="Nedim Arabacı"/><br /><sub><b>Nedim Arabacı</b></sub></a><br /><a href="#question-needim" title="Answering Questions">💬</a> <a href="https://github.com/fabien0102/openapi-codegen/commits?author=needim" title="Code">💻</a> <a href="#ideas-needim" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/antoniel"><img src="https://avatars.githubusercontent.com/u/17225358?v=4?s=100" width="100px;" alt="Antoniel Magalhães"/><br /><sub><b>Antoniel Magalhães</b></sub></a><br /><a href="#example-antoniel" title="Examples">💡</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/DreierF"><img src="https://avatars.githubusercontent.com/u/5631865?v=4?s=100" width="100px;" alt="Florian Dreier"/><br /><sub><b>Florian Dreier</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=DreierF" title="Code">💻</a></td>
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="http://fabianalthaus.de"><img src="https://avatars.githubusercontent.com/u/2795534?v=4?s=100" width="100px;" alt="Fabian Althaus"/><br /><sub><b>Fabian Althaus</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=el-j" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/ci-vamp"><img src="https://avatars.githubusercontent.com/u/116516277?v=4?s=100" width="100px;" alt="ci-vamp"/><br /><sub><b>ci-vamp</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/issues?q=author%3Aci-vamp" title="Bug reports">🐛</a> <a href="https://github.com/fabien0102/openapi-codegen/commits?author=ci-vamp" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://twitter.com/oalanoliv"><img src="https://avatars.githubusercontent.com/u/4368481?v=4?s=100" width="100px;" alt="Alan Oliveira"/><br /><sub><b>Alan Oliveira</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=alan-oliv" title="Code">💻</a></td>
</tr>
</tbody>
<tr>
<td align="center"><a href="https://fabien0102.com/"><img src="https://avatars.githubusercontent.com/u/1761469?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Fabien BERNARD</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=fabien0102" title="Code">💻</a> <a href="#design-fabien0102" title="Design">🎨</a> <a href="https://github.com/fabien0102/openapi-codegen/commits?author=fabien0102" title="Documentation">📖</a> <a href="#ideas-fabien0102" title="Ideas, Planning, & Feedback">🤔</a> <a href="#projectManagement-fabien0102" title="Project Management">📆</a> <a href="https://github.com/fabien0102/openapi-codegen/pulls?q=is%3Apr+reviewed-by%3Afabien0102" title="Reviewed Pull Requests">👀</a></td>
<td align="center"><a href="https://github.com/mpotomin"><img src="https://avatars.githubusercontent.com/u/639406?v=4?s=100" width="100px;" alt=""/><br /><sub><b>mpotomin</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=mpotomin" title="Code">💻</a> <a href="#ideas-mpotomin" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/fabien0102/openapi-codegen/pulls?q=is%3Apr+reviewed-by%3Ampotomin" title="Reviewed Pull Requests">👀</a></td>
<td align="center"><a href="https://github.com/micha-f"><img src="https://avatars.githubusercontent.com/u/200647?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Michael Franzkowiak</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=micha-f" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/SferaDev"><img src="https://avatars.githubusercontent.com/u/2181866?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Alexis Rico</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=SferaDev" title="Code">💻</a> <a href="#ideas-SferaDev" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center"><a href="https://ned.im/"><img src="https://avatars.githubusercontent.com/u/271912?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nedim Arabacı</b></sub></a><br /><a href="#question-needim" title="Answering Questions">💬</a> <a href="https://github.com/fabien0102/openapi-codegen/commits?author=needim" title="Code">💻</a> <a href="#ideas-needim" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center"><a href="https://github.com/antoniel"><img src="https://avatars.githubusercontent.com/u/17225358?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Antoniel Magalhães</b></sub></a><br /><a href="#example-antoniel" title="Examples">💡</a> <a href="https://github.com/fabien0102/openapi-codegen/issues?q=author%3Aantoniel" title="Bug reports">🐛</a></td>
<td align="center"><a href="https://github.com/DreierF"><img src="https://avatars.githubusercontent.com/u/5631865?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Florian Dreier</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=DreierF" title="Code">💻</a></td>
</tr>
<tr>
<td align="center"><a href="http://fabianalthaus.de"><img src="https://avatars.githubusercontent.com/u/2795534?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Fabian Althaus</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=el-j" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/ci-vamp"><img src="https://avatars.githubusercontent.com/u/116516277?v=4?s=100" width="100px;" alt=""/><br /><sub><b>ci-vamp</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/issues?q=author%3Aci-vamp" title="Bug reports">🐛</a> <a href="https://github.com/fabien0102/openapi-codegen/commits?author=ci-vamp" title="Code">💻</a></td>
<td align="center"><a href="https://twitter.com/oalanoliv"><img src="https://avatars.githubusercontent.com/u/4368481?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Alan Oliveira</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=alan-oliv" title="Code">💻</a></td>
</tr>
</table>

<!-- markdownlint-restore -->
Expand Down
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*\}/g, (match) => `{${camel(match)}}`);
url.replace(/\{.*\}/g, (match) => `{${camel(match)}}`);
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ describe("generateReactQueryComponents", () => {
`);
});

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

it("should deal with pathParams (kebab 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>((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 59bae62

Please sign in to comment.