Skip to content

Commit

Permalink
fix(execute): parse stringified objects (#3474)
Browse files Browse the repository at this point in the history
This change is related to Parameter.style="deepObject".

Refs swagger-api/swagger-ui#7734
  • Loading branch information
glowcloud committed Apr 23, 2024
1 parent 8bd661e commit c4ff9a2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/http/index.js
Expand Up @@ -323,6 +323,14 @@ function formatKeyValueBySerializationOption(key, value, skipEncoding, serializa
const encodeFn = (v) => valueEncoder(v, escape);
const encodeKeyFn = skipEncoding ? (k) => k : (k) => encodeFn(k);

if (typeof value === 'string') {
try {
value = JSON.parse(value);
} catch {
// can't parse the value so treat it as as a simple string
}
}

// Primitive
if (typeof value !== 'object') {
return [[encodeKeyFn(key), encodeFn(value)]];
Expand Down
65 changes: 65 additions & 0 deletions test/oas3/execute/main.js
Expand Up @@ -871,6 +871,71 @@ describe('buildRequest - OpenAPI Specification 3.0', () => {
});
});

it('should serialize JSON values provided as objects in `deepObject` style', () => {
const req = buildRequest({
spec: {
openapi: '3.0.0',
paths: {
'/': {
post: {
operationId: 'myOp',
requestBody: {
content: {
'application/x-www-form-urlencoded': {
schema: {
type: 'object',
properties: {
a: {
type: 'object',
properties: {
b: {
type: 'string',
},
c: {
type: 'array',
},
d: {
type: 'object',
},
},
},
},
},
encoding: {
a: {
style: 'deepObject',
},
},
},
},
},
},
},
},
},
operationId: 'myOp',
requestBody: {
a: {
b: 'c',
c: ['d', 'e'],
d: {
e: 'f',
},
},
},
});

expect(req).toEqual({
method: 'POST',
url: `/`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
credentials: 'same-origin',
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',
});
});

it('should serialize JSON values provided as arrays of stringified objects', () => {
const req = buildRequest({
spec: {
Expand Down

0 comments on commit c4ff9a2

Please sign in to comment.