Skip to content

Commit 56201db

Browse files
committed
Fix of missing application of field mapper when some other supported annotation is used except @named
1 parent 05009e8 commit 56201db

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ object JsonCodecMaker {
826826
val classInfos = new mutable.LinkedHashMap[Type, ClassInfo]
827827

828828
def getClassInfo(tpe: Type): ClassInfo = classInfos.getOrElseUpdate(tpe, {
829-
case class FieldAnnotations(partiallyMappedName: String, transient: Boolean, stringified: Boolean)
829+
case class FieldAnnotations(partiallyMappedName: Option[String], transient: Boolean, stringified: Boolean)
830830

831831
def hasSupportedAnnotation(m: TermSymbol): Boolean = {
832832
m.info: Unit // to enforce the type information completeness and availability of annotations
@@ -862,7 +862,7 @@ object JsonCodecMaker {
862862
warn(s"Both $supportedTransientTypeNames and '${typeOf[named]}' or " +
863863
s"$supportedTransientTypeNames and '${typeOf[stringified]}' defined for '$name' of '$tpe'.")
864864
}
865-
val partiallyMappedName = namedValueOpt(m.annotations.find(_.tree.tpe =:= typeOf[named]), tpe).getOrElse(name)
865+
val partiallyMappedName = namedValueOpt(m.annotations.find(_.tree.tpe =:= typeOf[named]), tpe)
866866
annotations = annotations.updated(name, FieldAnnotations(partiallyMappedName, trans > 0, strings > 0))
867867
case _ =>
868868
}
@@ -877,8 +877,8 @@ object JsonCodecMaker {
877877
val annotationOption = annotations.get(name)
878878
if (annotationOption.exists(_.transient)) None
879879
else {
880-
val fieldNameMapper: String => String = n => cfg.fieldNameMapper.lift(n).getOrElse(n)
881-
val mappedName = annotationOption.fold(fieldNameMapper(name))(_.partiallyMappedName)
880+
val mappedName = annotationOption.flatMap(_.partiallyMappedName)
881+
.getOrElse(cfg.fieldNameMapper.lift(name).getOrElse(name))
882882
val tmpName = TermName("_" + symbol.name)
883883
val getter = getters.getOrElse(name,
884884
fail(s"'$name' parameter of '$tpe' should be defined as 'val' or 'var' in the primary constructor."))

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ object JsonCodecMaker {
885885
val classInfos = new mutable.LinkedHashMap[TypeRepr, ClassInfo]
886886

887887
def getClassInfo(tpe: TypeRepr): ClassInfo = classInfos.getOrElseUpdate(tpe, {
888-
case class FieldAnnotations(partiallyMappedName: String, transient: Boolean, stringified: Boolean)
888+
case class FieldAnnotations(partiallyMappedName: Option[String], transient: Boolean, stringified: Boolean)
889889

890890
def hasSupportedAnnotation(m: Symbol): Boolean = m.annotations.exists { a =>
891891
val tpe = a.tpe
@@ -912,7 +912,7 @@ object JsonCodecMaker {
912912
if ((named > 0 || strings > 0) && trans > 0)
913913
warn(s"Both $supportedTransientTypeNames and '${Type.show[named]}' or " +
914914
s"$supportedTransientTypeNames and '${Type.show[stringified]}' defined for '$name' of '${tpe.show}'.")
915-
val partiallyMappedName = namedValueOpt(m.annotations.find(_.tpe =:= TypeRepr.of[named]), tpe).getOrElse(name)
915+
val partiallyMappedName = namedValueOpt(m.annotations.find(_.tpe =:= TypeRepr.of[named]), tpe)
916916
annotations = annotations.updated(name, FieldAnnotations(partiallyMappedName, trans > 0, strings > 0))
917917
case _ =>
918918
}
@@ -926,7 +926,8 @@ object JsonCodecMaker {
926926
i += 1
927927
val name = symbol.name
928928
val annotationOption = annotations.get(name)
929-
val mappedName = annotationOption.fold(cfg.fieldNameMapper(name).getOrElse(name))(_.partiallyMappedName)
929+
val mappedName = annotationOption.flatMap(_.partiallyMappedName)
930+
.getOrElse(cfg.fieldNameMapper(name).getOrElse(name))
930931
val field = tpeClassSym.fieldMember(name)
931932
val getterOrField =
932933
if (field.exists) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,6 +2068,10 @@ class JsonCodecMakerSpec extends VerifyingSpec {
20682068
verifySerDeser(codecOfStringified, Stringified(1, 2, Option(1), List(2)),
20692069
"""{"i":"1","bi":"2","o":"1","l":["2"]}""")
20702070
}
2071+
"serialize and deserialize fields that have field name mapper and stringified by annotation" in {
2072+
verifySerDeser(make(CodecMakerConfig.withFieldNameMapper { case x => x + "_" }), Stringified(1, 2, Option(1), List(2)),
2073+
"""{"i_":"1","bi_":"2","o_":"1","l_":["2"]}""")
2074+
}
20712075
"throw parse exception when stringified fields have non-string values" in {
20722076
verifyDeserError(codecOfStringified, """{"i":1,"bi":"2","o":"1","l":["2"]}""",
20732077
"""expected '"', offset: 0x00000005""")

0 commit comments

Comments
 (0)