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
Handle an empty query parameters with @Default
#5515
Comments
In addition to the bugfix, we also need to improve the error message when an incorrect request is made so that users can easily identify the problem. |
@ikhoon |
Yes, please. 😀 |
@ikhoon class MyService {
@Get("/list")
public String list(@Default @Param int page) {
...
}
} and the parameter exists but the value has not been present like In this case, which response do you prefer? |
Plus, in terms of handling empty query parameters, I am considering couple of cases like below: Case 1Say service is like: class MyService {
@Get("/list")
public String list(@Default("1") @Param List<Integer> params) {
...
}
} But the request is sent like Then which action would be processed?
Case 2If service is like class MyService {
@Get("/list")
public String list(@Default @Param List<Integer> params) {
...
}
} and the request is sent like Then which action would be processed?
Case 3If service is like class MyService {
@Get("/list")
public String list(@Default @Param List<String> params) {
...
}
} and the request is sent like Then which action would be processed?
Please give me your opinion for enhancement. Thank you. |
In that case, IllegalArgumentException might be good.
I prefer to inject Thanks for your thorough analysis. 👍 |
Motivation: Say we have an annotated service that uses `@Default `annotation to optionally bind query parameters. ```java class MyService { @get("/list") public String list(@default @param Integer page) { ... } } ``` It does not inject `null` if there is a parameter but no value. A NumberFormatException is raised when `/list?page=` is sent as the request path. It would be better to solve this problem as follows the value of the parameter is empty. Modifications: - Logger will warn that the parameter value is not present (It may be intended, such as sending an empty string as the parameter) - Inject an empty string `""` if the type of the parameter is `String` - Inject `null` if the type of the parameter is not `String` such as `Integer`, `Boolean` and so on - Throw an `IllegalArgumentException` if the type of the parameter is primitive type - Inject an empty list `[]` if the type of the parameter is `List` or `Set` Result: - Closes #5515. --------- Co-authored-by: jrhee17 <guins_j@guins.org>
Say we have an annotated service that uses
@Default
annotation to optionally bind query parameters.As per the Javadoc of
@Default
, when a value is not specified for@Default
, a non-existent parameter is converted intonull
.page
is null if/list
is the request path.armeria/core/src/main/java/com/linecorp/armeria/server/annotation/Default.java
Lines 35 to 36 in ccb29fe
However, it does not work well if there is a parameter but no value. A
NumberFormatException
is raised when/list?page=
is sent as the request path.It would be better to solve this problem as follows the value of the parameter is empty.
page
isString
.page
is notString
such asInteger
,Boolean
and so on.The text was updated successfully, but these errors were encountered: