Support for binding empty-string / invalid values for Option[PlayEnum] query string params to None instead of failing
See original GitHub issueUnfortunately browsers submit input parameters even if their values are empty. This means that input myenum value=“” will send “?myenum=&…” which causes enumeratum to fail with “Cannot parse parameter myenum as an Enum”. In my specific case, myenum is a pulldown with optional filter values not required to be selected (“”, All), (“MyEnum.One”, One), etc. In my routes file I have “myenum: Option[MyEnum] ?= None” and expect that myenum=&… (the case where someone submits a form with a filter pulldown left at default value of “”) would return None.
I believe the best thing would be for enumeratum to treat empty parameters as None for enums defined as Option in routes file.
One solution could be making UrlBinders.scala only consider the first nonEmpty parameter:
def queryBinder[A <: EnumEntry](
...
def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, A]] = {
// params.get(key).flatMap(_.headOption).map { p =>
params.get(key).flatMap(_.find(_.nonEmpty)).map { p =>
val maybeBound = ...
It could be better to add a new QueryStringBindable that treats Option[PlayEnum] in routes different than PlayEnum but I am not sure if that is possible or actually any better than above solution.
(BTW: thanks so much for enumeratum…I am blown away at how much better enum heavy code is with it and very sad how long I went (years!) without knowing there was a better way to do enum in scala. )
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
The skepticism is completely justified and your title update is spot on. Your workarounds will give anyone that comes along with the same issue food for thought. I think the ticket can be closed for now as discussed/reopen if you ever feel like supporting it. While I chew on the view layer suggestion, I will start with my own binder like this (unless you would suggest something else):
By the way, I think enumeratum needs to be here so new users can find it easier: https://www.playframework.com/documentation/2.6.x/ModuleDirectory. Please consider submitting it unless you disagree.
The point of Play and subsequently, this lib, is to make sure that doesn’t happen. There might be some differences in opinion given that not everyone uses Play for the same use-cases 😃
Seeing as how I, and quite a few others have been using
enumeratum-play
, and, the default Play binders with forms, query strings, etc for a few years now without needing the fail-invalid-values-to-None behaviour that you’re suggesting, I remain somewhat skeptical that this is the right path to go (or encourage), given that we’ve already identified workarounds for this specific use-case (adding an enum member for your view layer and doing translations across layers, writing your own binder)I’ll leave this ticket open for a while longer to see if there is something there that will make me keener to maintain some more code, but in the mean time I’ve also amended the title of this ticket to be more specific.