-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(cli): update controller template to enable filter for findById endpoint #4114
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
Conversation
I also have a question, in https://github.com/strongloop/loopback-next/blob/master/examples/todo-list/src/controllers/todo-list.controller.ts#L80, we use the flag |
573bc4a
to
1cc20c6
Compare
@agnes512 see code in buildSchema When that flag is added, the openapi spec of the response schema will include relational schemas, otherwise the included items in response will be treated as unknown properties :p |
@@ -112,8 +112,11 @@ export class <%= className %>Controller { | |||
}, | |||
}, | |||
}) | |||
async findById(@param.path.<%= idType %>('id') id: <%= idType %>): Promise<<%= modelName %>> { | |||
return this.<%= repositoryNameCamel %>.findById(id); | |||
async findById( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
based on your question in comment, the response schema should also be updated to
content: {
`application/json`: {
schema: getModelSchemaRef(<%= modelName %>, includeRelations),
}
}
@@ -122,8 +122,11 @@ export class ProductReviewController { | |||
}, | |||
}, | |||
}) | |||
async findById(@param.path.number('id') id: number): Promise<ProductReview> { | |||
return this.barRepository.findById(id); | |||
async findById( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -291,8 +294,11 @@ export class ProductReviewController { | |||
}, | |||
}, | |||
}) | |||
async findById(@param.path.number('id') id: number): Promise<ProductReview> { | |||
return this.barRepository.findById(id); | |||
async findById( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great initiative! The changes look mostly good, I have one comment to consider.
schema: {type: 'array', items: getModelSchemaRef(<%= modelName %>)}, | ||
schema: { | ||
type: 'array', | ||
items: getModelSchemaRef(<%= modelName %>, {includeRelations: true}), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
return this.<%= repositoryNameCamel %>.findById(id); | ||
async findById( | ||
@param.path.<%= idType %>('id') id: <%= idType %>, | ||
@param.query.object('filter', getFilterSchemaFor(<%= modelName %>)) filter?: Filter<<%= modelName %>> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we support all filter properties in findById
? For example, I find it weird to combine findById
with where
or limit
/skip
filter properties, because findById
always returns a single record matching the given id only. Should we perhaps describe only filter.include
and filter.fields
properties in the schema?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See also #3336 Filter on findById is ignoring the where clause.
I am concerned that if we describe filter
parameter of findById
method as accepting all filter properties, then our users will try to set those (unsupported) properties and get surprised/confused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏 LGTM
9a7913b
to
efb2e16
Compare
efb2e16
to
cc59367
Compare
As LB4 supports inclusion, I think it's fair to allow users to traverse related models with
findById()
endpoint with CLI generated controllers.Also, the todoList example enables such filters. I think they should be consistent as we have a note in the site:
Checklist
👉 Read and sign the CLA (Contributor License Agreement) 👈
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated👉 Check out how to submit a PR 👈