Skip to content

Commit 4275de3

Browse files
committed
Fixed #1285 by adding missing support of the @named annotation for terms of Scala 3 enumerations
1 parent 21318cb commit 4275de3

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,13 +1531,14 @@ object JsonCodecMaker {
15311531
else '{ $in.unexpectedKeyError($l) }
15321532

15331533
def discriminatorValue(tpe: TypeRepr): String =
1534-
val named = tpe.typeSymbol.annotations.filter(_.tpe =:= TypeRepr.of[named])
1534+
val isEnum = tpe.termSymbol.flags.is(Flags.Enum)
1535+
var named = (if (isEnum) tpe.termSymbol else tpe.typeSymbol).annotations.filter(_.tpe =:= TypeRepr.of[named])
15351536
if (named.nonEmpty) {
15361537
if (named.size > 1) fail(s"Duplicated '${TypeRepr.of[named].show}' defined for '${tpe.show}'.")
15371538
namedValueOpt(named.headOption, tpe).get
15381539
} else cfg.adtLeafClassNameMapper({
15391540
if (tpe =:= TypeRepr.of[None.type]) "scala.None"
1540-
else if (tpe.termSymbol.flags.is(Flags.Enum)) {
1541+
else if (isEnum) {
15411542
tpe match
15421543
case TermRef(_, name) => name
15431544
case _ => fail(s"Unsupported enum type: '${tpe.show}', tree=$tpe")

jsoniter-scala-macros/shared/src/test/scala-3/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMakerNewEnumSpec.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ enum LinkedList[+T]:
7575
case End
7676
case Node(value: T, next: LinkedList[T])
7777

78+
enum Side:
79+
@named("0") case Buy
80+
@named("1") case Sell
81+
7882
enum ClientOut(@transient val tpe: String):
7983
@named("p") case Ping(@named("l") timestamp: Long) extends ClientOut("p")
8084
@named("m") case Move(@named("m") move: Long) extends ClientOut("m")
@@ -157,6 +161,9 @@ class JsonCodecMakerNewEnumSpec extends VerifyingSpec {
157161
verifySerDeser(make[List[FooEnum[Option]]], List(FooEnum.Bar[Option](Some(1)), FooEnum.Baz[Option](Some("VVV"))),
158162
"""[{"type":"Bar","a":1},{"type":"Baz","a":"VVV"}]""")
159163
}
164+
"serialize and deserialize Scala3 enums with cases that use named annotation" in {
165+
verifySerDeser(makeWithoutDiscriminator[List[Side]], List(Side.Buy, Side.Sell), """["0","1"]""")
166+
}
160167
"serialize and deserialize Scala3 enums with a transient field" in {
161168
verifySerDeser(make[List[ClientOut]](CodecMakerConfig.withDiscriminatorFieldName(Some("t"))),
162169
List(ClientOut.Ping(1), ClientOut.Move(1)), """[{"t":"p","l":1},{"t":"m","m":1}]""")

0 commit comments

Comments
 (0)