Automatically generating tapir codecs using zio json codecs

I am trying to define the following endpoint:

object WebApiEndpoints {
  val geminiMarketData = endpoint.get
    .in("ws")
    .out(
      webSocketBody[
        String,
        CodecFormat.TextPlain,
        MarketDataResponse,
        CodecFormat.Json
      ](ZioStreams)
    )
}

I already have the zio json codecs in scope

given MarketDataResponseCodec: JsonCodec[MarketDataResponse] =
    DeriveJsonCodec
      .gen[MarketDataResponse]

and am getting an error asking for a

ttp.tapir.Codec[
    Array[Byte],
    MarketDataResponse,
    sttp.tapir.CodecFormat.Json
  ]

However I cannot find an example in the documentation on how to derive this type automatically. I am seeing some reference to creating a schema but I am not sure how that relates when I already have the zio json codec.

I would like to avoid creating the codec manually if at all possible

Hello!

I added an example of a WS endpoint accepting and producing WS messages:

It should be sufficient to import the Tapir Codecs for ZIO JSON, using import sttp.tapir.json.zio.*. Using this and the built-in implicits, a codec using them should be automatically built for WebSocketFrames: tapir/core/src/main/scala/sttp/tapir/Codec.scala at master · softwaremill/tapir · GitHub

1 Like