[2.1-master] Router - PathBinder for Option behaving strangely?

370 views
Skip to first unread message

Manuel Bernhardt

unread,
Nov 4, 2012, 12:49:58 PM11/4/12
to play-fr...@googlegroups.com
Hi,

I am a bit puzzled as to why I get those:

[error] /Users/manu/workspace/saas/culture-hub/conf/routes:180: No URL
path binder found for type Option[String]. Try to implement an
implicit PathBindable for this type.
[error] GET /organizations/:orgId/site/:language
controllers.organization.CMS.list(orgId, language:
Option[String])

[error] /Users/manu/workspace/saas/culture-hub/conf/routes:173: No URL
path binder found for type Option[org.bson.types.ObjectId]. Try to
implement an implicit PathBindable for this type.
[error] DELETE /organizations/:orgId/groups/remove/:groupId
controllers.organization.Groups.remove(orgId, groupId:
Option[org.bson.types.ObjectId])


I have a custom binder for ObjectId, which I think works fine. For
Option[String| I'd expect it to work out of the box though, or did the
syntax change?

Thanks,

Manuel

Julien Richard-Foy

unread,
Nov 4, 2012, 2:54:33 PM11/4/12
to play-fr...@googlegroups.com
PathBindable[Boolean] instances have been removed: https://github.com/playframework/Play20/commit/bfbdaf918bf4fde2604b4bda405e5acd8f780f5f#framework/src/play/src/main/scala/play/api/mvc/Binders.scala

How to handle a case such as the following?
GET   /foo/:bar/baz    controllers.Application.foo(bar: Option[String])

Manuel Bernhardt

unread,
Nov 5, 2012, 1:44:10 AM11/5/12
to play-fr...@googlegroups.com
Thanks Julien for the heads up. I now use an own Option bindable, and
I'm wondering as well whether it'd make sense to have one similar
mechanism available out of the box again anyway, it seems like a
pretty common use-case to me.

Manuel
> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.

Manuel Bernhardt

unread,
Nov 6, 2012, 1:48:45 AM11/6/12
to play-fr...@googlegroups.com

Guillaume Bort

unread,
Nov 6, 2012, 4:54:07 AM11/6/12
to play-fr...@googlegroups.com
It was removed on purpose. "Optional" path parameters simply don't exist. If it is a path part it can't be optional.
--
Guillaume Bort, http://guillaume.bort.fr

Manuel Bernhardt

unread,
Nov 6, 2012, 6:56:23 AM11/6/12
to play-fr...@googlegroups.com
Ok, granted, an optional binding makes no sense. But it would be
useful to be able to invoke a handler that takes optional arguments,
of the kind:

def createOrUpdate(id: Option[SomeIdType]) = Action {
... // common boilerplate work
id.map { lookupId =>
...
}.getOrElse {
...
}
}

Is there perhaps a trick to do this kind of invocation? Otherwise it
means adding two boilerplate methods just for that case (or create a
custom option binder).

Manuel

Guillaume Bort

unread,
Nov 6, 2012, 9:06:54 AM11/6/12
to play-fr...@googlegroups.com
Well I would say that it is simple to write 2 methods here. Creating an Option binder to hack this would just lead to more confusion for users. You can easily have 2 methods that share some common boilerplate.

Jonathan Lyons

unread,
Nov 18, 2013, 4:33:26 PM11/18/13
to play-fr...@googlegroups.com
Doesn't that violate DRY (Don't Repeat Yourself?) I don't think it adds more confusion. A lot of the time the code for an update and a create are so similar that creating two functions means you are duplicating code unnecessarily. I like to code for maximum re-usability. Yes, I could rip out the code and stick it in a library in between the model and controller layer but I really don't want to do that.
Reply all
Reply to author
Forward
0 new messages