Skip to content

Commit

Permalink
Mend ability to serialize primitives in request body object values
Browse files Browse the repository at this point in the history
  • Loading branch information
shockey committed Dec 7, 2017
1 parent e4d5097 commit 917fe74
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,12 @@ function isFile(obj) {
return obj !== null && typeof obj === 'object' && typeof obj.pipe === 'function'
}

function formatValue({value, collectionFormat, allowEmptyValue}, skipEncoding) {
// REVIEW: OAS3: usage of this fn for compatibility w/ new value formats
function formatValue(input, skipEncoding) {
const {collectionFormat, allowEmptyValue} = input

// `input` can be string in OAS3 contexts
const value = typeof input === 'object' ? input.value : input

const SEPARATORS = {
csv: ',',
ssv: '%20',
Expand All @@ -163,9 +167,14 @@ function formatValue({value, collectionFormat, allowEmptyValue}, skipEncoding) {
else encodeFn = obj => JSON.stringify(obj)
}

if (value && !Array.isArray(value)) {
if (typeof value === 'object' && !Array.isArray(value)) {
return ''
}

if (!Array.isArray(value)) {
return encodeFn(value)
}

if (Array.isArray(value) && !collectionFormat) {
return value.map(encodeFn).join(',')
}
Expand Down
Empty file removed test.js
Empty file.
10 changes: 10 additions & 0 deletions test/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ describe('http', () => {
expect(encodeFormOrQuery(req.query)).toEqual('one=1&two=2&three=false')
})

it('should parse a generic object into a query string', function () {
const data = {
one: 1,
two: 'two',
three: false
}

expect(encodeFormOrQuery(data)).toEqual('one=1&two=two&three=false')
})

it('should handle arrays', function () {
const req = {
query: {
Expand Down

0 comments on commit 917fe74

Please sign in to comment.