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
fix(rest): parse query string even when there is no rest query param #2089
Conversation
-1 from me, I have intentionally disabled query parsing when no query arguments are described in the operation spec. @larsm11 @raymondfeng Let's discuss. What is the use case for having query arguments not documented in the OpenAPI spec? I find the example provided in #2088 as too artificial. IMO, a real-world endpoint will want |
it('parses query without decorated rest query params', async () => { | ||
// See https://github.com/strongloop/loopback-next/issues/2088 | ||
const server = await givenAServer({rest: {port: 0, host: '127.0.0.1'}}); | ||
server.handler(queryRequestHandler); |
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.
Please inline the handler in this test case.
I find it difficult to understand what's going on in this test case when the query handler is defined elsewhere and the name does not explain what is the handler actually doing.
Alternatively, find a better name for this handler function, e.g. respondWithParsedQueryObject
.
I think disabling query parsing is over-restrictive. Parsing query string is not a heavy operation. There are valid use cases that we want to have query string parsed regardless of being referenced in a controller method argument.
|
0803677
to
85282f4
Compare
There is also the question of consistency. So far, LB4 applications were usually parsing the query by calling With your change in place, this code will be effectively never called, because the query will be always parsed by the Express layer. If we agree that the query should be parsed inside Express, then please clean up the code in
Agreed. My main objection is that all of these query parameters should be included in the OpenAPI spec for documentation purposes and for consumption by client generators. Once these params are documented, we will automatically parse the query as part of argument processing. On the other hand, I see that the current implementation does not work when the parsed query needs to be accessed before the parameter parser did it job, e.g. from a middleware or a custom sequence action called early in the sequence flow. I guess that means we do need to change the behavior as you are proposing here. |
22ba32f
to
7e6117b
Compare
I switch to |
7e6117b
to
41e7811
Compare
@bajtos PTAL |
@bajtos Good feedback! PTAL. |
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, please consider addressing my comment below.
@@ -702,6 +688,8 @@ paths: | |||
}); | |||
|
|||
async function givenAServer(options?: {rest: RestServerConfig}) { | |||
options = options || {rest: {port: 0}}; |
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.
Please note that givenHttpServerConfig
sets the port to 0 by default. I think the following simpler version should work well too:
async function givenAServer(options: {rest?: RestServerConfig} = {}) {
options.rest = givenHttpServerConfig(options.rest);
// ...
}
See #2088
Now we use Express body parsers instead of
body
module. I think there was a side effect ofbody
module that parsesrequest.query
beyond Express.Checklist
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated