Skip to content

Commit

Permalink
Add support for specifying custom json codecs and tapir schema
Browse files Browse the repository at this point in the history
  • Loading branch information
pwliwanow committed Feb 4, 2025
1 parent 996bb0f commit 7cdd20e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ object OpenApiAnnotations {
final case class sumTypeSerDeStrategy[A](value: OpenApiSumTypeSerDeStrategy[A]) extends StaticAnnotation
final case class jsonCaseConverter(from: OpenApiNamingConvention, to: OpenApiNamingConvention)
extends StaticAnnotation
final case class jsonEncoder[A](value: io.circe.Encoder[A]) extends StaticAnnotation
final case class jsonDecoder[A](value: io.circe.Decoder[A]) extends StaticAnnotation
final case class tapirSchema[A](value: sttp.tapir.Schema[A]) extends StaticAnnotation
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ final private[chopsticks] case class OpenApiParsedAnnotations[A](
validator: Option[Validator[A]] = None,
default: Option[(A, Option[Any])] = None,
sumTypeSerDeStrategy: Option[OpenApiSumTypeSerDeStrategy[A]] = None,
jsonCaseConverter: Option[jsonCaseConverter] = None
jsonCaseConverter: Option[jsonCaseConverter] = None,
jsonEncoder: Option[io.circe.Encoder[A]] = None,
jsonDecoder: Option[io.circe.Decoder[A]] = None,
tapirSchema: Option[sttp.tapir.Schema[A]] = None
) {
def transformJsonLabel(label: String): String = {
jsonCaseConverter match {
Expand All @@ -31,6 +34,9 @@ object OpenApiParsedAnnotations {
case a: default[A @unchecked] => typed.copy(default = Some((a.value, a.encodedValue)))
case a: sumTypeSerDeStrategy[A @unchecked] => typed.copy(sumTypeSerDeStrategy = Some(a.value))
case a: jsonCaseConverter => typed.copy(jsonCaseConverter = Some(a))
case a: jsonEncoder[A @unchecked] => typed.copy(jsonEncoder = Some(a.value))
case a: jsonDecoder[A @unchecked] => typed.copy(jsonDecoder = Some(a.value))
case a: tapirSchema[A @unchecked] => typed.copy(tapirSchema = Some(a.value))
case _ => typed
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import java.time.{
ZonedDateTime
}
import java.util.UUID
import scala.annotation.nowarn
import scala.collection.immutable.ListMap
import scala.collection.mutable.ListBuffer
import scala.language.existentials
Expand Down Expand Up @@ -1040,9 +1039,8 @@ object OpenApiZioSchemaCirceConverter {
validator(a).map(OpenApiValidation.errorMessage)
}
}
decoder
metadata.jsonDecoder.getOrElse(decoder)
}

}
}

Expand Down Expand Up @@ -1987,12 +1985,11 @@ object OpenApiZioSchemaCirceConverter {
addAnnotations(baseEncoder.asInstanceOf[Encoder[A]], extractAnnotations(annotations))
}

@nowarn
private def addAnnotations[A](
baseEncoder: Encoder[A],
metadata: OpenApiParsedAnnotations[A]
): Encoder[A] = {
baseEncoder
metadata.jsonEncoder.getOrElse(baseEncoder)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ object OpenApiZioSchemaToTapirConverter {
result = metadata.default.fold(result) { case (default, encodedDefault) =>
result.default(default, encodedDefault)
}
result
metadata.tapirSchema.getOrElse(result)
}

private def schemaName(entityName: String): SName = SName(entityName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ object OpenApiZioSchemas extends DefaultJavaTimeSchemas {
schema.annotate(OpenApiAnnotations.jsonCaseConverter(from, to))
def withSnakeCaseJsonFields: Schema[A] =
withJsonFieldsCaseConverter(OpenApiNamingConvention.OpenApiCamelCase, OpenApiNamingConvention.OpenApiSnakeCase)

def withJsonEncoder(encoder: io.circe.Encoder[A]): Schema[A] =
schema.annotate(OpenApiAnnotations.jsonEncoder(encoder))
def withJsonDecoder(decoder: io.circe.Decoder[A]): Schema[A] =
schema.annotate(OpenApiAnnotations.jsonDecoder(decoder))
def withTapirSchema(tapirSchema: sttp.tapir.Schema[A]): Schema[A] =
schema.annotate(OpenApiAnnotations.tapirSchema(tapirSchema))
}

object Validators {
Expand Down

0 comments on commit 7cdd20e

Please sign in to comment.