Skip to content

Commit

Permalink
Added withScreamingSnakeCaseDiscriminatorValues to Configuration and …
Browse files Browse the repository at this point in the history
…PicklerConfiguration.
  • Loading branch information
Kamil Kloch committed Mar 14, 2024
1 parent 15b0ea8 commit c02cb55
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/src/main/scala/sttp/tapir/generic/Configuration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
14 changes: 14 additions & 0 deletions core/src/test/scala/sttp/tapir/generic/SchemaGenericAutoTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit c02cb55

Please sign in to comment.