From 319a28af29541d7f61cca82389e1d486204f321f Mon Sep 17 00:00:00 2001 From: Lubos Date: Thu, 7 Nov 2024 17:49:05 +0800 Subject: [PATCH] fix: handle pagination with basic refs --- .changeset/tiny-bobcats-invent.md | 5 ++++ packages/openapi-ts/src/ir/operation.ts | 30 +++++++++++++------ .../src/openApi/3.0.x/parser/pagination.ts | 11 +++++++ .../src/openApi/3.1.x/parser/pagination.ts | 11 +++++++ .../plugins/@tanstack/query-core/plugin.ts | 2 +- packages/openapi-ts/test/sample.cjs | 2 +- 6 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 .changeset/tiny-bobcats-invent.md diff --git a/.changeset/tiny-bobcats-invent.md b/.changeset/tiny-bobcats-invent.md new file mode 100644 index 000000000..053f87bee --- /dev/null +++ b/.changeset/tiny-bobcats-invent.md @@ -0,0 +1,5 @@ +--- +'@hey-api/openapi-ts': patch +--- + +fix: handle pagination with basic refs diff --git a/packages/openapi-ts/src/ir/operation.ts b/packages/openapi-ts/src/ir/operation.ts index 4e52e7a08..05443cc02 100644 --- a/packages/openapi-ts/src/ir/operation.ts +++ b/packages/openapi-ts/src/ir/operation.ts @@ -1,3 +1,4 @@ +import type { IRContext } from './context'; import type { IROperationObject, IRResponseObject, IRSchemaObject } from './ir'; import type { Pagination } from './pagination'; import { @@ -21,18 +22,29 @@ export const hasOperationDataRequired = ( return false; }; -export const operationPagination = ( - operation: IROperationObject, -): Pagination | undefined => { +export const operationPagination = ({ + context, + operation, +}: { + context: IRContext; + operation: IROperationObject; +}): Pagination | undefined => { if (operation.body?.pagination) { + if (typeof operation.body.pagination === 'boolean') { + return { + in: 'body', + name: 'body', + schema: operation.body.schema, + }; + } + + const schema = operation.body.schema.$ref + ? context.resolveIrRef(operation.body.schema.$ref) + : operation.body.schema; return { in: 'body', - name: - operation.body.pagination === true ? 'body' : operation.body.pagination, - schema: - operation.body.pagination === true - ? operation.body.schema - : operation.body.schema.properties![operation.body.pagination], + name: operation.body.pagination, + schema: schema.properties![operation.body.pagination], }; } diff --git a/packages/openapi-ts/src/openApi/3.0.x/parser/pagination.ts b/packages/openapi-ts/src/openApi/3.0.x/parser/pagination.ts index 3e255590d..936678c8e 100644 --- a/packages/openapi-ts/src/openApi/3.0.x/parser/pagination.ts +++ b/packages/openapi-ts/src/openApi/3.0.x/parser/pagination.ts @@ -84,5 +84,16 @@ export const paginationField = ({ } } + for (const allOf of schema.allOf ?? []) { + const pagination = paginationField({ + context, + name, + schema: allOf, + }); + if (pagination) { + return pagination; + } + } + return false; }; diff --git a/packages/openapi-ts/src/openApi/3.1.x/parser/pagination.ts b/packages/openapi-ts/src/openApi/3.1.x/parser/pagination.ts index b3c287c3d..48303c81c 100644 --- a/packages/openapi-ts/src/openApi/3.1.x/parser/pagination.ts +++ b/packages/openapi-ts/src/openApi/3.1.x/parser/pagination.ts @@ -80,5 +80,16 @@ export const paginationField = ({ } } + for (const allOf of schema.allOf ?? []) { + const pagination = paginationField({ + context, + name, + schema: allOf, + }); + if (pagination) { + return pagination; + } + } + return false; }; diff --git a/packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts b/packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts index 1d4a85e3d..07f44e3d9 100644 --- a/packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts +++ b/packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts @@ -853,7 +853,7 @@ export const handler: PluginHandler< plugin.infiniteQueryOptions && (['get', 'post'] as (typeof method)[]).includes(method) ) { - const pagination = operationPagination(operation); + const pagination = operationPagination({ context, operation }); if (pagination) { if (!hasInfiniteQueries) { diff --git a/packages/openapi-ts/test/sample.cjs b/packages/openapi-ts/test/sample.cjs index d0e4461ed..afc488d8e 100644 --- a/packages/openapi-ts/test/sample.cjs +++ b/packages/openapi-ts/test/sample.cjs @@ -43,7 +43,7 @@ const main = async () => { // style: 'PascalCase', // tree: false, }, - // '@tanstack/react-query', + '@tanstack/react-query', // 'zod', ], // useOptions: false,