Skip to content

Commit

Permalink
Merge pull request #1239 from hey-api/fix/parser-ref-pagination
Browse files Browse the repository at this point in the history
fix: handle pagination with basic refs
  • Loading branch information
mrlubos authored Nov 7, 2024
2 parents 4a4f582 + 319a28a commit 3562159
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-bobcats-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: handle pagination with basic refs
30 changes: 21 additions & 9 deletions packages/openapi-ts/src/ir/operation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { IRContext } from './context';
import type { IROperationObject, IRResponseObject, IRSchemaObject } from './ir';
import type { Pagination } from './pagination';
import {
Expand All @@ -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<IRSchemaObject>(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],
};
}

Expand Down
11 changes: 11 additions & 0 deletions packages/openapi-ts/src/openApi/3.0.x/parser/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
11 changes: 11 additions & 0 deletions packages/openapi-ts/src/openApi/3.1.x/parser/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/test/sample.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const main = async () => {
// style: 'PascalCase',
// tree: false,
},
// '@tanstack/react-query',
'@tanstack/react-query',
// 'zod',
],
// useOptions: false,
Expand Down

0 comments on commit 3562159

Please sign in to comment.