Skip to content

Commit ba91bf2

Browse files
Kyle Shockeyshockey
authored andcommitted
Query parameters... working
1 parent 5b41033 commit ba91bf2

File tree

4 files changed

+105
-12
lines changed

4 files changed

+105
-12
lines changed

src/execute/oas3/parameter-builders.js

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import stylize from './style-serializer'
22

33
export default {
44
path,
5-
query
5+
query,
6+
header
67
}
78

89
function path({req, value, parameter}) {
@@ -29,14 +30,52 @@ function query({req, value, parameter}) {
2930
}
3031

3132
if (value) {
32-
req.query[parameter.name] = {
33-
value: stylize({
34-
key: parameter.name,
35-
value,
36-
style: parameter.style || 'form',
37-
explode: typeof parameter.explode === 'undefined' ? true : parameter.explode
38-
}),
39-
skipEncoding: true
33+
const type = typeof value
34+
35+
if (parameter.style === 'deepObject') {
36+
const valueKeys = Object.keys(value)
37+
valueKeys.forEach(k => {
38+
const v = value[k]
39+
req.query[`${parameter.name}[${k}]`] = {
40+
value: stylize({
41+
key: k,
42+
value: v,
43+
style: 'deepObject'
44+
}),
45+
skipEncoding: true
46+
}
47+
})
48+
}
49+
else if (
50+
type === "object" &&
51+
!Array.isArray(value) &&
52+
(parameter.style === 'form' || !parameter.style) &&
53+
(parameter.explode || parameter.explode === undefined)
54+
) {
55+
// form explode needs to be handled here,
56+
// since we aren't assigning to `req.query[parameter.name]`
57+
// like we usually do.
58+
const valueKeys = Object.keys(value)
59+
valueKeys.forEach(k => {
60+
const v = value[k]
61+
req.query[k] = {
62+
value: stylize({
63+
key: k,
64+
value: v,
65+
style: parameter.style || 'form'
66+
})
67+
}
68+
})
69+
} else {
70+
req.query[parameter.name] = {
71+
value: stylize({
72+
key: parameter.name,
73+
value,
74+
style: parameter.style || 'form',
75+
explode: typeof parameter.explode === 'undefined' ? true : parameter.explode
76+
}),
77+
skipEncoding: true
78+
}
4079
}
4180
}
4281
else if (parameter.allowEmptyValue) {
@@ -45,3 +84,15 @@ function query({req, value, parameter}) {
4584
req.query[paramName].allowEmptyValue = true
4685
}
4786
}
87+
88+
function header({req, parameter, value}) {
89+
req.headers = req.headers || {}
90+
if (typeof value !== 'undefined') {
91+
req.headers[parameter.name] = stylize({
92+
key: parameter.name,
93+
value,
94+
style: parameter.style || 'simple',
95+
explode: typeof parameter.explode || false
96+
})
97+
}
98+
}

src/execute/oas3/style-serializer.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ function encodeObject({key, value, style, explode}) {
9292
if (style === 'form') {
9393
return valueKeys.reduce((prev, curr) => {
9494
const val = value[curr]
95-
const prefix = prev ? `${prev}&` : ''
95+
const prefix = prev ? `${prev},` : ``
9696

97-
return `${prefix}${curr}=${val}`
97+
return `${prefix}${curr},${val}`
9898
}, '')
9999
}
100100
}
@@ -115,4 +115,8 @@ function encodePrimitive({key, value, style, explode}) {
115115
if (style === 'form') {
116116
return value
117117
}
118+
119+
if (style === 'deepObject') {
120+
return value
121+
}
118122
}

src/http.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export function encodeFormOrQuery(data) {
181181
const isObject = a => a && typeof a === 'object'
182182
const paramValue = data[parameterName]
183183
const skipEncoding = !!paramValue.skipEncoding
184-
const encodedParameterName = encodeURIComponent(parameterName)
184+
const encodedParameterName = skipEncoding ? parameterName : encodeURIComponent(parameterName)
185185
const notArray = isObject(paramValue) && !Array.isArray(paramValue)
186186
result[encodedParameterName] = formatValue(notArray ? paramValue : {value: paramValue}, skipEncoding)
187187
return result

test/oas3/execute/style-explode.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,44 @@ describe.only('buildRequest w/ `style` & `explode` - OpenAPI Specification 3.0',
12941294
})
12951295
})
12961296

1297+
it('should build a query parameter in form/no-explode format', function () {
1298+
// Given
1299+
const spec = {
1300+
openapi: '3.0.0',
1301+
paths: {
1302+
'/users': {
1303+
get: {
1304+
operationId: 'myOperation',
1305+
parameters: [
1306+
{
1307+
name: 'id',
1308+
in: 'query',
1309+
style: 'form',
1310+
explode: false
1311+
}
1312+
]
1313+
}
1314+
}
1315+
}
1316+
}
1317+
1318+
// when
1319+
const req = buildRequest({
1320+
spec,
1321+
operationId: 'myOperation',
1322+
parameters: {
1323+
id: VALUE
1324+
}
1325+
})
1326+
1327+
expect(req).toEqual({
1328+
method: 'GET',
1329+
url: '/users?id=role,admin,firstName,Alex',
1330+
credentials: 'same-origin',
1331+
headers: {},
1332+
})
1333+
})
1334+
12971335
it('should build a query parameter in deepObject/explode format', function () {
12981336
// Given
12991337
const spec = {

0 commit comments

Comments
 (0)