Skip to content

HttpLayerRouter is mixing error schemas between different APIs #6243

@matheuspuel

Description

@matheuspuel

What version of Effect is running?

3.21.2

What steps can reproduce the bug?

This is a minimal reproduction of the issue:

import { HttpApi, HttpApiBuilder, HttpApiEndpoint, HttpApiGroup, HttpLayerRouter } from "@effect/platform"
import { NodeHttpServer } from "@effect/platform-node"
import { Effect, Layer, Schema } from "effect"

const Api1 = HttpApi.make("Api1").add(
  HttpApiGroup.make("group1").add(
    HttpApiEndpoint.post("endpoint1")`/1`.addError(Schema.transformLiteral("BAD", "x"))
  )
)
const Handlers1 = HttpApiBuilder.group(Api1, "group1", (_) => _.handle("endpoint1", () => Effect.void))
const Routes1 = HttpLayerRouter.addHttpApi(Api1).pipe(Layer.provide(Layer.mergeAll(Handlers1)))

const Api2 = HttpApi.make("Api2").add(
  HttpApiGroup.make("group2").add(
    HttpApiEndpoint.get("endpoint2")`/2`.addError(Schema.transformLiteral("GOOD", "x"))
  )
)
const Handlers2 = HttpApiBuilder.group(Api2, "group2", (_) => _.handle("endpoint2", () => Effect.fail("x" as const)))
const Routes2 = HttpLayerRouter.addHttpApi(Api2).pipe(Layer.provide(Layer.mergeAll(Handlers2)))

const AllRoutes = Layer.mergeAll(Routes1, Routes2)
const { handler } = HttpLayerRouter.toWebHandler(AllRoutes.pipe(Layer.provide(NodeHttpServer.layerContext)))

const request = new Request("http://localhost:3000/2")
const response = await handler(request)
const text = await response.text()
console.log(text)

What is the expected behavior?

Should log "GOOD", because of the transformation defined in the error schema for the endpoint 2.

What do you see instead?

It is logging "BAD" instead, incorrectly using the error schema from the endpoint 1.

Additional information

It works correctly if remove Layer.mergeAll from Layer.provide(Layer.mergeAll(Handlers2)). It is unnecessary in this minimal example, but is required for registering multiple endpoints in the same API.

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