Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue when fields have default values #600

Merged
merged 1 commit into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,24 @@ object ScalaAnnotationIntrospector extends NopAnnotationIntrospector with ValueI
// Locate the constructor param that matches it
descriptor.properties.find(_.param.exists(_.index == creator.getCreatorIndex)) match {
case Some(pd) => {
val mappedCreator = overrides.get(pd.name) match {
case Some(refHolder) => WrappedCreatorProperty(creator, refHolder)
case _ => creator
}
if (applyDefaultValues) {
pd match {
case PropertyDescriptor(_, Some(ConstructorParameter(_, _, Some(defaultValue))), _, _, _, _, _) => {
mappedCreator.withNullProvider(new NullValueProvider {
val updatedCreator = creator.withNullProvider(new NullValueProvider {
override def getNullValue(ctxt: DeserializationContext): AnyRef = defaultValue()

override def getNullAccessPattern: AccessPattern = AccessPattern.DYNAMIC
})
updatedCreator match {
case cp: CreatorProperty => applyOverrides(cp, pd.name, overrides)
case cp => cp
}
}
case _ => {
applyOverrides(creator, pd.name, overrides)
}
case _ => mappedCreator
}
} else {
mappedCreator
applyOverrides(creator, pd.name, overrides)
}
}
case _ => creator
Expand All @@ -146,6 +147,14 @@ object ScalaAnnotationIntrospector extends NopAnnotationIntrospector with ValueI
}
}

private def applyOverrides(creator: CreatorProperty, propertyName: String,
overrides: Map[String, ClassHolder]): CreatorProperty = {
overrides.get(propertyName) match {
case Some(refHolder) => WrappedCreatorProperty(creator, refHolder)
case _ => creator
}
}

override def findValueInstantiator(config: DeserializationConfig, beanDesc: BeanDescription,
defaultInstantiator: ValueInstantiator): ValueInstantiator = {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class OptionWithNumberDeserializerTest extends DeserializerTest {
it should "deserialize OptionLongWithDefault when registerReferencedValueType is used" in {
ScalaAnnotationIntrospectorModule.registerReferencedValueType(classOf[OptionLongWithDefault], "valueLong", classOf[Long])
try {
val v1 = deserialize("""{"valueLong":151}""", classOf[OptionLong])
v1 shouldBe OptionLong(Some(151L))
val v1 = deserialize("""{"valueLong":151}""", classOf[OptionLongWithDefault])
v1 shouldBe OptionLongWithDefault(Some(151L))
v1.valueLong.get shouldBe 151L
//this next call will fail with a Scala unboxing exception unless you call ScalaAnnotationIntrospectorModule.registerReferencedValueType
//or use one of the equivalent classes in OptionWithNumberDeserializerTest
Expand Down