'onDecodeFailureNextEndpoint' For Query Parameters?


I know this is not ideal, but I’ve been asked to implement a 3rd party spec that requires the use of a single path and method for multiple requests differentiated by a query parameter.

The requests are totally different and having OpenAPI docs for each of the requests as opposed to one is a huge win, but Tapir will only allow me to move onto the next defined endpoint if the path is a partial match.

Is there any way I can achieve the same behaviour if the query parameters match?

In the OpenAPI world this has been discussed before, the limitation of deciding if an endpoint is unique by a combination of path and method, so people have got around that by adding path fragments that are ignored by the underlying server. Not totally relevant for this specific case but I imagine the answer is going to be ‘no’ because of how OpenAPI defines uniqueness :smiley:

To change this behavior in server interpreters, you should customise the DecodeFailureHandler, so that it returns None instead of a 400 in case there’s a decode failure on a query parameter.

That’s what happening by default: https://github.com/softwaremill/tapir/blob/8ce3345d7c192dff0e037051ed504bd373d9b481/server/core/src/main/scala/sttp/tapir/server/interceptor/decodefailure/DecodeFailureHandler.scala#L103

So I think using query[String]("discriminator").onDecodeFailureNextEndpoint should do the trick.

However, you won’t get correct docs, and OpenAPI mandates that each endpoint has a different path.

1 Like