Skip to content

Commit

Permalink
Merge pull request #1976 from seglo/seglo/custom-schema-enum-discrimi…
Browse files Browse the repository at this point in the history
…nator

[BUG] oneOfUsingField fails for case object enums that inherit enumeratum EnumEntry reproducer
  • Loading branch information
adamw authored Mar 28, 2022
2 parents 2563701 + 1375714 commit 4354132
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object OneOfMacro {
import _root_.sttp.tapir.SchemaType._
import _root_.scala.collection.immutable.{List, Map}
val mappingAsList = List(..$mapping)
val mappingAsMap = mappingAsList.toMap
val mappingAsMap: Map[$weakTypeV, Schema[_]] = mappingAsList.toMap
val discriminator = SDiscriminator(
_root_.sttp.tapir.FieldName($name, $conf.toEncodedName($name)),
// cannot use .collect because of a bug in ScalaJS (Trying to access the this of another class ... during phase: jscode)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sttp.tapir.codec.enumeratum

import enumeratum.EnumEntry.Snakecase
import enumeratum._
import enumeratum.values._
import org.scalatest.flatspec.AnyFlatSpec
Expand All @@ -8,6 +9,8 @@ import sttp.tapir.Codec.PlainCodec
import sttp.tapir.Schema.SName
import sttp.tapir.Schema.annotations.{default, description}
import sttp.tapir.SchemaType.{SInteger, SString}
import sttp.tapir.generic.Derived
import sttp.tapir.generic.auto._
import sttp.tapir.{DecodeResult, Schema, Validator}

class TapirCodecEnumeratumTest extends AnyFlatSpec with Matchers {
Expand Down Expand Up @@ -100,9 +103,31 @@ class TapirCodecEnumeratumTest extends AnyFlatSpec with Matchers {
}

it should "find schema for enumeratum enum entries and enrich with metadata from default annotations" in {
implicitly[Schema[TestEnumEntryWithSomeEncodedDefault]].default shouldBe Some((TestEnumEntryWithSomeEncodedDefault.Value2, Some(TestEnumEntryWithSomeEncodedDefault.Value2)))
implicitly[Schema[TestEnumEntryWithSomeEncodedDefault]].default shouldBe Some(
(TestEnumEntryWithSomeEncodedDefault.Value2, Some(TestEnumEntryWithSomeEncodedDefault.Value2))
)
implicitly[Schema[TestEnumEntryWithNoEncodedDefault]].default shouldBe Some((TestEnumEntryWithNoEncodedDefault.Value2, None))
}

it should "create schema with custom discriminator based on enumeratum enum" in {
// given
sealed trait OfferType extends EnumEntry with Snakecase
object OfferType {
case object OfferOne extends OfferType
}

sealed trait CreateOfferRequest {
def `type`: OfferType
}

final case class CreateOfferOneRequest(`type`: OfferType) extends CreateOfferRequest

// then - should compile
val createOfferRequestSchema: Schema[CreateOfferRequest] = {
val one = implicitly[Derived[Schema[CreateOfferOneRequest]]].value
Schema.oneOfUsingField[CreateOfferRequest, OfferType](_.`type`, _.entryName)(OfferType.OfferOne -> one)
}
}
}

object TapirCodecEnumeratumTest {
Expand Down

0 comments on commit 4354132

Please sign in to comment.