-
Notifications
You must be signed in to change notification settings - Fork 532
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
Added query parameters getter inside RoutingContext #581
Conversation
* | ||
* @return the map of query parameters (if a query parameter value is an array, it will be provided as comma separated string, so if you need to extract array from query use {@link #queryParam(String)} queryParam) | ||
*/ | ||
Map<String, String> queryParams(); |
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.
In order to make it correct this should be a MultiMap
http://vertx.io/docs/apidocs/io/vertx/core/MultiMap.html
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.
Working on it, I will commit as soon as possible
return getQueryParamsParsed().get(query); | ||
} | ||
|
||
protected void setQueryParams(Map<String, List<String>> q) { |
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.
same, should be multimap
queryParams.put(e.getKey(), String.join(",", e.getValue())); | ||
} | ||
|
||
private Map<String, String> getQueryParams() { |
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.
Multimap
|
||
private Map<String, String> getQueryParams() { | ||
if (queryParams == null) { | ||
queryParams = new HashMap<>(); |
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.
the initial state could be fetched from the HttpServerRequest.getParams()
since it does the initial parsing for us
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.
The real trouble is that HttpServerRequest.params()
don't return only query params, It returns an unique multimap with path params and query params. I've done a small test:
Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
router.route("/:id").handler(routingContext -> {
System.out.println(Arrays.toString(routingContext.request().params().entries().toArray()));
routingContext.response().end("Hello!");
});
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
Doing a request with this URL:
http://localhost:8080/ciao?ei=ao
It prints in console this: [ei: ao, id: ciao]
return queryParams; | ||
} | ||
|
||
private Map<String, List<String>> getQueryParamsParsed() { |
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.
This becomes not needed if we populate from the base object and parse on setPathParams
right?
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.
Use the MultiMap
type for Map<String, List<String>>
kind of type and maybe we can remove the need for the second map parsedPathParams
Fixed a bug that duplicates entries in queryParams when more than one handler is set for a path |
To allow users to avoid conflicts between path parameters and query parameters, I created a query parameters getter.
Unfortunately i can't return from
queryParams()
aMap<String, List<String>>
because of this: https://github.com/vert-x3/vertx-codegen/blob/master/src/main/java/io/vertx/codegen/ClassModel.java#L424So:
queryParams()
returns aMap<String, String>.
If one query parameter is an array It will return as a comma separated listqueryParam(name)
returns aList<String>
, containing the query parameter value/valuesI also wrote a small unit test to test this getter