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(rest): implement query parameter validation (issue 1573) #2307

Closed
wants to merge 41 commits into from

Conversation

YaelGit
Copy link

@YaelGit YaelGit commented Jan 30, 2019

Implements #1573

Checklist

  • 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

openapi spec allows developers to provide additional restrictions on input parameters via parameter
schema. For example, pageSize can include schema restrictions

fix loopbackio#1573
openapi spec allows developers to provide additional restrictions on input parameters via parameter
schema. For example, pageSize can include schema restrictions

fix loopbackio#1573
@YaelGit
Copy link
Author

YaelGit commented Jan 30, 2019

@bajtos - Please note I pushed my changes to validate query parameters

@raymondfeng raymondfeng changed the title Issue 1573 feat(rest): implement query parameter validation (issue 1573) Jan 30, 2019
operationSpec,
request,
);

Copy link
Contributor

Choose a reason for hiding this comment

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

This is suspicious. Why do we parse the request body again into query?

* @param globalSchemas The referenced schemas generated from `OpenAPISpec.components.schemas`.
*/
export function validateRequestQuery(
query: RequestBody,
Copy link
Contributor

Choose a reason for hiding this comment

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

If we want to reuse RequestBody for query, we should rename the type as something like ValueWithSchema.

Copy link
Author

Choose a reason for hiding this comment

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

@raymondfeng - do you mean we should duplicate the type requestBody in types.ts and then use this new type (ValueWithSchema) in request-query.validator.ts?
should we also duplicate the BodyParser, BodyParserFunction? and then duplicate the code in body-parser.ts?

Copy link
Contributor

Choose a reason for hiding this comment

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

No duplication. What I meant is to refactor RequestBody to ValueWithSchema into its own file and use it for both body and query validation.

);

return ajv.compile(schemaWithRef);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

There seem to be quite a bit duplicate code as request-body-validator. Can we refactor the code for better reuse?

Copy link
Author

Choose a reason for hiding this comment

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

@raymondfeng -Thanks for the review and comments.
I will try to reply: I was actually looking to differentiate the 2 in order to allow/take into consideration future changes and needs-do you think this is un-needed? will the only difference be - the extraction of the query params and the structure to be sent to same validation function to leverage the AJV ? we need not forget, we also need different error messages. Please share your thoughts.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would create a utility function that validates ValueWithSchema using AJV.

Copy link
Contributor

@raymondfeng raymondfeng left a comment

Choose a reason for hiding this comment

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

@YaelGit Thank you for the contribution. I left a few comments for you to consider.

raymondfeng and others added 5 commits January 30, 2019 13:35
This change greatly simplifies our build and project setup.

- Source files are compiled from `src/{foo}` to `dist/{foo}`, the same
  pattern is applied to test files too.

- Both TypeScript sources and JavaScript output are stored in the same
  path relative to project root. This makes it much easier to refer
  to test fixtures.

