Skip to content

Commit 12211f6

Browse files
authored
add macros for tapir codegen (#1281)
1 parent 7fbeafe commit 12211f6

File tree

1 file changed

+26
-0
lines changed
  • jsoniter-scala-macros/shared/src/main/scala-2/com/github/plokhotnyuk/jsoniter_scala/macros

1 file changed

+26
-0
lines changed

jsoniter-scala-macros/shared/src/main/scala-2/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMaker.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,14 @@ object JsonCodecMaker {
527527
*/
528528
def makeCirceLike[A]: JsonValueCodec[A] = macro Impl.makeCirceLike[A]
529529

530+
/**
531+
* Replacements for the `make` call preconfigured to behave as expected by openapi specifications.
532+
* */
533+
def makeOpenapiLike[A]: JsonValueCodec[A] = macro Impl.makeOpenapiLike[A]
534+
def makeOpenapiEnumLike[A]: JsonValueCodec[A] = macro Impl.makeOpenapiEnumLike[A]
535+
def makeOpenapiADTLikeDefaultMapping[A](discriminator: String): JsonValueCodec[A] = macro Impl.makeOpenapiADTLikeDefaultMapping[A]
536+
def makeOpenapiADTLike[A](discriminator: String, mapping: PartialFunction[String, String]): JsonValueCodec[A] = macro Impl.makeOpenapiADTLike[A]
537+
530538
/**
531539
* A replacement for the `make` call with the
532540
* `CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false).withTransientNone(false).withDiscriminatorFieldName(None).withAdtLeafClassNameMapper(x => enforce_snake_case(simpleClassName(x))).withFieldNameMapper(enforce_snake_case).withJavaEnumValueNameMapper(enforce_snake_case)`
@@ -570,6 +578,24 @@ object JsonCodecMaker {
570578
make(c)(CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false).withTransientNone(false)
571579
.withDiscriminatorFieldName(None).withCirceLikeObjectEncoding(true))
572580

581+
def makeOpenapiLike[A: c.WeakTypeTag](c: blackbox.Context): c.Expr[JsonValueCodec[A]] =
582+
make(c)(CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false)
583+
.withRequireCollectionFields(true).withAllowRecursiveTypes(true))
584+
585+
def makeOpenapiEnumLike[A: c.WeakTypeTag](c: blackbox.Context): c.Expr[JsonValueCodec[A]] =
586+
make(c)(CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false)
587+
.withRequireCollectionFields(true).withAllowRecursiveTypes(true).withDiscriminatorFieldName(scala.None))
588+
589+
def makeOpenapiADTLikeDefaultMapping[A: c.WeakTypeTag](c: blackbox.Context)(discriminator: c.Expr[String]): c.Expr[JsonValueCodec[A]] =
590+
make(c)(CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false)
591+
.withRequireCollectionFields(true).withAllowRecursiveTypes(true)
592+
.withRequireDiscriminatorFirst(false).withDiscriminatorFieldName(Some(c.eval(c.Expr[String](c.untypecheck(discriminator.tree.duplicate))))))
593+
594+
def makeOpenapiADTLike[A: c.WeakTypeTag](c: blackbox.Context)(discriminator: c.Expr[String], mapping: c.Expr[PartialFunction[String, String]]): c.Expr[JsonValueCodec[A]] =
595+
make(c)(CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false)
596+
.withRequireCollectionFields(true).withAllowRecursiveTypes(true)
597+
.withRequireDiscriminatorFirst(false).withDiscriminatorFieldName(Some(c.eval(c.Expr[String](c.untypecheck(discriminator.tree.duplicate))))).withAdtLeafClassNameMapper(x => c.eval(c.Expr[PartialFunction[String, String]](c.untypecheck(mapping.tree.duplicate))).apply(com.github.plokhotnyuk.jsoniter_scala.macros.JsonCodecMaker.simpleClassName(x))))
598+
573599
def makeCirceLikeSnakeCased[A: c.WeakTypeTag](c: blackbox.Context): c.Expr[JsonValueCodec[A]] =
574600
make(c)(CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false).withTransientNone(false)
575601
.withDiscriminatorFieldName(None).withAdtLeafClassNameMapper(x => enforce_snake_case(simpleClassName(x)))

0 commit comments

Comments
 (0)