Skip to content

Commit 598028a

Browse files
authored
Merge pull request #1195 from hey-api/fix/tanstack-query-missing-import
fix: TanStack Query plugin using missing import for infinite query
2 parents 15acb33 + 753643f commit 598028a

File tree

6 files changed

+91
-18
lines changed

6 files changed

+91
-18
lines changed

.changeset/orange-singers-drop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hey-api/openapi-ts': patch
3+
---
4+
5+
fix: TanStack Query plugin using missing import for infinite query

packages/openapi-ts/src/ir/pagination.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { IRSchemaObject } from './ir';
22

3-
export const paginationKeywordsRegExp = /^(cursor|offset|page|start)/;
3+
export const paginationKeywordsRegExp = /^(cursor|offset|page|start)$/;
44

55
export interface Pagination {
66
in: string;

packages/openapi-ts/src/openApi/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const parseExperimental = ({
6868
config: Config;
6969
parserConfig: ParserConfig;
7070
spec: unknown;
71-
}) => {
71+
}): IRContext | undefined => {
7272
const context = new IRContext({
7373
config,
7474
parserConfig,
@@ -78,17 +78,15 @@ export const parseExperimental = ({
7878
switch (context.spec.openapi) {
7979
case '3.0.3':
8080
parseV3_0_3(context as IRContext<OpenApiV3_0_3>);
81-
break;
81+
return context;
8282
case '3.1.0':
8383
parseV3_1_0(context as IRContext<OpenApiV3_1_0>);
84-
break;
84+
return context;
8585
default:
8686
// TODO: parser - uncomment after removing legacy parser.
8787
// For now, we fall back to legacy parser if spec version
8888
// is not supported
89-
break;
90-
// throw new Error('Unsupported OpenAPI specification');
89+
// throw new Error('Unsupported OpenAPI specification');
90+
return;
9191
}
92-
93-
return context;
9492
};

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from '../../../generate/services';
1818
import { relativeModulePath } from '../../../generate/utils';
1919
import type { IROperationObject } from '../../../ir/ir';
20+
import { paginationKeywordsRegExp } from '../../../ir/pagination';
2021
import { isOperationParameterRequired } from '../../../openApi';
2122
import { getOperationKey } from '../../../openApi/common/parser/operation';
2223
import type {
@@ -101,8 +102,6 @@ const getPaginationIn = (parameter: OperationParameter) => {
101102
}
102103
};
103104

104-
const paginationWordsRegExp = /^(cursor|offset|page|start)/;
105-
106105
const createInfiniteParamsFn = 'createInfiniteParams';
107106
const createQueryKeyFn = 'createQueryKey';
108107
const infiniteQueryOptionsFn = 'infiniteQueryOptions';
@@ -903,8 +902,8 @@ export const handlerLegacy: PluginLegacyHandler<
903902
let paginationField!: Model | OperationParameter;
904903

905904
const paginationParameter = operation.parameters.find((parameter) => {
906-
paginationWordsRegExp.lastIndex = 0;
907-
if (paginationWordsRegExp.test(parameter.name)) {
905+
paginationKeywordsRegExp.lastIndex = 0;
906+
if (paginationKeywordsRegExp.test(parameter.name)) {
908907
paginationField = parameter;
909908
return true;
910909
}
@@ -919,17 +918,17 @@ export const handlerLegacy: PluginLegacyHandler<
919918
(model) => model.meta?.$ref === ref,
920919
);
921920
return refModel?.properties.find((property) => {
922-
paginationWordsRegExp.lastIndex = 0;
923-
if (paginationWordsRegExp.test(property.name)) {
921+
paginationKeywordsRegExp.lastIndex = 0;
922+
if (paginationKeywordsRegExp.test(property.name)) {
924923
paginationField = property;
925924
return true;
926925
}
927926
});
928927
}
929928

930929
return parameter.properties.find((property) => {
931-
paginationWordsRegExp.lastIndex = 0;
932-
if (paginationWordsRegExp.test(property.name)) {
930+
paginationKeywordsRegExp.lastIndex = 0;
931+
if (paginationKeywordsRegExp.test(property.name)) {
933932
paginationField = property;
934933
return true;
935934
}

packages/openapi-ts/test/sample.cjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const main = async () => {
1111
// debug: true,
1212
experimental_parser: true,
1313
// input: './test/spec/v3.json',
14-
input: './test/spec/3.1.0/full.json',
15-
// input: './test/spec/v2.json',
14+
// input: './test/spec/3.1.0/full.json',
15+
input: './test/spec/tanstack.json',
1616
// input: 'https://mongodb-mms-prod-build-server.s3.amazonaws.com/openapi/2caffd88277a4e27c95dcefc7e3b6a63a3b03297-v2-2023-11-15.json',
1717
// name: 'foo',
1818
output: {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"paths": {
8+
"/api/search": {
9+
"post": {
10+
"operationId": "search",
11+
"requestBody": {
12+
"content": {
13+
"application/json": {
14+
"schema": {
15+
"$ref": "#/components/schemas/SearchRequest"
16+
}
17+
}
18+
}
19+
}
20+
}
21+
}
22+
},
23+
"components": {
24+
"schemas": {
25+
"SearchParamsRequest": {
26+
"required": ["contractIds", "contractNumbers"],
27+
"type": "object",
28+
"properties": {
29+
"contractIds": {
30+
"type": "array",
31+
"items": {
32+
"type": "string",
33+
"format": "uuid"
34+
}
35+
},
36+
"contractNumbers": {
37+
"type": "array",
38+
"items": {
39+
"type": "string"
40+
}
41+
}
42+
}
43+
},
44+
"SearchRequest": {
45+
"required": ["searchParameters"],
46+
"type": "object",
47+
"properties": {
48+
"searchParameters": {
49+
"$ref": "#/components/schemas/SearchParamsRequest"
50+
},
51+
"pageParameters": {
52+
"$ref": "#/components/schemas/PageParameters"
53+
}
54+
}
55+
},
56+
"PageParameters": {
57+
"type": "object",
58+
"properties": {
59+
"offset": {
60+
"type": "integer",
61+
"format": "int32"
62+
},
63+
"limit": {
64+
"type": "integer",
65+
"format": "int32"
66+
}
67+
}
68+
}
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)