Parameter list as val in akka-http 10.2.x?

Below is a reduced example of something my akka-http 10.1.x code is doing, but no longer works in 10.2.x because of the ParamMagent / ParamSpec change. There’s no mention of how to migrate this in the migration guide, and I think think that would be a useful addition.

The basic case is that I have is a val tuple of parameters that I reuse in multiple routes. In 10.1.x, the ParamMagnet handled this. Now, I have two different problems:

  1. paramsTuple has NamedReceptacle types in it instead of ParamSpec
  2. After adding explicit typing that the paramsTuple should have ParamSpec types instead, I get the error that I can’t pass a tuple to parameters.
def r1(): Route =
    get {
      val paramsTuple = (
        "id".as[String],
        "query".as[String].?
      )
      parameters(
        paramsTuple
      ) {
        (
          id,
          query
        ) =>
          complete(OK, s"$id $query")
      }
    }

Hi @philvarner,

that syntax is indeed not supported any more.

The best replacement would be to put the whole directive into a val:

val parameterD /* : Directive[(String, String)] */ = parameters("id".as[String], "query".as[String].?)

parameterD { (id, query) => 
  //...
}

Would that work?

Johannes

Yes, I think think that’s a good solution for what I described. I forgot that these tuple lists were being used as arguments to both a parameters and a formFields directives. I started to refactor this to explicitly un-tuple the arguments, but then realized it would be too easy to misalign the parameters. Instead, I did something like this:


val paramId = "id".as[String]
val paramQuery =  "query".as[String].?

def r1(): Route =
    get {
      parameters(
        paramId,
        paramQuery
      ) {
        (
          id,
          query
        ) =>
          complete(OK, s"$id $query")
      }
    }

In this case, my types are more refined that just String, so I think I’d get a compile error if I misaligned them, but at least this is more clear This still doesn’t solve the problem of me adding a new parameter to the parameters and forgetting to add it to the formFields use, but It’s close enough.

Thanks!

I see. We used to have AnyParam directives in spray for the purpose of supporting both formFields and parameters at the same time but those turned out hard to migrate and maintain, so they were never migrated to akka-http. I guess with the new scheme it might still be possible but it’s probably not trivial.