File tree 4 files changed +36
-1
lines changed
4 files changed +36
-1
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " openapi-fetch " : patch
3
+ ---
4
+
5
+ Fix behavior for empty arrays and objects in default ` querySerializer `
Original file line number Diff line number Diff line change @@ -305,7 +305,7 @@ export function defaultQuerySerializer<T = unknown>(q: T): string {
305
305
if ( q && typeof q === "object" ) {
306
306
for ( const [ k , v ] of Object . entries ( q ) ) {
307
307
const value = defaultQueryParamSerializer ( [ k ] , v ) ;
308
- if ( value !== undefined ) {
308
+ if ( value ) {
309
309
search . push ( value ) ;
310
310
}
311
311
}
@@ -328,6 +328,9 @@ export function defaultQueryParamSerializer<T = unknown>(
328
328
return `${ deepObjectPath ( key ) } =${ String ( value ) } ` ;
329
329
}
330
330
if ( Array . isArray ( value ) ) {
331
+ if ( ! value . length ) {
332
+ return undefined ;
333
+ }
331
334
const nextValue : string [ ] = [ ] ;
332
335
for ( const item of value ) {
333
336
const next = defaultQueryParamSerializer ( key , item ) ;
@@ -338,6 +341,9 @@ export function defaultQueryParamSerializer<T = unknown>(
338
341
return nextValue . join ( `&` ) ;
339
342
}
340
343
if ( typeof value === "object" ) {
344
+ if ( ! Object . keys ( value ) . length ) {
345
+ return undefined ;
346
+ }
341
347
const nextValue : string [ ] = [ ] ;
342
348
for ( const [ k , v ] of Object . entries ( value ) ) {
343
349
if ( v !== undefined && v !== null ) {
Original file line number Diff line number Diff line change @@ -185,6 +185,18 @@ describe("client", () => {
185
185
) ;
186
186
} ) ;
187
187
188
+ it ( "array params (empty)" , async ( ) => {
189
+ const client = createClient < paths > ( ) ;
190
+ mockFetchOnce ( { status : 200 , body : "{}" } ) ;
191
+ await client . GET ( "/query-params" , {
192
+ params : {
193
+ query : { array : [ ] } ,
194
+ } ,
195
+ } ) ;
196
+
197
+ expect ( fetchMocker . mock . calls [ 0 ] [ 0 ] ) . toBe ( "/query-params" ) ;
198
+ } ) ;
199
+
188
200
it ( "object params" , async ( ) => {
189
201
const client = createClient < paths > ( ) ;
190
202
mockFetchOnce ( { status : 200 , body : "{}" } ) ;
Original file line number Diff line number Diff line change @@ -194,6 +194,18 @@ describe("client", () => {
194
194
) ;
195
195
} ) ;
196
196
197
+ it ( "array params (empty)" , async ( ) => {
198
+ const client = createClient < paths > ( ) ;
199
+ mockFetchOnce ( { status : 200 , body : "{}" } ) ;
200
+ await client . GET ( "/query-params" , {
201
+ params : {
202
+ query : { array : [ ] } ,
203
+ } ,
204
+ } ) ;
205
+
206
+ expect ( fetchMocker . mock . calls [ 0 ] [ 0 ] ) . toBe ( "/query-params" ) ;
207
+ } ) ;
208
+
197
209
it ( "object params" , async ( ) => {
198
210
const client = createClient < paths > ( ) ;
199
211
mockFetchOnce ( { status : 200 , body : "{}" } ) ;
You can’t perform that action at this time.
0 commit comments