diff --git a/core/src/main/scala/sttp/tapir/generic/Configuration.scala b/core/src/main/scala/sttp/tapir/generic/Configuration.scala index d9f45f7449..faae61d8da 100644 --- a/core/src/main/scala/sttp/tapir/generic/Configuration.scala +++ b/core/src/main/scala/sttp/tapir/generic/Configuration.scala @@ -19,6 +19,7 @@ final case class Configuration(toEncodedName: String => String, discriminator: O def withKebabCaseMemberNames: Configuration = copy(toEncodedName = Configuration.kebabCaseTransformation) def withDiscriminator(d: String): Configuration = copy(discriminator = Some(d)) def withSnakeCaseDiscriminatorValues: Configuration = copy(toDiscriminatorValue = Configuration.shortSnakeCaseSubtypeTransformation) + def withScreamingSnakeCaseDiscriminatorValues: Configuration = copy(toDiscriminatorValue = Configuration.shortScreamingSnakeCaseSubtypeTransformation) def withKebabCaseDiscriminatorValues: Configuration = copy(toDiscriminatorValue = Configuration.shortKebabCaseSubtypeTransformation) def withFullDiscriminatorValues: Configuration = copy(toDiscriminatorValue = Configuration.fullIdentitySubtypeTransformation) def withFullSnakeCaseDiscriminatorValues: Configuration = copy(toDiscriminatorValue = Configuration.fullSnakeCaseSubtypeTransformation) @@ -60,6 +61,9 @@ object Configuration { private val shortSnakeCaseSubtypeTransformation: SName => String = shortIdentitySubtypeTransformation.andThen(snakeCaseTransformation) + private val shortScreamingSnakeCaseSubtypeTransformation: SName => String = + shortIdentitySubtypeTransformation.andThen(screamingSnakeCaseTransformation) + private val shortKebabCaseSubtypeTransformation: SName => String = shortIdentitySubtypeTransformation.andThen(kebabCaseTransformation) diff --git a/core/src/test/scala/sttp/tapir/generic/SchemaGenericAutoTest.scala b/core/src/test/scala/sttp/tapir/generic/SchemaGenericAutoTest.scala index c805292c1c..311ee9e6b1 100644 --- a/core/src/test/scala/sttp/tapir/generic/SchemaGenericAutoTest.scala +++ b/core/src/test/scala/sttp/tapir/generic/SchemaGenericAutoTest.scala @@ -309,6 +309,20 @@ class SchemaGenericAutoTest extends AsyncFlatSpec with Matchers { ) } + it should "generate one-of schema using the given discriminator (screaming snake case subtype names)" in { + implicit val customConf: Configuration = Configuration.default.withDiscriminator("who_am_i").withScreamingSnakeCaseDiscriminatorValues + implicitly[Schema[Entity]].schemaType.asInstanceOf[SCoproduct[Entity]].discriminator shouldBe Some( + SDiscriminator( + FieldName("who_am_i"), + Map( + "ORGANIZATION" -> SRef(SName("sttp.tapir.generic.Organization")), + "PERSON" -> SRef(SName("sttp.tapir.generic.Person")), + "UNKNOWN_ENTITY" -> SRef(SName("sttp.tapir.generic.UnknownEntity")) + ) + ) + ) + } + it should "generate one-of schema using the given discriminator (full subtype names)" in { implicit val customConf: Configuration = Configuration.default.withDiscriminator("who_am_i").withFullDiscriminatorValues implicitly[Schema[Entity]].schemaType.asInstanceOf[SCoproduct[Entity]].discriminator shouldBe Some( diff --git a/json/pickler/src/main/scala/sttp/tapir/json/pickler/PicklerConfiguration.scala b/json/pickler/src/main/scala/sttp/tapir/json/pickler/PicklerConfiguration.scala index c0170fa3a2..da8012c2a1 100644 --- a/json/pickler/src/main/scala/sttp/tapir/json/pickler/PicklerConfiguration.scala +++ b/json/pickler/src/main/scala/sttp/tapir/json/pickler/PicklerConfiguration.scala @@ -13,6 +13,9 @@ final case class PicklerConfiguration(genericDerivationConfig: Configuration) { def withSnakeCaseDiscriminatorValues: PicklerConfiguration = PicklerConfiguration( genericDerivationConfig.withSnakeCaseDiscriminatorValues ) + def withScreamingSnakeCaseDiscriminatorValues: PicklerConfiguration = PicklerConfiguration( + genericDerivationConfig.withScreamingSnakeCaseDiscriminatorValues + ) def withKebabCaseDiscriminatorValues: PicklerConfiguration = PicklerConfiguration( genericDerivationConfig.withKebabCaseDiscriminatorValues ) diff --git a/json/pickler/src/test/scala/sttp/tapir/json/pickler/SchemaDerivationTest.scala b/json/pickler/src/test/scala/sttp/tapir/json/pickler/SchemaDerivationTest.scala index 4fbf927672..beaac68eff 100644 --- a/json/pickler/src/test/scala/sttp/tapir/json/pickler/SchemaDerivationTest.scala +++ b/json/pickler/src/test/scala/sttp/tapir/json/pickler/SchemaDerivationTest.scala @@ -354,6 +354,21 @@ class SchemaGenericAutoTest extends AsyncFlatSpec with Matchers with Inside { ) } + it should "generate one-of schema using the given discriminator (screaming snake case subtype names)" in { + implicit val customConf: PicklerConfiguration = + PicklerConfiguration.default.withDiscriminator("who_am_i").withScreamingSnakeCaseDiscriminatorValues + implicitlySchema[Entity].schemaType.asInstanceOf[SCoproduct[Entity]].discriminator shouldBe Some( + SDiscriminator( + FieldName("who_am_i"), + Map( + "ORGANIZATION" -> SRef(SName("sttp.tapir.json.pickler.Organization")), + "PERSON" -> SRef(SName("sttp.tapir.json.pickler.Person")), + "UNKNOWN_ENTITY" -> SRef(SName("sttp.tapir.json.pickler.UnknownEntity")) + ) + ) + ) + } + it should "generate one-of schema using the given discriminator (full subtype names)" in { implicit val customConf: PicklerConfiguration = PicklerConfiguration.default.withDiscriminator("who_am_i").withFullDiscriminatorValues implicitlySchema[Entity].schemaType.asInstanceOf[SCoproduct[Entity]].discriminator shouldBe Some(