Skip to content

Commit 1626b34

Browse files
authored
fix(fetch): respect OpenAPI 3.0 explode default value for query params (#2143)
* fix(fetch): respect OpenAPI 3.0 explode default value for query params * feat: add override.fetch.explode option for backward compatibility * fix: make includeHttpResponseReturnType optional in FetchOptions * docs: move the document to output.md
1 parent f3b9204 commit 1626b34

File tree

6 files changed

+21
-6
lines changed

6 files changed

+21
-6
lines changed

docs/src/pages/reference/configuration/output.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,15 @@ Default: `true`
960960
When using `fetch` for `client` or `httpClient`, the `fetch` response type includes http status for easier processing by the application.
961961
If you want to return a defined return type instead of an automatically generated return type, set this value to `false`.
962962

963+
##### explode
964+
965+
Type: `Boolean`.
966+
Default: `false`
967+
968+
By default, the `fetch` client follows the OpenAPI specification for query parameter explode behavior. This means that query parameters will be exploded unless explicitly set to `false` in the OpenAPI schema.
969+
970+
If you want to maintain backward compatibility with the previous behavior (where only parameters with `explode: true` are exploded), you can set this value to `true`.
971+
963972
#### query
964973

965974
Type: `Object`.

packages/core/src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,8 @@ export type SwrOptions = {
622622
};
623623

624624
export type FetchOptions = {
625-
includeHttpResponseReturnType: boolean;
625+
includeHttpResponseReturnType?: boolean;
626+
explode?: boolean;
626627
};
627628

628629
export type InputTransformerFn = (spec: OpenAPIObject) => OpenAPIObject;

packages/fetch/src/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ export const generateRequestFunction = (
6060
const explodeParameters = parameters.filter((parameter) => {
6161
const { schema } = resolveRef<ParameterObject>(parameter, context);
6262

63-
return schema.in === 'query' && schema.explode;
63+
if (override.fetch.explode) {
64+
return schema.in === 'query' && schema.explode;
65+
} else {
66+
return schema.in === 'query' && schema.explode !== false;
67+
}
6468
});
6569

6670
const explodeParametersNames = explodeParameters.map((parameter) => {
@@ -114,7 +118,7 @@ ${
114118

115119
const isNdJson = response.contentTypes.some(isContentTypeNdJson);
116120
const responseTypeName = fetchResponseTypeName(
117-
override.fetch.includeHttpResponseReturnType,
121+
override.fetch?.includeHttpResponseReturnType,
118122
isNdJson ? 'Response' : response.definition.success,
119123
operationName,
120124
);
@@ -276,7 +280,7 @@ export type ${responseTypeName} = ${compositeName} & {
276280
};
277281

278282
export const fetchResponseTypeName = (
279-
includeHttpResponseReturnType: boolean,
283+
includeHttpResponseReturnType: boolean | undefined,
280284
definitionSuccessResponse: string,
281285
operationName: string,
282286
) => {

packages/orval/src/utils/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ export const normalizeOptions = async (
353353
includeHttpResponseReturnType:
354354
outputOptions.override?.fetch?.includeHttpResponseReturnType ??
355355
true,
356+
explode: outputOptions.override?.fetch?.explode ?? false,
356357
...(outputOptions.override?.fetch ?? {}),
357358
},
358359
useDates: outputOptions.override?.useDates || false,

packages/swr/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ export const getSwrMutationFetcherOptionType = (
236236
export const getSwrMutationFetcherType = (
237237
response: GetterResponse,
238238
httpClient: OutputHttpClient,
239-
includeHttpResponseReturnType: boolean,
239+
includeHttpResponseReturnType: boolean | undefined,
240240
operationName: string,
241241
mutator?: GeneratorMutator,
242242
) => {

packages/swr/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ export const ${swrKeyFnName} = (${queryKeyProps}) => [\`${route}\`${
533533
const swrMutationFetcherType = getSwrMutationFetcherType(
534534
response,
535535
httpClient,
536-
override.fetch.includeHttpResponseReturnType,
536+
override.fetch?.includeHttpResponseReturnType,
537537
operationName,
538538
mutator,
539539
);

0 commit comments

Comments
 (0)