Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for specifying custom json codecs and tapir schema #639

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -26,7 +26,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 @@ -973,9 +972,8 @@ object OpenApiZioSchemaCirceConverter {
validator(a).map(OpenApiValidation.errorMessage)
}
}
decoder
metadata.jsonDecoder.getOrElse(decoder)
}

}
}

Expand Down Expand Up @@ -1854,12 +1852,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 @@ -220,7 +220,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 {
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