Skip to content

Commit

Permalink
fix: handle pagination with basic refs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Nov 7, 2024
1 parent 4a4f582 commit 319a28a
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 => {

Check warning on line 31 in packages/openapi-ts/src/ir/operation.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/ir/operation.ts#L26-L31

Added lines #L26 - L31 were not covered by tests
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;

Check warning on line 43 in packages/openapi-ts/src/ir/operation.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/ir/operation.ts#L33-L43

Added lines #L33 - L43 were not covered by tests
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],

Check warning on line 47 in packages/openapi-ts/src/ir/operation.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/ir/operation.ts#L46-L47

Added lines #L46 - L47 were not covered by tests
};
}

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;
}
}

Check warning on line 97 in packages/openapi-ts/src/openApi/3.0.x/parser/pagination.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/openApi/3.0.x/parser/pagination.ts#L87-L97

Added lines #L87 - L97 were not covered by tests
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;
}

Check warning on line 91 in packages/openapi-ts/src/openApi/3.1.x/parser/pagination.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/openApi/3.1.x/parser/pagination.ts#L90-L91

Added lines #L90 - L91 were not covered by tests
}

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 });

Check warning on line 856 in packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts#L856

Added line #L856 was not covered by tests

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 319a28a

Please sign in to comment.