How to bind optional query parameter?

The first option is to set the parameter to Option[T]:

case class MyQP(mandatory: String, opt: Option[Int]) derives QueryStringRW

If you make a request with params ?mandatory=abc, opt will have value of None.


The second option is to set the parameter to some default value:

case class MyQP2(mandatory: String, opt: Int = 42) derives QueryStringRW

Here if you make a request with params ?mandatory=abc the opt will have value of 42.

Note that you need the -Yretain-trees scalac flag turned on, otherwise it won't work!