This change is also enabling future improvements, for example TypeScript
project references and migration to jest test runner.
new HttpErrors.BadRequest('Request query is required'),
{
code: 'MISSING_REQUIRED_PARAMETER',
parameterName: 'request query',
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to see which query param.

Copy link
Author

Choose a reason for hiding this comment

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

is this sufficient?
query_validation_errors

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure.

raymondfeng and others added 12 commits January 31, 2019 09:51
- Source files are compiled from `src/{foo}` to `dist/{foo}`, the same
  pattern is applied to test files too.

- Both TypeScript sources and JavaScript output are stored in the same
  path relative to project root. This makes it much easier to refer
  to test fixtures.

This is a follow-up for 066d525.
Allow the user to specify a custom Repository class to inherit from.
CLI supports custom repository name
* via an interactive prompt
* via CLI options
* via JSON config

Two tests modified to use the new parameter to pass
Modified tests:
* generates a kv repository from default model
* generates a kv repository from decorator defined model
The greenkeeper.json is now automatically updated as part of
`npm install` based on lerna packages.
- Source files are compiled from `src/{foo}` to `dist/{foo}`, the same
  pattern is applied to test files too.

- Both TypeScript sources and JavaScript output are stored in the same
  path relative to project root. This makes it much easier to refer
  to test fixtures.

This is a follow-up for 066d525 and 91a37dc.
@bajtos
Copy link
Member

bajtos commented Feb 5, 2019

Conflicting files

packages/rest/test/unit/coercion/paramObject.unit.ts

In #2330, we have moved test files from test to src/__tests__. Please run follow the steps in How to rebase your branch to resolve the merge conflict. Please do not run git merge, it's important to use git rebase master.

Sorry for the inconvenience.

@bajtos bajtos self-assigned this Feb 8, 2019
@bajtos bajtos added OpenAPI REST Issues related to @loopback/rest package and REST transport in general labels Feb 8, 2019
@bajtos
Copy link
Member

bajtos commented Feb 8, 2019

@YaelGit ping 👋

What's the status of this pull request? Are you still keen to get it finished?

Merge branch 'master' into issue-1573

Please note we are avoiding merge commits to keep our git history neatly linear. We use git rebase instead, see How to rebase your branch.

query.value = queryValue;
globalSchemas = {properties: schemasValue};
query.schema = globalSchemas;
validateRequestQuery(query, operationSpec.requestBody, globalSchemas);
Copy link
Member

Choose a reason for hiding this comment

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

IIUC, you are building an object with all parameters from the query string, and then validate this object in one pass. I have two reservations about this approach:

  • It seems overly complicated to me.
  • There are more parameter sources besides query and body, we need to validate parameters from all sources (e.g. path and header).

Have you considered to call ajv validator repeatedly, once for each parameter?

Copy link
Author

Choose a reason for hiding this comment

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

Hello @bajtos ,
sorry for the delay - yes i do plan to close the issue soon and in regards to your comments:

  1. I will do the rebase as requested.
  2. Regarding the validation of all parameters all together - i did it in this way due to following same validation standard done for body parameters.
  3. Regrading the request for handling other parameter types, i think a new issue should be opened for other parameter types as the initial issue was for query only.

bajtos and others added 7 commits February 8, 2019 07:55
 - @loopback/benchmark@1.1.10
 - @loopback/docs@1.7.3
 - @loopback/example-hello-world@1.1.3
 - @loopback/example-log-extension@1.1.3
 - @loopback/example-rpc-server@1.1.2
 - @loopback/example-soap-calculator@1.3.3
 - @loopback/example-todo-list@1.4.3
 - @loopback/example-todo@1.4.3
 - @loopback/authentication@1.0.12
 - @loopback/boot@1.0.12
 - @loopback/build@1.3.0
 - @loopback/cli@1.6.0
 - @loopback/context@1.5.1
 - @loopback/core@1.1.6
 - @loopback/http-caching-proxy@1.0.6
 - @loopback/http-server@1.1.5
 - @loopback/metadata@1.0.6
 - @loopback/openapi-spec-builder@1.0.6
 - @loopback/openapi-v3-types@1.0.6
 - @loopback/openapi-v3@1.2.1
 - @loopback/repository-json-schema@1.3.1
 - @loopback/repository@1.1.5
 - @loopback/rest-explorer@1.1.8
 - @loopback/rest@1.5.5
 - @loopback/service-proxy@1.0.8
 - @loopback/testlab@1.0.6
openapi spec allows developers to provide additional restrictions on input parameters via parameter
schema. For example, pageSize can include schema restrictions

fix loopbackio#1573
openapi spec allows developers to provide additional restrictions on input parameters via parameter
schema. For example, pageSize can include schema restrictions

fix loopbackio#1573
openapi spec allows developers to provide additional restrictions on input parameters via parameter
schema. For example, pageSize can include schema restrictions

fix loopbackio#1573
openapi spec allows developers to provide additional restrictions on input parameters via parameter
schema. For example, pageSize can include schema restrictions

fix loopbackio#1573
openapi spec allows developers to provide additional restrictions on input parameters via parameter
schema. For example, pageSize can include schema restrictions

fix loopbackio#1573
openapi spec allows developers to provide additional restrictions on input parameters via parameter
schema. For example, pageSize can include schema restrictions

fix loopbackio#1573
@YaelGit YaelGit closed this Feb 12, 2019
@YaelGit
Copy link
Author

YaelGit commented Feb 12, 2019

Unfortunately due to some technical issue with my Laptop - things went wrong while trying to rebase.
I will need to reopen a new PR to re-enter my code.

@YaelGit YaelGit reopened this Feb 12, 2019
@YaelGit YaelGit closed this Feb 12, 2019
@YaelGit
Copy link
Author

YaelGit commented Feb 13, 2019

Hello @bajtos @raymondfeng,
Due to technical issues, I have opened a new PRin the following link:
#2382
The PR includes implementation regarding all of the comments above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OpenAPI REST Issues related to @loopback/rest package and REST transport in general
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants