Skip to content

Commit 788039a

Browse files
ponelatbuunguyen
authored andcommitted
stringify request.body in #execute (#1034)
* stringify request.body in #execute and NOT in buildRequest, so as to allow other code to modify the body, before finally getting processed by execute * Add another test for non-objects
1 parent 00fb9cf commit 788039a

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

src/execute.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import assign from 'lodash/assign'
22
import getIn from 'lodash/get'
3+
import isObject from 'lodash/isObject'
34
import btoa from 'btoa'
45
import url from 'url'
56
import http, {mergeInQueryOrForm} from './http'
@@ -47,6 +48,10 @@ export function execute({
4748

4849
const request = self.buildRequest({spec, operationId, parameters, securities, ...extras})
4950

51+
if (request.body && isObject(request.body)) {
52+
request.body = JSON.stringify(request.body)
53+
}
54+
5055
// Build request and execute it
5156
return userHttp(request)
5257
}

test/execute.js

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,28 @@ describe('execute', () => {
663663
}
664664
})
665665
})
666+
667+
it('should NOT stringify the body, if provided with a javascript object (only execute should do that, allowing us to modify the object in a clean way)', function() {
668+
669+
// Given
670+
const spec = {
671+
host: 'swagger.io',
672+
paths: {'/me': {post: {parameters: [{name: 'body', in: 'body'}], operationId: 'makeMe'}}}
673+
}
674+
675+
const req = buildRequest({
676+
spec,
677+
operationId: 'makeMe',
678+
parameters: {
679+
body: {
680+
one: 1,
681+
}
682+
}})
683+
684+
expect(req.body).toEqual({
685+
one: 1
686+
})
687+
})
666688
})
667689

668690
// Note: this is to handle requestContentType and responseContentType
@@ -674,7 +696,7 @@ describe('execute', () => {
674696
paths: {'/one': {get: {operationId: 'getMe'}}}
675697
}
676698

677-
const buildRequestSpy = spyOn(stubs, 'buildRequest')
699+
const buildRequestSpy = spyOn(stubs, 'buildRequest').andReturn({})
678700

679701
execute({
680702
fetch: createSpy().andReturn({then() { }}),
@@ -689,6 +711,55 @@ describe('execute', () => {
689711
})
690712
})
691713

714+
it('should stringify body, if provided with javascript object', function () {
715+
// Given
716+
const spec = {
717+
host: 'swagger.io',
718+
paths: {'/me': {post: {parameters: [{name: 'body', in: 'body'}], operationId: 'makeMe'}}}
719+
}
720+
721+
const fetchSpy = createSpy().andReturn({then() { }})
722+
723+
execute({
724+
fetch: fetchSpy,
725+
spec,
726+
operationId: 'makeMe',
727+
parameters: {
728+
body: {
729+
one: 1,
730+
two: {
731+
three: 3
732+
}
733+
}
734+
}
735+
})
736+
737+
expect(fetchSpy.calls.length).toEqual(1)
738+
expect(fetchSpy.calls[0].arguments[0].body).toEqual('{"one":1,"two":{"three":3}}')
739+
})
740+
741+
it('should NOT stringify body, if its a non-object', function () {
742+
// Given
743+
const spec = {
744+
host: 'swagger.io',
745+
paths: {'/me': {post: {parameters: [{name: 'body', in: 'body'}], operationId: 'makeMe'}}}
746+
}
747+
748+
const fetchSpy = createSpy().andReturn({then() { }})
749+
750+
execute({
751+
fetch: fetchSpy,
752+
spec,
753+
operationId: 'makeMe',
754+
parameters: {
755+
body: 'hello'
756+
}
757+
})
758+
759+
expect(fetchSpy.calls.length).toEqual(1)
760+
expect(fetchSpy.calls[0].arguments[0].body).toEqual('hello')
761+
})
762+
692763
describe('applySecurities', function () {
693764
it('should NOT add any securities, if the operation does not require it', function () {
694765
const spec = {

0 commit comments

Comments
 (0)