@@ -20,11 +20,16 @@ import { serialize as serializeCookie } from '../helpers/cookie.js';
20
20
21
21
const arrayOrEmpty = ( ar ) => ( Array . isArray ( ar ) ? ar : [ ] ) ;
22
22
23
- const findObjectSchema = ( schema , { recurse = true , depth = 1 } = { } ) => {
23
+ const findObjectOrArraySchema = ( schema , { recurse = true , depth = 1 } = { } ) => {
24
24
if ( ! isPlainObject ( schema ) ) return undefined ;
25
25
26
- // check if the schema is an object type
27
- if ( schema . type === 'object' || ( Array . isArray ( schema . type ) && schema . type . includes ( 'object' ) ) ) {
26
+ // check if the schema is an object or array type
27
+ if (
28
+ schema . type === 'object' ||
29
+ schema . type === 'array' ||
30
+ ( Array . isArray ( schema . type ) &&
31
+ ( schema . type . includes ( 'object' ) || schema . type . includes ( 'array' ) ) )
32
+ ) {
28
33
return schema ;
29
34
}
30
35
@@ -33,14 +38,18 @@ const findObjectSchema = (schema, { recurse = true, depth = 1 } = {}) => {
33
38
if ( recurse ) {
34
39
// traverse oneOf keyword first
35
40
const oneOfResult = Array . isArray ( schema . oneOf )
36
- ? schema . oneOf . find ( ( subschema ) => findObjectSchema ( subschema , { recurse, depth : depth + 1 } ) )
41
+ ? schema . oneOf . find ( ( subschema ) =>
42
+ findObjectOrArraySchema ( subschema , { recurse, depth : depth + 1 } )
43
+ )
37
44
: undefined ;
38
45
39
46
if ( oneOfResult ) return oneOfResult ;
40
47
41
48
// traverse anyOf keyword second
42
49
const anyOfResult = Array . isArray ( schema . anyOf )
43
- ? schema . anyOf . find ( ( subschema ) => findObjectSchema ( subschema , { recurse, depth : depth + 1 } ) )
50
+ ? schema . anyOf . find ( ( subschema ) =>
51
+ findObjectOrArraySchema ( subschema , { recurse, depth : depth + 1 } )
52
+ )
44
53
: undefined ;
45
54
46
55
if ( anyOfResult ) return anyOfResult ;
@@ -49,18 +58,18 @@ const findObjectSchema = (schema, { recurse = true, depth = 1 } = {}) => {
49
58
return undefined ;
50
59
} ;
51
60
52
- const parseJsonObject = ( { value, silentFail = false } ) => {
61
+ const parseJsonObjectOrArray = ( { value, silentFail = false } ) => {
53
62
try {
54
63
const parsedValue = JSON . parse ( value ) ;
55
- if ( typeof parsedValue === 'object' ) {
64
+ if ( isPlainObject ( parsedValue ) || Array . isArray ( parsedValue ) ) {
56
65
return parsedValue ;
57
66
}
58
67
if ( ! silentFail ) {
59
- throw new Error ( 'Expected JSON serialized object' ) ;
68
+ throw new Error ( 'Expected JSON serialized object or array ' ) ;
60
69
}
61
70
} catch {
62
71
if ( ! silentFail ) {
63
- throw new Error ( 'Could not parse object parameter value string as JSON Object' ) ;
72
+ throw new Error ( 'Could not parse parameter value string as JSON Object or JSON Array ' ) ;
64
73
}
65
74
}
66
75
return value ;
@@ -303,10 +312,23 @@ export function buildRequest(options) {
303
312
}
304
313
305
314
if ( specIsOAS3 && typeof value === 'string' ) {
306
- if ( has ( 'type' , parameter . schema ) && findObjectSchema ( parameter . schema , { recurse : false } ) ) {
307
- value = parseJsonObject ( { value, silentFail : false } ) ;
308
- } else if ( findObjectSchema ( parameter . schema , { recurse : true } ) ) {
309
- value = parseJsonObject ( { value, silentFail : true } ) ;
315
+ if (
316
+ has ( 'type' , parameter . schema ) &&
317
+ typeof parameter . schema . type === 'string' &&
318
+ findObjectOrArraySchema ( parameter . schema , { recurse : false } )
319
+ ) {
320
+ value = parseJsonObjectOrArray ( { value, silentFail : false } ) ;
321
+ } else if (
322
+ has ( 'type' , parameter . schema ) &&
323
+ Array . isArray ( parameter . schema . type ) &&
324
+ findObjectOrArraySchema ( parameter . schema , { recurse : false } )
325
+ ) {
326
+ value = parseJsonObjectOrArray ( { value, silentFail : true } ) ;
327
+ } else if (
328
+ ! has ( 'type' , parameter . schema ) &&
329
+ findObjectOrArraySchema ( parameter . schema , { recurse : true } )
330
+ ) {
331
+ value = parseJsonObjectOrArray ( { value, silentFail : true } ) ;
310
332
}
311
333
}
312
334
0 commit comments