Hi all. I'm currently trying to declare a list of ...
# http4k
r
Hi all. I'm currently trying to declare a list of type
List<ContractRoute>
. However, I'm ending up with the type
List<Pair<ContractRouteSpec2<Int, String>.Binder, (Request) -> Response>>
when I add lenses to the route path. I want to add the list of routes to an api contract, which accepts the former type but not the latter, so I was wondering how I could fix it. More details inside: Edit: Found the solution. https://stackoverflow.com/questions/53278208/how-do-you-model-a-path-parameter-in-the-middle-with-http4k
The reason I'm wishing to do so is because I wish to do this:
Copy code
val api = contract {
        routes += merchantItemController.routes
    }
But it states:
Copy code
None of the following candidates is applicable:
fun plusAssign(t: ContractRoute): Unit
fun plusAssign(t: Collection<ContractRoute>): Unit
Now I realized that that was causes the List<Pair...> is adding a lenses to the route. This route with a lens is the type
List<Pair<ContractRouteSpec2<Int, String>.Binder, (Request) -> Response>>
:
Copy code
val routes = listOf(
        "/merchants" / merchantIdLens / "items" bindContract Method.GET to
                { id: Int ->
                    { _: Request ->
                        Response(OK)
                    }
                }
    )
Copy code
Whereas route without a lens is the type `List<ContractRoute>`:
```val routes = listOf(
    "/merchants" / "items" bindContract Method.GET to
            { _: Request ->
                Response(OK)
            }
)
But given that I wish to use the merchantIdLens, is there a recommended way to solve it? I would greatly appreciate any help. If it helps, I could also share the entire file (with the actual logic as opposed to these simplified examples).
Update: Turns out the answer was just to set a parameter (even if it's just empty) per non initial static segment in the url route path.
Copy code
val routes = listOf(
    "/merchants" / merchantIdLens / "items" bindContract Method.GET to
            { id, _ ->
                { _: Request ->
                    Response(OK)
                }
            }
)
Source: https://stackoverflow.com/questions/53278208/how-do-you-model-a-path-parameter-in-the-middle-with-http4k
d
Yes - this is a byproduct of the way that the open api stuff currently works.. the typesafety of the API can't tell the difference between a "static" segment and a dynamic one, so it doesn't know NOT to turn a ContractRouteSpec1 into a ContractRouteSpec2 when you call the / function. Anyway - glad to see you found the answer!