Description
I'm trying to upgrade Dropwizard from 1.3.23 to 2.0.10 and am running into some issues around the use of parameters. Specifically, I see that this commit deprecated a number of *Param classes.
Some of these, like IntParam are straightfoward. Using OptionalInt as a replacement makes sense. However, taking non-primitive parameters as an example, Optional<UUID> is NOT a sufficient replacement for UUIDParam. Using it results in me getting a 404 when using a non-UUID as input (where I previously got 400 using UUIDParam).
Additionally, and maybe this is a separate issue, I found the compatibility-preserving use of UUIDParam is strange. When diving into the source code, I found this resource method[1]:
@GET
@Path("/uuid")
public String getUUID(@QueryParam("uuid") Optional<UUIDParam> uuid) {
return uuid.orElse(new UUIDParam("d5672fa8-326b-40f6-bf71-d9dacf44bcdc")).get().toString();
}
This construct seems an extremely clunky way to manage this. In 1.3.X, I could write code like this instead:
@GET
@Path("/uuid")
public String getUUID(@Nullable @QueryParam("uuid") UUIDParam uuid) {
if (uuid != null) {
return uuid.get()
} else {
return "d5672fa8-326b-40f6-bf71-d9dacf44bcdc"
}
}
You can see in the above that there is a lot of unnecessary conversion going on (we construct a UUID via UUIDParam and validate it, even though we just want to return a String). I use Kotlin primarily, my preferred example above would be even simpler:
uuid.get()?.toString ?: "d5672fa8-326b-40f6-bf71-d9dacf44bcdc"
What I'd prefer overall is something that worked like this:
@GET
@Path("/uuid")
public String getUUID(@Nullable @QueryParam("uuid") Optional<UUID> uuid) {
return uuid.map { it.toString() }.orElse("d5672fa8-326b-40f6-bf71-d9dacf44bcdc")
}
More than anything, really, I just want some guidance for the right way to handle various param types, especially in light of the deprecation. The documentation is light on this (I found the MR saying AbstractParam and child classes will be removed, but nothing saying why, and no code showing what would replace the more complicated use-cases like UUIDParam above). I would love to see this improved. Until then, I'm hesitant to upgrade to 2.X.X. Param handling is still confusing in 1.X.X, but at least I won't have to worry about behavior changes.
Activity
joschi commentedon Jun 18, 2020
The code in
OptionalQueryParamResourceTestis not very nice.If you want to have it a little more idiomatic, you can write:
If
Optional<java.util.UUID>works for you (java.util.UUIDbehaves slightly different thanUUIDParam), you could also write:mikeholler commentedon Jun 18, 2020
@joschi thanks for the snippet. I understand that I can do that. I am seeing a substantial behavioral difference that is preventing my existing code from being compatible. Particularly, this suite of tests used to pass in 1.3.23 and now fails in 2.0.10. It's like this for the *Param types built into Dropwizard as well, but I wanted to show this use case because:
AbstractParamsubclasses is not described in the Upgrade Notes for Dropwizard 2.0.X document.The specific test failures from this suite are the last two tests:
missing interval when interval param is non-nullableused to fail in the way shown in the test case, and now fails withNullPointerExceptionbecauseAbstractParam.toString()is implemented with this line:return value.toString()so whenvalueisnulland you call.toString()onAbstractParamit will fail.missing interval when interval param is nullableused to succeed, and now fails with a 400 status code and a message saying<interval> must be in IDO-8601 format.. This error message is occurring becauseparseis being called withinput = nullwhich did not happen in Dropwizard 1.3.27. In that version, there was no invocation of theparsemethod at all.These changes have serious implications on our large API, as we will have to manually update many parameters and test cases. I'm not really sure what the right solution is here, and would appreciate your support. I wouldn't be surprised if others were having problems as well.
P.S. Kotlin is my primary language nowadays, but if you need me to convert anything to Java or are confused about my code please let me know.
mikeholler commentedon Jun 18, 2020
I've also taken the time to put together the same test for
UUIDParam(see below). In this case, I would expect a deprecated feature to work as it did before, but again the below test suite passes in 1.3.72 and fails in 2.0.10 on the last two tests.sleberknight commentedon Jul 16, 2020
We also have experienced similar problems but with regular old long parameters, e.g.
OptionalLongandOptional<Long>which behave completely differently. (The same goes forOptionalIntandOptional<Integer>etc). Here is a sample endpoint using optionals:Observed behavior:
If you do
GET /optionalsTest/42/84, you get a200response with the expected response entity.If you do
GET /optionalsTest/abcd/84, you get a500response.If you do
GET /optionalsTest/42/abcd, you get a404response.So, when using
OptionalLongand supplying a non-numeric value, you get a500which is very confusing behavior. It should be a400asLongParamwould do.But, when using
Optional<Long>and supplying a non-numeric value, you get a404which is maybe even more confusing than getting a400or500.Using
LongParamalways gives a400response and gives a nice error message as well. For this sample endpoint:If you do
GET /longParamTest/abcdyou get a400response with the following body:So while I have no problem using
OptionalLongorOptional<Long>etc. (other than both IntelliJ and Sonar complaining about using optionals for method arguments), the completely different behavior (i.e. the500forOptionalLongand400forOptional<Long) is confusing and not as clear as the behaviorLongParamexhibits (as well as all the otherXxxParamclasses).github-actions commentedon Apr 21, 2021
This issue is stale because it has been open 90 days with no activity. Remove the "stale" label or comment or this will be closed in 14 days.