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

[BUG] oneOfUsingField fails for case object enums that inherit enumeratum EnumEntry reproducer #1976

Merged
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 @@ -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