Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: spike on API Explorer supporting complex objects in query param #4141

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ describe('TodoListApplication', () => {
});
});

it('exploded filter conditions work', async () => {
const list = await givenTodoListInstance(todoListRepo);
await givenTodoInstance(todoRepo, {title: 'todo1', todoListId: list.id});
await givenTodoInstance(todoRepo, {title: 'todo2', todoListId: list.id});
await givenTodoInstance(todoRepo, {title: 'todo3', todoListId: list.id});

const response = await client.get('/todos').query('filter[limit]=2');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit redundant, because query({filter: {limit: 2}}) is automatically converted by supertest to ?filter[limit]=2. Having said that, I think it's a good idea to have tests that are explicitly verifying different encoding styles, as opposed to the current status, where such encodings are tested implicitly, see:

https://github.com/strongloop/loopback-next/blob/303d3ff6ebe751b238ab16e021f6a25f09f4a1be/examples/todo-list/src/__tests__/acceptance/todo-list.acceptance.ts#L110

https://github.com/strongloop/loopback-next/blob/303d3ff6ebe751b238ab16e021f6a25f09f4a1be/examples/todo-list/src/__tests__/acceptance/todo-list.acceptance.ts#L196-L198

Writing such tests is out of scope of this spike, but please do include a task/todo-item in the acceptance criteria of the story that will track the real implementation.


expect(response.body).to.have.length(2);
});

/*
============================================================================
TEST HELPERS
Expand Down
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
9 changes: 8 additions & 1 deletion 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,
) {
if (!spec.schema && spec.content) {
const content = spec.content;
const jsonSpec = content['application/json'];
spec.schema = jsonSpec.schema;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ok in a spike, but please note that the real implementation must not mutate (modify) the spec object given to us by the caller.

}

const schema = spec.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