Skip to content

Commit 672390b

Browse files
authored
OAS3 binary media type support (#1324)
* support a wider range of form-bearing media types * supporting changes for OAS3 binary request bodies * satisfy linter
1 parent 7ba17ef commit 672390b

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/execute/oas3/build-request.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,28 @@ export default function (options, req) {
4949
if (requestBodyMediaTypes.indexOf(requestContentType) > -1) {
5050
// only attach body if the requestBody has a definition for the
5151
// contentType that has been explicitly set
52-
if (requestContentType === 'application/x-www-form-urlencoded') {
52+
if (requestContentType === 'application/x-www-form-urlencoded' || requestContentType.indexOf('multipart/') === 0) {
5353
if (typeof requestBody === 'object') {
5454
req.form = {}
5555
Object.keys(requestBody).forEach((k) => {
5656
const val = requestBody[k]
5757
let newVal
5858

59-
if (typeof val === 'object') {
59+
let isFile
60+
61+
if (typeof File !== 'undefined') {
62+
isFile = val instanceof File // eslint-disable-line no-undef
63+
}
64+
65+
if (typeof Blob !== 'undefined') {
66+
isFile = isFile || val instanceof Blob // eslint-disable-line no-undef
67+
}
68+
69+
if (typeof Buffer !== 'undefined') {
70+
isFile = isFile || val instanceof Buffer
71+
}
72+
73+
if (typeof val === 'object' && !isFile) {
6074
if (Array.isArray(val)) {
6175
newVal = val.toString()
6276
}

test/oas3/execute/main.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,4 +911,46 @@ describe('buildRequest - OpenAPI Specification 3.0', function () {
911911
})
912912
})
913913
})
914+
describe('special media types', function () {
915+
describe('file-as-body types', function () {
916+
it('should preserve blobs for application/octet-stream', () => {
917+
const spec = {
918+
openapi: '3.0.0',
919+
paths: {
920+
'/one': {
921+
get: {
922+
operationId: 'getMe',
923+
requestBody: {
924+
content: {
925+
'application/octet-stream': {
926+
schema: {
927+
type: 'string',
928+
format: 'binary'
929+
}
930+
}
931+
}
932+
}
933+
}
934+
}
935+
}
936+
}
937+
938+
// when
939+
const req = buildRequest({
940+
spec,
941+
operationId: 'getMe',
942+
requestBody: Buffer.from('this is a test')
943+
})
944+
945+
expect(req).toInclude({
946+
method: 'GET',
947+
url: '/one',
948+
credentials: 'same-origin',
949+
headers: {},
950+
})
951+
952+
expect(req.body.toString('base64')).toEqual('dGhpcyBpcyBhIHRlc3Q=')
953+
})
954+
})
955+
})
914956
})

0 commit comments

Comments
 (0)