Reject extra query parameters

瀏覽次數:46 次
跳到第一則未讀訊息

Richard Rodseth

未讀,
2015年5月29日 下午6:31:592015/5/29
收件者: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

未讀,
2015年5月31日 清晨5:54:412015/5/31
收件者: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

未讀,
2015年6月2日 下午4:02:482015/6/2
收件者:spray...@googlegroups.com
Thanks!

Blake

未讀,
2015年6月25日 上午9:03:112015/6/25
收件者: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

回覆所有人
回覆作者
轉寄
0 則新訊息