From b634bc905fc660e270908d6ee09b01b7f3811bf5 Mon Sep 17 00:00:00 2001 From: Stijn Van Hulle Date: Fri, 15 Nov 2024 16:01:16 +0100 Subject: [PATCH] fix: Correct use of variables when a path/params contains _ or - (#1401) --- .changeset/nervous-pets-shave.md | 5 + docs/changelog.md | 4 + docs/package.json | 2 +- e2e/package.json | 2 +- examples/advanced/package.json | 2 +- examples/advanced/petStore.yaml | 27 +- .../src/gen/clients/axios/operations.ts | 2 +- .../axios/petService/findPetsByStatus.ts | 11 +- .../petController/useFindPetsByStatus.ts | 35 +- .../petController/useFindPetsByStatusSWR.ts | 33 +- examples/advanced/src/gen/docs.html | 8 +- examples/advanced/src/gen/index.ts | 9 +- examples/advanced/src/gen/mocks/index.ts | 2 +- .../createFindPetsByStatusFaker.ts | 6 +- .../src/gen/mocks/petController/index.ts | 2 +- examples/advanced/src/gen/models/ts/index.ts | 3 +- .../ts/petController/FindPetsByStatus.ts | 16 +- .../src/gen/models/ts/petController/index.ts | 3 +- .../petController/findPetsByStatusHandler.ts | 2 +- examples/advanced/src/gen/zod/index.ts | 4 +- .../petController/findPetsByStatusSchema.ts | 6 +- .../src/gen/zod/petController/index.ts | 4 +- .../gen/clients/axios/petService/deletePet.ts | 10 +- .../clients/axios/petService/getPetById.ts | 9 +- .../axios/petService/updatePetWithForm.ts | 10 +- .../clients/axios/petService/uploadFile.ts | 6 +- .../clients/axios/userService/deleteUser.ts | 9 +- .../axios/userService/getUserByName.ts | 9 +- .../clients/axios/userService/updateUser.ts | 6 +- examples/client/src/gen/tagObject.ts | 18 +- examples/react-query/package.json | 4 +- .../src/gen/hooks/useGetOrderByIdHook.ts | 15 +- .../gen/hooks/useGetOrderByIdSuspenseHook.ts | 15 +- .../src/gen/hooks/useGetPetByIdHook.ts | 15 +- .../gen/hooks/useGetPetByIdSuspenseHook.ts | 15 +- .../src/gen/hooks/useGetUserByNameHook.ts | 15 +- .../gen/hooks/useGetUserByNameSuspenseHook.ts | 15 +- examples/simple-single/package.json | 2 +- examples/zod/src/gen/zodClients.ts | 18 +- package.json | 8 +- .../plugin-client/src/components/Client.tsx | 1 - .../__snapshots__/deletePetObject.ts | 4 +- .../src/components/InfiniteQuery.tsx | 1 - .../src/components/InfiniteQueryOptions.tsx | 7 +- .../src/components/Query.tsx | 1 - .../src/components/QueryOptions.tsx | 3 +- .../src/components/SuspenseQuery.tsx | 1 - .../updatePetByIdPathParamsObject.ts | 4 +- .../src/components/Query.tsx | 1 - .../src/components/QueryOptions.tsx | 3 +- .../src/components/Query.tsx | 1 - .../src/components/QueryOptions.tsx | 3 +- .../updatePetByIdPathParamsObject.ts | 4 +- packages/plugin-swr/src/components/Query.tsx | 3 +- .../src/components/QueryOptions.tsx | 1 - .../updatePetByIdPathParamsObject.ts | 4 +- .../src/components/InfiniteQuery.tsx | 1 - .../src/components/InfiniteQueryOptions.tsx | 5 +- .../plugin-vue-query/src/components/Query.tsx | 1 - .../src/components/QueryOptions.tsx | 3 +- .../updatePetByIdPathParamsObject.ts | 4 +- packages/unplugin-kubb/package.json | 4 +- pnpm-lock.yaml | 585 ++++++++++++------ 63 files changed, 703 insertions(+), 329 deletions(-) create mode 100644 .changeset/nervous-pets-shave.md diff --git a/.changeset/nervous-pets-shave.md b/.changeset/nervous-pets-shave.md new file mode 100644 index 000000000..bc1fd5bb9 --- /dev/null +++ b/.changeset/nervous-pets-shave.md @@ -0,0 +1,5 @@ +--- +"@kubb/oas": patch +--- + +Correct use of variables when a path/params contains \_ or - diff --git a/docs/changelog.md b/docs/changelog.md index 15952a9d5..ed545d0e0 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -3,6 +3,10 @@ title: Changelog --- # Changelog + +## 3.0.6 +-[`plugin-oas`](/plugins/plugin-oas/): Correct use of variables when a path/params contains _ or - + ## 3.0.5 - [`react`](/helpers/react//): Better error logging + wider range for `@kubb/react` peerDependency diff --git a/docs/package.json b/docs/package.json index 2b9319f3a..f7285bec3 100644 --- a/docs/package.json +++ b/docs/package.json @@ -21,7 +21,7 @@ "serve": "vitepress serve" }, "dependencies": { - "@shikijs/vitepress-twoslash": "^1.22.2", + "@shikijs/vitepress-twoslash": "^1.23.0", "mermaid": "^11.4.0", "sitemap": "^8.0.0", "vitepress": "^1.5.0", diff --git a/e2e/package.json b/e2e/package.json index 6e36e08c8..a8ad62a7b 100644 --- a/e2e/package.json +++ b/e2e/package.json @@ -32,7 +32,7 @@ "@kubb/plugin-ts": "workspace:*", "@kubb/plugin-zod": "workspace:*", "@kubb/react": "workspace:*", - "@tanstack/react-query": "^5.59.20", + "@tanstack/react-query": "^5.60.2", "@tanstack/solid-query": "^5.59.20", "@tanstack/svelte-query": "^5.59.20", "@tanstack/vue-query": "^5.59.20", diff --git a/examples/advanced/package.json b/examples/advanced/package.json index 2d85b34c3..2056c122e 100644 --- a/examples/advanced/package.json +++ b/examples/advanced/package.json @@ -39,7 +39,7 @@ "@kubb/plugin-zod": "workspace:*", "@kubb/react": "workspace:*", "@tanstack/query-core": "^5.59.20", - "@tanstack/react-query": "^5.59.20", + "@tanstack/react-query": "^5.60.2", "@tanstack/solid-query": "^5.59.20", "@tanstack/svelte-query": "^5.59.20", "@tanstack/vue-query": "^5.59.20", diff --git a/examples/advanced/petStore.yaml b/examples/advanced/petStore.yaml index 33a43cb40..6b95cd7e9 100644 --- a/examples/advanced/petStore.yaml +++ b/examples/advanced/petStore.yaml @@ -175,7 +175,7 @@ paths: - petstore_auth: - write:pets - read:pets - /pet/findByStatus: + /pet/findByStatus/{step_id}: get: tags: - pet @@ -183,18 +183,23 @@ paths: description: Multiple status values can be provided with comma separated strings operationId: findPetsByStatus parameters: - - name: status - in: query - description: Status values that need to be considered for filter - required: false - explode: true + - name: step_id + in: path + required: true schema: type: string - default: available - enum: - - available - - pending - - sold +# - name: status +# in: query +# description: Status values that need to be considered for filter +# required: false +# explode: true +# schema: +# type: string +# default: available +# enum: +# - available +# - pending +# - sold responses: "200": description: successful operation diff --git a/examples/advanced/src/gen/clients/axios/operations.ts b/examples/advanced/src/gen/clients/axios/operations.ts index 3b29794ec..40519e742 100644 --- a/examples/advanced/src/gen/clients/axios/operations.ts +++ b/examples/advanced/src/gen/clients/axios/operations.ts @@ -12,7 +12,7 @@ export const operations = { method: 'post', }, findPetsByStatus: { - path: '/pet/findByStatus', + path: '/pet/findByStatus/:step_id', method: 'get', }, findPetsByTags: { diff --git a/examples/advanced/src/gen/clients/axios/petService/findPetsByStatus.ts b/examples/advanced/src/gen/clients/axios/petService/findPetsByStatus.ts index b2897764a..dd91b210f 100644 --- a/examples/advanced/src/gen/clients/axios/petService/findPetsByStatus.ts +++ b/examples/advanced/src/gen/clients/axios/petService/findPetsByStatus.ts @@ -1,25 +1,24 @@ import client from '../../../../axios-client.ts' import type { RequestConfig } from '../../../../axios-client.ts' -import type { FindPetsByStatusQueryResponse, FindPetsByStatusQueryParams, FindPetsByStatus400 } from '../../../models/ts/petController/FindPetsByStatus.ts' +import type { FindPetsByStatusQueryResponse, FindPetsByStatusPathParams, FindPetsByStatus400 } from '../../../models/ts/petController/FindPetsByStatus.ts' /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status - * @link /pet/findByStatus + * @link /pet/findByStatus/:step_id */ export async function findPetsByStatus( { - params, + stepId, }: { - params?: FindPetsByStatusQueryParams + stepId: FindPetsByStatusPathParams['step_id'] }, config: Partial = {}, ) { const res = await client({ method: 'GET', - url: '/pet/findByStatus', + url: `/pet/findByStatus/${stepId}`, baseURL: 'https://petstore3.swagger.io/api/v3', - params, ...config, }) return res diff --git a/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByStatus.ts b/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByStatus.ts index 371afaace..f58682915 100644 --- a/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByStatus.ts +++ b/examples/advanced/src/gen/clients/hooks/petController/useFindPetsByStatus.ts @@ -1,45 +1,50 @@ import client from '../../../../tanstack-query-client.ts' import type { RequestConfig, ResponseConfig } from '../../../../tanstack-query-client.ts' import type { QueryKey, QueryObserverOptions, UseQueryResult } from '../../../../tanstack-query-hook.ts' -import type { FindPetsByStatusQueryResponse, FindPetsByStatusQueryParams, FindPetsByStatus400 } from '../../../models/ts/petController/FindPetsByStatus.ts' +import type { FindPetsByStatusQueryResponse, FindPetsByStatusPathParams, FindPetsByStatus400 } from '../../../models/ts/petController/FindPetsByStatus.ts' import { queryOptions, useQuery } from '../../../../tanstack-query-hook.ts' import { findPetsByStatusQueryResponseSchema } from '../../../zod/petController/findPetsByStatusSchema.ts' -export const findPetsByStatusQueryKey = (params?: FindPetsByStatusQueryParams) => [{ url: '/pet/findByStatus' }, ...(params ? [params] : [])] as const +export const findPetsByStatusQueryKey = ({ + stepId, +}: { + stepId: FindPetsByStatusPathParams['step_id'] +}) => [{ url: '/pet/findByStatus/:step_id', params: { stepId: stepId } }] as const export type FindPetsByStatusQueryKey = ReturnType /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status - * @link /pet/findByStatus + * @link /pet/findByStatus/:step_id */ async function findPetsByStatus( { - params, + stepId, }: { - params?: FindPetsByStatusQueryParams + stepId: FindPetsByStatusPathParams['step_id'] }, config: Partial = {}, ) { - const res = await client({ method: 'GET', url: '/pet/findByStatus', params, ...config }) + const res = await client({ method: 'GET', url: `/pet/findByStatus/${stepId}`, ...config }) return { ...res, data: findPetsByStatusQueryResponseSchema.parse(res.data) } } export function findPetsByStatusQueryOptions( { - params, + stepId, }: { - params?: FindPetsByStatusQueryParams + stepId: FindPetsByStatusPathParams['step_id'] }, config: Partial = {}, ) { - const queryKey = findPetsByStatusQueryKey(params) + const queryKey = findPetsByStatusQueryKey({ stepId }) return queryOptions({ + enabled: !!stepId, queryKey, queryFn: async ({ signal }) => { config.signal = signal - return findPetsByStatus({ params }, config) + return findPetsByStatus({ stepId }, config) }, }) } @@ -47,7 +52,7 @@ export function findPetsByStatusQueryOptions( /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status - * @link /pet/findByStatus + * @link /pet/findByStatus/:step_id */ export function useFindPetsByStatus< TData = ResponseConfig, @@ -55,9 +60,9 @@ export function useFindPetsByStatus< TQueryKey extends QueryKey = FindPetsByStatusQueryKey, >( { - params, + stepId, }: { - params?: FindPetsByStatusQueryParams + stepId: FindPetsByStatusPathParams['step_id'] }, options: { query?: Partial, FindPetsByStatus400, TData, TQueryData, TQueryKey>> @@ -65,9 +70,9 @@ export function useFindPetsByStatus< } = {}, ) { const { query: queryOptions, client: config = {} } = options ?? {} - const queryKey = queryOptions?.queryKey ?? findPetsByStatusQueryKey(params) + const queryKey = queryOptions?.queryKey ?? findPetsByStatusQueryKey({ stepId }) const query = useQuery({ - ...(findPetsByStatusQueryOptions({ params }, config) as unknown as QueryObserverOptions), + ...(findPetsByStatusQueryOptions({ stepId }, config) as unknown as QueryObserverOptions), queryKey, ...(queryOptions as unknown as Omit), }) as UseQueryResult & { diff --git a/examples/advanced/src/gen/clients/swr/petController/useFindPetsByStatusSWR.ts b/examples/advanced/src/gen/clients/swr/petController/useFindPetsByStatusSWR.ts index a78073336..50567f1d9 100644 --- a/examples/advanced/src/gen/clients/swr/petController/useFindPetsByStatusSWR.ts +++ b/examples/advanced/src/gen/clients/swr/petController/useFindPetsByStatusSWR.ts @@ -1,31 +1,34 @@ import client from '../../../../swr-client.ts' import useSWR from 'swr' import type { RequestConfig } from '../../../../swr-client.ts' -import type { FindPetsByStatusQueryResponse, FindPetsByStatusQueryParams, FindPetsByStatus400 } from '../../../models/ts/petController/FindPetsByStatus.ts' +import type { FindPetsByStatusQueryResponse, FindPetsByStatusPathParams, FindPetsByStatus400 } from '../../../models/ts/petController/FindPetsByStatus.ts' import { findPetsByStatusQueryResponseSchema } from '../../../zod/petController/findPetsByStatusSchema.ts' -export const findPetsByStatusQueryKeySWR = (params?: FindPetsByStatusQueryParams) => [{ url: '/pet/findByStatus' }, ...(params ? [params] : [])] as const +export const findPetsByStatusQueryKeySWR = ({ + stepId, +}: { + stepId: FindPetsByStatusPathParams['step_id'] +}) => [{ url: '/pet/findByStatus/:step_id', params: { stepId: stepId } }] as const export type FindPetsByStatusQueryKeySWR = ReturnType /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status - * @link /pet/findByStatus + * @link /pet/findByStatus/:step_id */ async function findPetsByStatusSWR( { - params, + stepId, }: { - params?: FindPetsByStatusQueryParams + stepId: FindPetsByStatusPathParams['step_id'] }, config: Partial = {}, ) { const res = await client({ method: 'GET', - url: '/pet/findByStatus', + url: `/pet/findByStatus/${stepId}`, baseURL: 'https://petstore3.swagger.io/api/v3', - params, ...config, }) return findPetsByStatusQueryResponseSchema.parse(res.data) @@ -33,15 +36,15 @@ async function findPetsByStatusSWR( export function findPetsByStatusQueryOptionsSWR( { - params, + stepId, }: { - params?: FindPetsByStatusQueryParams + stepId: FindPetsByStatusPathParams['step_id'] }, config: Partial = {}, ) { return { fetcher: async () => { - return findPetsByStatusSWR({ params }, config) + return findPetsByStatusSWR({ stepId }, config) }, } } @@ -49,13 +52,13 @@ export function findPetsByStatusQueryOptionsSWR( /** * @description Multiple status values can be provided with comma separated strings * @summary Finds Pets by status - * @link /pet/findByStatus + * @link /pet/findByStatus/:step_id */ export function useFindPetsByStatusSWR( { - params, + stepId, }: { - params?: FindPetsByStatusQueryParams + stepId: FindPetsByStatusPathParams['step_id'] }, options: { query?: Parameters>[2] @@ -64,9 +67,9 @@ export function useFindPetsByStatusSWR( } = {}, ) { const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {} - const queryKey = findPetsByStatusQueryKeySWR(params) + const queryKey = findPetsByStatusQueryKeySWR({ stepId }) return useSWR(shouldFetch ? queryKey : null, { - ...findPetsByStatusQueryOptionsSWR({ params }, config), + ...findPetsByStatusQueryOptionsSWR({ stepId }, config), ...queryOptions, }) } diff --git a/examples/advanced/src/gen/docs.html b/examples/advanced/src/gen/docs.html index c3b112808..a47c74181 100644 --- a/examples/advanced/src/gen/docs.html +++ b/examples/advanced/src/gen/docs.html @@ -483,13 +483,11 @@ " class="sc-epnzzT sc-eMwmJz drsioI dWZUhK sc-drVZOg jtjIAv">

Invalid input

Request samples

Content type
{
  • "id": 10,
  • "name": "doggie",
  • "category": {
    },
  • "photoUrls": [
    ],
  • "tags": [
    ],
  • "status": "available"
}

Response samples

Content type
{
  • "id": 10,
  • "category": {
    },
  • "photoUrls": [
    ],
  • "tags": [
    ],
  • "status": "available"
}

Finds Pets by status

Multiple status values can be provided with comma separated strings

-
Authorizations:
petstore_auth
query Parameters
status
string
Default: "available"
Enum: "available" "pending" "sold"

Status values that need to be considered for filter

-

Responses

Response samples

Content type
[
  • {
    }
]

Finds Pets by tags

Response samples

Content type
[
  • {
    }
]

Finds Pets by tags

Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.

Authorizations:
petstore_auth
query Parameters
tags
Array of strings

Tags to filter by

@@ -647,7 +645,7 @@ " class="sc-epnzzT sc-eMwmJz drsioI dWZUhK sc-drVZOg jtjIAv">

unexpected error

Request samples

Content type
application/json
{
  • "name": "string",
  • "tag": "string"
}

Response samples

Content type
application/json
null