Skip to content

Commit a635e37

Browse files
authored
support string values for object type parameters (#1321)
1 parent a6b975f commit a635e37

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/execute/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,15 @@ export function buildRequest(options) {
210210
throw new Error(`Required parameter ${parameter.name} is not provided`)
211211
}
212212

213+
if (specIsOAS3 && parameter.schema && parameter.schema.type === 'object' && typeof value === 'string') {
214+
try {
215+
value = JSON.parse(value)
216+
}
217+
catch (e) {
218+
throw new Error('Could not parse object parameter value string as JSON')
219+
}
220+
}
221+
213222
if (builder) {
214223
builder({req, parameter, value, operation, spec})
215224
}

test/oas3/execute/style-explode/query.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,45 @@ describe('OAS 3.0 - buildRequest w/ `style` & `explode` - query parameters', fun
870870
})
871871
})
872872

873+
it('should handle building a query parameter in form/explode format, with a stringified object provided, if schema type is indicated', function () {
874+
// Given
875+
const spec = {
876+
openapi: '3.0.0',
877+
paths: {
878+
'/users': {
879+
get: {
880+
operationId: 'myOperation',
881+
parameters: [
882+
{
883+
name: 'id',
884+
in: 'query',
885+
schema: {
886+
type: 'object'
887+
}
888+
}
889+
]
890+
}
891+
}
892+
}
893+
}
894+
895+
// when
896+
const req = buildRequest({
897+
spec,
898+
operationId: 'myOperation',
899+
parameters: {
900+
id: JSON.stringify(VALUE)
901+
}
902+
})
903+
904+
expect(req).toEqual({
905+
method: 'GET',
906+
url: `/users?role=admin&firstName=Alex&greeting=${SAFE_INPUT_RESULT}`,
907+
credentials: 'same-origin',
908+
headers: {},
909+
})
910+
})
911+
873912
it('should build a query parameter with escaped non-RFC3986 characters', function () {
874913
// Given
875914
const spec = {

0 commit comments

Comments
 (0)