Skip to content

Nesting doesn't work for Endpoint API-generated routes #4085

@anqit

Description

@anqit

Describe the bug
When using Routes.nest(prefix) (or the equivalent PathCodec / Routes syntax) with routes created via Endpoint.implementHandler, the endpoint's internal codec fails to decode the request path,
producing a MalformedPath error. The nesting correctly updates the RoutePattern for matching, but the endpoint's internal input codec still expects the original (un-prefixed) path.

To Reproduce

 ```scala
 import zio.*
 import zio.http.*
 import zio.http.codec.PathCodec
 import zio.http.codec.PathCodec.given
 import zio.http.endpoint.Endpoint

 object NestBug extends ZIOAppDefault:

   val endpoint: Endpoint[Unit, Unit, Nothing, String, zio.http.endpoint.AuthType.None] =
     Endpoint(Method.GET / PathCodec.empty).out[String]

   val route: Route[Any, Nothing] =
     endpoint.implementPurely(_ => "hello")

   val prefix: PathCodec[Unit] = "api"

   val nested: Routes[Any, Nothing] = prefix / route.toRoutes

   override val run =
     Server.serve(nested).provide(Server.default)
 ```

 Sending `GET /api` returns:
 ```json
 {
   "name": "MalformedPath",
   "message": "Malformed path /api failed to decode using /: Expected end of path but found: api"
 }
 ```

Expected behaviour
The request should match the nested route and return "hello".

I think that when nesting routes, the route matched by the prefix should be "consumed" and the remaining route is what should be processed/matched by the endpoint's codec, but it looks like the endpoint is trying to process the entirety of the original route.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions