Skip to content

Commit

Permalink
Fix unwanted trimming of whitespaces for backtiked field names in Sca…
Browse files Browse the repository at this point in the history
…la 2 macros
  • Loading branch information
plokhotnyuk committed Jan 6, 2025
1 parent f23ddd2 commit a96f7dc
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,8 @@ object JsonCodecMaker {

def decodeName(s: Symbol): String = NameTransformer.decode(s.name.toString)

def decodeFieldName(s: Symbol): String = NameTransformer.decode(s.name.toString.trim) // FIXME: Why is there a space at the end of field name?!

def resolveConcreteType(tpe: Type, mtpe: Type): Type = {
val tpeTypeParams =
if (tpe.typeSymbol.isClass) tpe.typeSymbol.asClass.typeParams
Expand Down Expand Up @@ -820,7 +822,7 @@ object JsonCodecMaker {
val getters = tpe.members.collect { case m: MethodSymbol if m.isParamAccessor && m.isGetter => m }
val annotations = tpe.members.collect {
case m: TermSymbol if hasSupportedAnnotation(m) =>
val name = decodeName(m).trim // FIXME: Why is there a space at the end of field name?!
val name = decodeFieldName(m)
val named = m.annotations.filter(_.tree.tpe =:= typeOf[named])
if (named.size > 1) fail(s"Duplicated '${typeOf[named]}' defined for '$name' of '$tpe'.")
val trans = m.annotations.filter(a => a.tree.tpe =:= typeOf[transient] ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2136,9 +2136,9 @@ class JsonCodecMakerSpec extends VerifyingSpec {
WriterConfig.withEscapeUnicode(true))
}
"serialize and deserialize case classes with Scala operators in field names" in {
case class Operators(`=<>!#%^&|*/\\~+-:$`: Int)
case class Operators(` =<>!#%^&|*/\\~+-:$ `: Int)

verifySerDeser(make[Operators], Operators(7), """{"=<>!#%^&|*/\\~+-:$":7}""")
verifySerDeser(make[Operators], Operators(7), """{" =<>!#%^&|*/\\~+-:$ ":7}""")
}
"don't serialize default values of case classes that defined for fields when the transientDefault flag is on (by default)" in {
val codecOfDefaults: JsonValueCodec[Defaults] = make
Expand Down

0 comments on commit a96f7dc

Please sign in to comment.