softwaremill / tapir

Rapid development of self-documenting APIs
https://tapir.softwaremill.com
Apache License 2.0
1.36k stars 411 forks source link

Does ZTapir endpoint execute code within a scope? #4055

Open Kalin-Rudnicki opened 2 weeks ago

Kalin-Rudnicki commented 2 weeks ago

Looking at the function signatures for:

    def zServerSecurityLogic[R, U](
        f: SECURITY_INPUT => ZIO[R, ERROR_OUTPUT, U]
    ): ZPartialServerEndpoint[R, SECURITY_INPUT, U, INPUT, ERROR_OUTPUT, OUTPUT, C] =
      ZPartialServerEndpoint(e, f)

  def serverLogic[R0](logic: PRINCIPAL => INPUT => ZIO[R0, ERROR_OUTPUT, OUTPUT]): ZServerEndpoint[R with R0, C] =
    ServerEndpoint(
      endpoint,
      _ => securityLogic(_: SECURITY_INPUT).either.resurrect,
      _ => (u: PRINCIPAL) => (i: INPUT) => logic(u)(i).either.resurrect
    )

Within zServerSecurityLogic I would like to use ZIO.logAnnotateScoped(_, _) to add log context from within the JWT that will be there for all requests. If this is possible, it would be super ideal to add this here, as all endpoints are already calling a shared function that decodes the JWT.

If things are already executed within a scope, I believe the correct signatures would be: (?)

f: SECURITY_INPUT => ZIO[R & Scope, ERROR_OUTPUT, U]

logic: PRINCIPAL => INPUT => ZIO[R0 & Scope, ERROR_OUTPUT, OUTPUT]
adamw commented 2 weeks ago

It's not currently run in a scope, in fact the environment isn't touched at all. Though it might make sense to implement such a functionality. Here's the code for handling requests - I imagine the changes would have to be done there. Provided that you are using zio-http, of course.

If you'd like to attempt a PR, that would be great :)