Skip to content

Commit c4ff9a2

Browse files
authored
fix(execute): parse stringified objects (#3474)
This change is related to Parameter.style="deepObject". Refs swagger-api/swagger-ui#7734
1 parent 8bd661e commit c4ff9a2

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/http/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,14 @@ function formatKeyValueBySerializationOption(key, value, skipEncoding, serializa
323323
const encodeFn = (v) => valueEncoder(v, escape);
324324
const encodeKeyFn = skipEncoding ? (k) => k : (k) => encodeFn(k);
325325

326+
if (typeof value === 'string') {
327+
try {
328+
value = JSON.parse(value);
329+
} catch {
330+
// can't parse the value so treat it as as a simple string
331+
}
332+
}
333+
326334
// Primitive
327335
if (typeof value !== 'object') {
328336
return [[encodeKeyFn(key), encodeFn(value)]];

test/oas3/execute/main.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,71 @@ describe('buildRequest - OpenAPI Specification 3.0', () => {
871871
});
872872
});
873873

874+
it('should serialize JSON values provided as objects in `deepObject` style', () => {
875+
const req = buildRequest({
876+
spec: {
877+
openapi: '3.0.0',
878+
paths: {
879+
'/': {
880+
post: {
881+
operationId: 'myOp',
882+
requestBody: {
883+
content: {
884+
'application/x-www-form-urlencoded': {
885+
schema: {
886+
type: 'object',
887+
properties: {
888+
a: {
889+
type: 'object',
890+
properties: {
891+
b: {
892+
type: 'string',
893+
},
894+
c: {
895+
type: 'array',
896+
},
897+
d: {
898+
type: 'object',
899+
},
900+
},
901+
},
902+
},
903+
},
904+
encoding: {
905+
a: {
906+
style: 'deepObject',
907+
},
908+
},
909+
},
910+
},
911+
},
912+
},
913+
},
914+
},
915+
},
916+
operationId: 'myOp',
917+
requestBody: {
918+
a: {
919+
b: 'c',
920+
c: ['d', 'e'],
921+
d: {
922+
e: 'f',
923+
},
924+
},
925+
},
926+
});
927+
928+
expect(req).toEqual({
929+
method: 'POST',
930+
url: `/`,
931+
headers: {
932+
'Content-Type': 'application/x-www-form-urlencoded',
933+
},
934+
credentials: 'same-origin',
935+
body: 'a%5Bb%5D=c&a%5Bc%5D=%5B%22d%22%2C%22e%22%5D&a%5Bd%5D=%7B%22e%22%3A%22f%22%7D',
936+
});
937+
});
938+
874939
it('should serialize JSON values provided as arrays of stringified objects', () => {
875940
const req = buildRequest({
876941
spec: {

0 commit comments

Comments
 (0)