Skip to content

Commit 9155874

Browse files
authored
Merge pull request #1200 from shockey/master
Mend ability to serialize primitives in request body object values
2 parents 697166f + 3fc436d commit 9155874

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/http.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,12 @@ function isFile(obj) {
140140
return obj !== null && typeof obj === 'object' && typeof obj.pipe === 'function'
141141
}
142142

143-
function formatValue({value, collectionFormat, allowEmptyValue}, skipEncoding) {
144-
// REVIEW: OAS3: usage of this fn for compatibility w/ new value formats
143+
function formatValue(input, skipEncoding) {
144+
const {collectionFormat, allowEmptyValue} = input
145+
146+
// `input` can be string in OAS3 contexts
147+
const value = typeof input === 'object' ? input.value : input
148+
145149
const SEPARATORS = {
146150
csv: ',',
147151
ssv: '%20',
@@ -163,9 +167,14 @@ function formatValue({value, collectionFormat, allowEmptyValue}, skipEncoding) {
163167
else encodeFn = obj => JSON.stringify(obj)
164168
}
165169

166-
if (value && !Array.isArray(value)) {
170+
if (typeof value === 'object' && !Array.isArray(value)) {
171+
return ''
172+
}
173+
174+
if (!Array.isArray(value)) {
167175
return encodeFn(value)
168176
}
177+
169178
if (Array.isArray(value) && !collectionFormat) {
170179
return value.map(encodeFn).join(',')
171180
}

test.js

Whitespace-only changes.

test/http.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,16 @@ describe('http', () => {
221221
expect(encodeFormOrQuery(req.query)).toEqual('one=1&two=2&three=false')
222222
})
223223

224+
it('should parse a generic object into a query string', function () {
225+
const data = {
226+
one: 1,
227+
two: 'two',
228+
three: false
229+
}
230+
231+
expect(encodeFormOrQuery(data)).toEqual('one=1&two=two&three=false')
232+
})
233+
224234
it('should handle arrays', function () {
225235
const req = {
226236
query: {

0 commit comments

Comments
 (0)