Skip to content

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

Merged
merged 1 commit into from
Nov 14, 2019

Conversation

agnes512
Copy link
Contributor

@agnes512 agnes512 commented Nov 12, 2019

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:

This page was generated from the loopback-next/examples/todo-list/README.md

Checklist

👉 Read and sign the CLA (Contributor License Agreement) 👈

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

👉 Check out how to submit a PR 👈

@agnes512 agnes512 marked this pull request as ready for review November 12, 2019 19:59
@agnes512
Copy link
Contributor Author

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 includeRelations. @nabdelgadir told me that this is for openAPI spec. But I don't see any documentation for it. Do we want to add it somewhere?

@agnes512 agnes512 force-pushed the update-controller-template branch from 573bc4a to 1cc20c6 Compare November 12, 2019 20:08
@jannyHou
Copy link
Contributor

@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(
Copy link
Contributor

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(
Copy link
Contributor

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(
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

@bajtos bajtos left a 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}),
Copy link
Member

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 %>>
Copy link
Member

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?

Copy link
Member

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.

Copy link
Contributor Author

@agnes512 agnes512 Nov 14, 2019

Choose a reason for hiding this comment

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

@bajtos I think findById only works for inclusion atm.

And I agree that the filter for findById should work for inclusion and field as you described in #1721. Order, limit, where, etc. don't quite make sense to me.

I'd like to land this PR first, then we can limit it in #1721 to fix #3336.

Copy link
Contributor

@jannyHou jannyHou left a comment

Choose a reason for hiding this comment

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

👏 LGTM

@bajtos
Copy link
Member

bajtos commented Nov 14, 2019

@agnes512 I think this pull request is implementing some parts of #1721, could you please check and update that issue to make it clear what remains to be done?

@agnes512 agnes512 force-pushed the update-controller-template branch from efb2e16 to cc59367 Compare November 14, 2019 19:17
@agnes512 agnes512 merged commit b0ce84b into master Nov 14, 2019
@agnes512 agnes512 deleted the update-controller-template branch November 14, 2019 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants