Skip to content

Commit

Permalink
fix: suport complex objects for query params in api explorer
Browse files Browse the repository at this point in the history
issue: #2208

BREAKING CHANGE
  • Loading branch information
deepakrkris committed Jan 23, 2020
1 parent 4031c97 commit cd3b772
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,10 @@ describe('Routing metadata for parameters', () => {
const expectedParamSpec = <ParameterObject>{
name: 'filter',
in: 'query',
style: 'deepObject',
explode: true,
schema: {
type: 'object',
additionalProperties: true,
content: {
'application/json': {
schema: {type: 'object', additionalProperties: true},
},
},
};
expectSpecToBeEqual(MyController, expectedParamSpec);
Expand All @@ -256,13 +255,15 @@ describe('Routing metadata for parameters', () => {
const expectedParamSpec: ParameterObject = {
name: 'filter',
in: 'query',
style: 'deepObject',
explode: true,
schema: {
type: 'object',
properties: {
where: {type: 'object', additionalProperties: true},
limit: {type: 'number'},
content: {
'application/json': {
schema: {
type: 'object',
properties: {
where: {type: 'object', additionalProperties: true},
limit: {type: 'number'},
},
},
},
},
};
Expand Down Expand Up @@ -300,11 +301,10 @@ describe('Routing metadata for parameters', () => {
name: 'filter',
in: 'query',
description: 'Search criteria',
style: 'deepObject',
explode: true,
schema: {
type: 'object',
additionalProperties: true,
content: {
'application/json': {
schema: {type: 'object', additionalProperties: true},
},
},
};
expectSpecToBeEqual(MyController, expectedParamSpec);
Expand Down
15 changes: 10 additions & 5 deletions packages/openapi-v3/src/decorators/parameter.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ export function param(paramSpec: ParameterObject) {
// generate schema if `paramSpec` has `schema` but without `type`
(isSchemaObject(paramSpec.schema) && !paramSpec.schema.type)
) {
// please note `resolveSchema` only adds `type` and `format` for `schema`
paramSpec.schema = resolveSchema(paramType, paramSpec.schema);
// If content explicitly mentioned do not resolve schema
if (!paramSpec.content) {
// please note `resolveSchema` only adds `type` and `format` for `schema`
paramSpec.schema = resolveSchema(paramType, paramSpec.schema);
}
}
}

Expand Down Expand Up @@ -212,9 +215,11 @@ export namespace param {
return param({
name,
in: 'query',
style: 'deepObject',
explode: true,
schema,
content: {
'application/json': {
schema,
},
},
...spec,
});
},
Expand Down
11 changes: 9 additions & 2 deletions packages/rest/src/coercion/coerce-parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ export function coerceParameter(
data: string | undefined | object,
spec: ParameterObject,
) {
const schema = spec.schema;
let schema = spec.schema;

if (!schema && spec.content) {
const content = spec.content;
const jsonSpec = content['application/json'];
schema = jsonSpec.schema;
}

if (!schema || isReferenceObject(schema)) {
debug(
'The parameter with schema %s is not coerced since schema' +
Expand Down Expand Up @@ -172,7 +179,7 @@ function parseJsonIfNeeded(
): string | object | undefined {
if (typeof data !== 'string') return data;

if (spec.in !== 'query' || spec.style !== 'deepObject') {
if (spec.in !== 'query' || (spec.style !== 'deepObject' && !spec.content)) {
debug(
'Skipping JSON.parse, argument %s is not in:query style:deepObject',
spec.name,
Expand Down

0 comments on commit cd3b772

Please sign in to comment.