Reject extra query parameters

46 views
Skip to first unread message

Richard Rodseth

unread,
May 29, 2015, 6:31:59 PM5/29/15
to spray...@googlegroups.com

Is there a simple way to allow only the query parameters specified in a parameters() directive?

I have the following in a route

      parameters('limit.as[Int] ? 1000, 'definition.?.as[Option[Long]], 'site.?.as[Option[Long]], 'status.?.as[Option[String]]) { (limit, definition, site, contextStatus) =>

But if I use ?definitionId=123 instead of ?definition=123 the route is accepted and if I don't catch this down the line the user is very confused.

Age Mooij

unread,
May 31, 2015, 5:54:41 AM5/31/15
to spray...@googlegroups.com
Hi Richard

You seem to have made every parameter in your list optional (using the ‘?’ modifier) so any request will pass through the parameter directive, no matter what the parameters actually were. 

If you want to limit passthrough using the standard parameters directive, you will have to make one or more parameters mandatory by removing the ‘?’ modifier.

See the docs for the parameter directive for some of the options:


Spray  doesn’t currently support the concept of "required parameters with default values”, i.e. limiting the allowed parameters to the ones specified using the parameters directive.

You could build such directive yourself though, using the lower-level (parameter) directives, but the implementation of the parameter directives is not exactly the simplest Scala source code you will probably have encountered. Here’s the code:


Hope this helps
Age



--
You received this message because you are subscribed to the Google Groups "spray.io User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
Visit this group at http://groups.google.com/group/spray-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/CAKH0pS7x_SyZ_VGvpZgoPZF-PWt%3DSTu33Jv8TPT039t5jzFTcg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Richard Rodseth

unread,
Jun 2, 2015, 4:02:48 PM6/2/15
to spray...@googlegroups.com
Thanks!

Blake

unread,
Jun 25, 2015, 9:03:11 AM6/25/15
to spray...@googlegroups.com

What would be really nice is if Spray's RequestContext held a reference to un-matched request parameters in the same way that it holds the un-matched path.


Then you could have multiple nested parameter and parameters directives and a final unmatchedParameters directive that gave you what was left or a rejectIfRemainingUnmatchedParameters.


It looks like that would be a fairly major change to the way that Spray works to support this though,


The only way we could get our code to work was


parameter('example1) { example1 =>

 parameter('example2) { example2 =>

   parameterMap.require(onlyTheseParams(Set("example1", "example2"))) {

     complete("Ok")

   }

 }

}


private def onlyTheseParams(parameters: Set[String]): (Map[String, String]) => Boolean = {

  params => params.keySet.filterNot(parameters).size == 0

}


which is nasty duplication

Reply all
Reply to author
Forward
0 new messages