Skip to content

Commit d3a6d8c

Browse files
committed
issue when fields have default values (#600)
1 parent a73d2a2 commit d3a6d8c

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/main/scala/com/fasterxml/jackson/module/scala/introspect/ScalaAnnotationIntrospectorModule.scala

+17-8
Original file line numberDiff line numberDiff line change
@@ -170,23 +170,24 @@ object ScalaAnnotationIntrospector extends NopAnnotationIntrospector with ValueI
170170
// Locate the constructor param that matches it
171171
descriptor.properties.find(_.param.exists(_.index == creator.getCreatorIndex)) match {
172172
case Some(pd) => {
173-
val mappedCreator = overrides.get(pd.name) match {
174-
case Some(refHolder) => WrappedCreatorProperty(creator, refHolder)
175-
case _ => creator
176-
}
177173
if (applyDefaultValues) {
178174
pd match {
179175
case PropertyDescriptor(_, Some(ConstructorParameter(_, _, Some(defaultValue))), _, _, _, _, _) => {
180-
mappedCreator.withNullProvider(new NullValueProvider {
176+
val updatedCreator = creator.withNullProvider(new NullValueProvider {
181177
override def getNullValue(ctxt: DeserializationContext): AnyRef = defaultValue()
182-
183178
override def getNullAccessPattern: AccessPattern = AccessPattern.DYNAMIC
184179
})
180+
updatedCreator match {
181+
case cp: CreatorProperty => applyOverrides(cp, pd.name, overrides)
182+
case cp => cp
183+
}
184+
}
185+
case _ => {
186+
applyOverrides(creator, pd.name, overrides)
185187
}
186-
case _ => mappedCreator
187188
}
188189
} else {
189-
mappedCreator
190+
applyOverrides(creator, pd.name, overrides)
190191
}
191192
}
192193
case _ => creator
@@ -204,6 +205,14 @@ object ScalaAnnotationIntrospector extends NopAnnotationIntrospector with ValueI
204205
}
205206
}
206207

208+
private def applyOverrides(creator: CreatorProperty, propertyName: String,
209+
overrides: Map[String, ClassHolder]): CreatorProperty = {
210+
overrides.get(propertyName) match {
211+
case Some(refHolder) => WrappedCreatorProperty(creator, refHolder)
212+
case _ => creator
213+
}
214+
}
215+
207216
override def findValueInstantiator(config: DeserializationConfig, beanDesc: BeanDescription,
208217
defaultInstantiator: ValueInstantiator): ValueInstantiator = {
209218

src/test/scala/com/fasterxml/jackson/module/scala/deser/OptionWithNumberDeserializerTest.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class OptionWithNumberDeserializerTest extends DeserializerTest with BeforeAndAf
6060
it should "deserialize OptionLongWithDefault when registerReferencedValueType is used" in {
6161
ScalaAnnotationIntrospectorModule.registerReferencedValueType(classOf[OptionLongWithDefault], "valueLong", classOf[Long])
6262
try {
63-
val v1 = deserialize("""{"valueLong":151}""", classOf[OptionLong])
64-
v1 shouldBe OptionLong(Some(151L))
63+
val v1 = deserialize("""{"valueLong":151}""", classOf[OptionLongWithDefault])
64+
v1 shouldBe OptionLongWithDefault(Some(151L))
6565
v1.valueLong.get shouldBe 151L
6666
//this next call will fail with a Scala unboxing exception unless you call ScalaAnnotationIntrospectorModule.registerReferencedValueType
6767
//or use one of the equivalent classes in OptionWithNumberDeserializerTest

0 commit comments

Comments
 (0)