-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1530 from hey-api/fix/pagination-ref-composition
fix: detect pagination in composite schemas with null type
- Loading branch information
Showing
11 changed files
with
252 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@hey-api/openapi-ts': patch | ||
--- | ||
|
||
fix: detect pagination in composite schemas with null type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...es/openapi-ts/test/__snapshots__/3.1.x/pagination-ref-any-of/@tanstack/react-query.gen.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// This file is auto-generated by @hey-api/openapi-ts | ||
|
||
import type { Options } from '@hey-api/client-fetch'; | ||
import { queryOptions, infiniteQueryOptions, type InfiniteData, type DefaultError, type UseMutationOptions } from '@tanstack/react-query'; | ||
import type { PostFooData } from '../types.gen'; | ||
import { postFoo, client } from '../sdk.gen'; | ||
|
||
type QueryKey<TOptions extends Options> = [ | ||
Pick<TOptions, 'baseUrl' | 'body' | 'headers' | 'path' | 'query'> & { | ||
_id: string; | ||
_infinite?: boolean; | ||
} | ||
]; | ||
|
||
const createQueryKey = <TOptions extends Options>(id: string, options?: TOptions, infinite?: boolean): QueryKey<TOptions>[0] => { | ||
const params: QueryKey<TOptions>[0] = { _id: id, baseUrl: (options?.client ?? client).getConfig().baseUrl } as QueryKey<TOptions>[0]; | ||
if (infinite) { | ||
params._infinite = infinite; | ||
} | ||
if (options?.body) { | ||
params.body = options.body; | ||
} | ||
if (options?.headers) { | ||
params.headers = options.headers; | ||
} | ||
if (options?.path) { | ||
params.path = options.path; | ||
} | ||
if (options?.query) { | ||
params.query = options.query; | ||
} | ||
return params; | ||
}; | ||
|
||
export const postFooQueryKey = (options: Options<PostFooData>) => [ | ||
createQueryKey('postFoo', options) | ||
]; | ||
|
||
export const postFooOptions = (options: Options<PostFooData>) => { | ||
return queryOptions({ | ||
queryFn: async ({ queryKey, signal }) => { | ||
const { data } = await postFoo({ | ||
...options, | ||
...queryKey[0], | ||
signal, | ||
throwOnError: true | ||
}); | ||
return data; | ||
}, | ||
queryKey: postFooQueryKey(options) | ||
}); | ||
}; | ||
|
||
const createInfiniteParams = <K extends Pick<QueryKey<Options>[0], 'body' | 'headers' | 'path' | 'query'>>(queryKey: QueryKey<Options>, page: K) => { | ||
const params = queryKey[0]; | ||
if (page.body) { | ||
params.body = { | ||
...queryKey[0].body as any, | ||
...page.body as any | ||
}; | ||
} | ||
if (page.headers) { | ||
params.headers = { | ||
...queryKey[0].headers, | ||
...page.headers | ||
}; | ||
} | ||
if (page.path) { | ||
params.path = { | ||
...queryKey[0].path as any, | ||
...page.path as any | ||
}; | ||
} | ||
if (page.query) { | ||
params.query = { | ||
...queryKey[0].query as any, | ||
...page.query as any | ||
}; | ||
} | ||
return params as unknown as typeof page; | ||
}; | ||
|
||
export const postFooInfiniteQueryKey = (options: Options<PostFooData>): QueryKey<Options<PostFooData>> => [ | ||
createQueryKey('postFoo', options, true) | ||
]; | ||
|
||
export const postFooInfiniteOptions = (options: Options<PostFooData>) => { | ||
return infiniteQueryOptions<unknown, DefaultError, InfiniteData<unknown>, QueryKey<Options<PostFooData>>, number | null | Pick<QueryKey<Options<PostFooData>>[0], 'body' | 'headers' | 'path' | 'query'>>( | ||
// @ts-ignore | ||
{ | ||
queryFn: async ({ pageParam, queryKey, signal }) => { | ||
// @ts-ignore | ||
const page: Pick<QueryKey<Options<PostFooData>>[0], 'body' | 'headers' | 'path' | 'query'> = typeof pageParam === 'object' ? pageParam : { | ||
body: { | ||
page: pageParam | ||
} | ||
}; | ||
const params = createInfiniteParams(queryKey, page); | ||
const { data } = await postFoo({ | ||
...options, | ||
...params, | ||
signal, | ||
throwOnError: true | ||
}); | ||
return data; | ||
}, | ||
queryKey: postFooInfiniteQueryKey(options) | ||
}); | ||
}; | ||
|
||
export const postFooMutation = (options?: Partial<Options<PostFooData>>) => { | ||
const mutationOptions: UseMutationOptions<unknown, DefaultError, Options<PostFooData>> = { | ||
mutationFn: async (localOptions) => { | ||
const { data } = await postFoo({ | ||
...options, | ||
...localOptions, | ||
throwOnError: true | ||
}); | ||
return data; | ||
} | ||
}; | ||
return mutationOptions; | ||
}; |
3 changes: 3 additions & 0 deletions
3
packages/openapi-ts/test/__snapshots__/3.1.x/pagination-ref-any-of/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// This file is auto-generated by @hey-api/openapi-ts | ||
export * from './types.gen'; | ||
export * from './sdk.gen'; |
17 changes: 17 additions & 0 deletions
17
packages/openapi-ts/test/__snapshots__/3.1.x/pagination-ref-any-of/sdk.gen.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// This file is auto-generated by @hey-api/openapi-ts | ||
|
||
import { createClient, createConfig, type Options } from '@hey-api/client-fetch'; | ||
import type { PostFooData } from './types.gen'; | ||
|
||
export const client = createClient(createConfig()); | ||
|
||
export const postFoo = <ThrowOnError extends boolean = false>(options: Options<PostFooData, ThrowOnError>) => { | ||
return (options?.client ?? client).post<unknown, unknown, ThrowOnError>({ | ||
...options, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
...options?.headers | ||
}, | ||
url: '/foo' | ||
}); | ||
}; |
19 changes: 19 additions & 0 deletions
19
packages/openapi-ts/test/__snapshots__/3.1.x/pagination-ref-any-of/types.gen.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// This file is auto-generated by @hey-api/openapi-ts | ||
|
||
export type Foo = { | ||
page?: number | null; | ||
}; | ||
|
||
export type PostFooData = { | ||
body: Foo; | ||
path?: never; | ||
query?: never; | ||
url: '/foo'; | ||
}; | ||
|
||
export type PostFooResponses = { | ||
/** | ||
* OK | ||
*/ | ||
200: unknown; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/openapi-ts/test/spec/3.1.x/pagination-ref-any-of.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
openapi: 3.1.1 | ||
info: | ||
title: OpenAPI 3.1.1 pagination ref any of example | ||
version: 1 | ||
paths: | ||
/foo: | ||
post: | ||
requestBody: | ||
content: | ||
'application/json': | ||
schema: | ||
$ref: '#/components/schemas/Foo' | ||
required: true | ||
responses: | ||
'200': | ||
description: OK | ||
components: | ||
schemas: | ||
Foo: | ||
properties: | ||
page: | ||
anyOf: | ||
- type: integer | ||
minimum: 1.0 | ||
- type: 'null' | ||
default: 1 | ||
type: object |