Q: Support for queryParam() and formParam() with default values - removed in 4.x API
See original GitHub issueEffectively #1291 introduces a breaking API change for queryParam()
and formParam()
that take a default value.
Previous API was:
String myParam = ctx.queryParam("my-param", "one");
I believe this now needs to use a Validator and needs to be written as:
String myParam = ctx.queryParamAsClass("my-param", String.class).getOrDefault("one");
Is that correct, have I missed something?
Noting that for people like myself that prefer external validation and are not interested in using Javalin Validation this API change appears to be a step backwards. Noting that it is pretty common for queryParam
and formParam
to have default values and this is now incurs some extra runtime overhead along with a difference in style when a default value is desired.
Maybe I have missed something. It looks to me like the API for queryParam/formParam that takes a default value as String could be re-instated for backward compatibility given the queryParamAsClass()
validator approach of 1291. What are the thoughts on that?
Thanks, Rob.
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (7 by maintainers)
Top GitHub Comments
I’ll close this for now, but would be willing to consider
xyzOrDefault
methods if someone wants them enough to provide a PR.You are correct, these methods were removed on purpose. They only existed for some methods, and I thought adding this as part of validation made more sense than keeping it in the base API. If a user just wants a default value, that’s pretty easily achieved by
Optional.ofNullable(ctx.queryParam("key")).orElse("value");
, orctx.queryParam("key") ?: "value"
- does this need to be part of the base API?I’ve never been a fan of
ctx.queryParam("key", "default");
, I don’t think it reads well, so I would rather keep it out. I thinkOptional.ofNullable(ctx.queryParam("key")).orElse("value");
reads better thanctx.queryParam("key", "value")
.