Circe,Tapir and JodaTime

799 Views Asked by At

I have a case class like

final case class MyClass(id: Long, eventData: EventsDTO)

final case class EventsDTO(
    customerId: Long,
    eventName: String,
    processTime: DateTime //JodaTime
)

I have custom encoder and decoder as

val dateFormatter = DateTimeFormat.forPattern("yyyyMMdd")
    implicit def TimestampFormat: Encoder[DateTime] with Decoder[DateTime] =
      new Encoder[DateTime] with Decoder[DateTime] {
        override def apply(a: DateTime): Json = Encoder.encodeString.apply(a.toString)

        override def apply(c: HCursor): Result[DateTime] =
          Decoder.decodeString.map(s => DateTime.parse(s, dateFormatter)).apply(c)
      }

I have my tapir endpoint as

 val inclusive: Route[Unit, MyClass] =
      endpoint.post
        .in(Paths.api)
        .out(jsonBody[WidgetsResponse])

But if run above code I get

Error:(71, 15) Cannot find a codec for type: com.Model.MyClass, formatted as: sttp.tapir.CodecFormat.Json.
Did you define a codec for: com.Model.MyClass?
Did you import the codecs for: sttp.tapir.CodecFormat.Json?
Is there an implicit schema for: com.Model.MyClass, and all of its components?
(codecs are looked up as implicit values of type Codec[com.Model.MyClass, sttp.tapir.CodecFormat.Json, _];
schemas are looked up as implicit values of type Schema[com.Model.MyClass])
      jsonBody[MyClass]


Error:(71, 15) not enough arguments for method jsonBody: (implicit codec: sttp.tapir.CodecForOptional[com.Model.MyClass, sttp.tapir.CodecFormat.Json, _])sttp.tapir.EndpointIO.Body[com.Model.MyClass, sttp.tapir.CodecFormat.Json, _].
Unspecified value parameter codec.
      jsonBody[MyClass]

Can someone please help what am I doing wrong ?

If I remove processTime: DateTime //JodaTime from EventsDTO , the application runs just fine.

1

There are 1 best solutions below

0
On

This was the missing piece

  implicit val schemaForDateTime: Schema[DateTime] = Schema(SString)