Skip to content

Commit b6d39dc

Browse files
committed
Clean up compilation error message at attempt to derive codec for class that has a transient field without default value
1 parent 3945444 commit b6d39dc

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,8 +924,10 @@ object JsonCodecMaker {
924924
val symbol = p.asTerm
925925
val name = decodeName(symbol)
926926
val annotationOption = annotations.get(name)
927-
if (annotationOption.exists(_.transient)) None
928-
else {
927+
if (annotationOption.exists(_.transient)) {
928+
if (symbol.isParamWithDefault) None
929+
else fail(s"Transient field '$name' in class '$tpe' should have default value.")
930+
} else {
929931
val mappedName = annotationOption.flatMap(_.partiallyMappedName)
930932
.getOrElse(cfg.fieldNameMapper.lift(name).getOrElse(name))
931933
val tmpName = TermName(s"_${symbol.name}")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2145,7 +2145,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
21452145
val construct = typeInfo.genNew(typeInfo.paramLists.map(_.foldLeft(new mutable.ListBuffer[Term]) { (params, fieldInfo) =>
21462146
params.addOne(if (fieldInfo.isTransient) {
21472147
fieldInfo.defaultValue
2148-
.getOrElse(fail(s"Transient field ${fieldInfo.symbol.name} in class ${tpe.show} have no default value"))
2148+
.getOrElse(fail(s"Transient field '${fieldInfo.symbol.name}' in class '${tpe.show}' should have default value."))
21492149
} else {
21502150
nonTransientFieldIndex += 1
21512151
Ref(readVars(nonTransientFieldIndex).symbol)

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3252,6 +3252,15 @@ class JsonCodecMakerSpec extends VerifyingSpec {
32523252
else "No implicit 'com.github.plokhotnyuk.jsoniter_scala.core.JsonValueCodec[_ >: scala.Nothing <: scala.Any]' defined for '_root_.java.util.Date'."
32533253
})
32543254
}
3255+
"don't generate codecs for classes with transient field without default value" in {
3256+
assert(intercept[TestFailedException](assertCompiles {
3257+
"""case class TransientFieldWithoutDefaultValue(@transient x: Int)
3258+
|
3259+
|JsonCodecMaker.make[TransientFieldWithoutDefaultValue]""".stripMargin
3260+
}).getMessage.contains {
3261+
"Transient field 'x' in class 'TransientFieldWithoutDefaultValue' should have default value."
3262+
})
3263+
}
32553264
}
32563265
"deserialize using the nullValue of codecs that are injected by implicits" in {
32573266
import test._

0 commit comments

Comments
 (0)