tapir icon indicating copy to clipboard operation
tapir copied to clipboard

[BUG] CORSInterceptor doesn't work with Vert.x server

Open susliko opened this issue 1 year ago • 0 comments

Tapir version: 1.20.0

Scala version: 3.4.0

Describe the bug

Hi! 🖖🏻 Preflight requests are not processed by the server: 405 Method Not Allowed is returned instead of 204 No Content

How to reproduce?

//> using scala 3.4.0
//> using dep com.softwaremill.sttp.tapir::tapir-vertx-server:1.10.0

import sttp.tapir._
import sttp.tapir.server.vertx.VertxFutureServerInterpreter
import sttp.tapir.server.vertx.VertxFutureServerInterpreter._
import io.vertx.core.Vertx
import io.vertx.ext.web._
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
import sttp.tapir.server.vertx.VertxFutureServerOptions
import sttp.tapir.server.interceptor.cors.CORSInterceptor
import io.vertx.ext.web.handler.CorsHandler

@main
def tapirVertx =
  given ExecutionContext = scala.concurrent.ExecutionContext.Implicits.global
  val vertx = Vertx.vertx()

  val server = vertx.createHttpServer()
  val router = Router.router(vertx)

  // CORS works with native Vert.x handler
  // router.route().handler(CorsHandler.create())

  val myEndpoint = endpoint.get
    .in("path")
    .out(plainBody[String])
    .serverLogic(_ => Future(Right("OK")))

  val corsInterceptor = CORSInterceptor.default[Future]
  val opts = VertxFutureServerOptions.customiseInterceptors.corsInterceptor(
    corsInterceptor
  )
  val attach = VertxFutureServerInterpreter(opts.options).route(myEndpoint)
  attach(router)

  Await.result(server.requestHandler(router).listen(9000).asScala, Duration.Inf)

and then

curl -v -X OPTIONS http://localhost:9000/path -H 'Origin: http://my.origin' -H 'Access-Control-Request-Method: GET'

Additional information

My guess is that all routing is done inside vert.x Router and execution control is not passed to interceptors

susliko avatar Mar 30 '24 09:03 susliko