softwaremill / tapir

Rapid development of self-documenting APIs

Home Page:https://tapir.softwaremill.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] Mismatch between OpenAPI and actual serialization / deserialization

SimunKaracic opened this issue · comments

Tapir version: ***
1.7.3

Scala version: ***
2.13.12

Describe the bug

What is the problem?
Server responds with snake_case JSON, but OpenAPI schema contains camelCase for all response field members.
How do I make the swagger interpreter pick up my AutoDerivation configuration?
Server is zio-http.

How to reproduce?

I don't have a reproduction sample, but here's some pieces of it:
AutoDerivation decoder

import io.circe._
import io.circe.generic.extras.{AutoDerivation, Configuration}

object SnakeCaseAutoDecoder extends AutoDerivation {
  implicit def configuration: Configuration = Configuration.default.withSnakeCaseMemberNames.withDefaults
}

Relevant imports in files containing endpoint definitions (maybe implicits are clashing?):

import sttp.tapir.generic.auto._
import sttp.tapir.json.circe.jsonBody
import sttp.tapir.ztapir._
import org.json_utils.SnakeCaseAutoDecoder._

Swagger endpoints creation (no imported implicits in the whole file):

SwaggerInterpreter().fromServerEndpoints[Task](apiEndpoints, apiName, "0.1.0")

Additional information
I can try to provide a reproduction sample if nothing stands out as immediately wrong or there's nothing for me to try from the description above

I think the problem might be that schema derivation is a separate process, and you need to configure it separately. You'll have to add similar configuration when deriving schemas (when happens e.g. at the jsonBody[] call), as described here: https://tapir.softwaremill.com/en/latest/endpoint/schemas.html#configuring-derivation

Does this help?

It solves the problem, but I have to manually add the import to every file with Endpoint definitions or else it just silently goes to CamelCase for that endpoint.

Is there a way to provide this in one place, and one place only?

Thank you anyway @adamw that was helpful!

Yes, well it's the same as with importing SnakeCaseAutoDecoder. One possible solution is to create a custom object e.g. object HttpSupport, which would extend all the appropriate traits / contain the necessary global configuration implicits. See https://tapir.softwaremill.com/en/latest/mytapir.html for more on this, or take a look at https://github.com/softwaremill/bootzooka where this is implemented in practice.