@@ -16,6 +16,28 @@ object ScalaAnnotationIntrospector extends NopAnnotationIntrospector with ValueI
16
16
private [this ] var _descriptorCache : LookupCache [ClassKey , BeanDescriptor ] =
17
17
new LRUMap [ClassKey , BeanDescriptor ](16 , 100 )
18
18
19
+ private case class FieldKey (clazz : Class [_], fieldName : String )
20
+
21
+ private val overrideMap = scala.collection.mutable.Map [FieldKey , Class [_]]()
22
+
23
+ /**
24
+ * jackson-module-scala does not always properly handle deserialization of Options or Collections wrapping
25
+ * Scala primitives (eg Int, Long, Boolean). There are general issues with serializing and deserializing
26
+ * Scala 2 Enumerations. These issues can be worked around by adding Jackson annotations on the affected fields.
27
+ * This function is designed to be used when it is not possible to apply Jackson annotations.
28
+ *
29
+ * @param clazz
30
+ * @param fieldName
31
+ * @param referencedType
32
+ */
33
+ def registerReferencedType (clazz : Class [_], fieldName : String , referencedType : Class [_]): Unit = {
34
+ overrideMap.update(FieldKey (clazz, fieldName), referencedType)
35
+ }
36
+
37
+ def clearRegisteredReferencedTypes (): Unit = {
38
+ overrideMap.clear()
39
+ }
40
+
19
41
def setDescriptorCache (cache : LookupCache [ClassKey , BeanDescriptor ]): LookupCache [ClassKey , BeanDescriptor ] = {
20
42
val existingCache = _descriptorCache
21
43
_descriptorCache = cache
@@ -109,27 +131,33 @@ object ScalaAnnotationIntrospector extends NopAnnotationIntrospector with ValueI
109
131
val applyDefaultValues = config.isEnabled(MapperFeature .APPLY_DEFAULT_VALUES )
110
132
val args = delegate.getFromObjectArguments(config)
111
133
Option (args) match {
112
- case Some (array) => {
134
+ case Some (array) if (applyDefaultValues || overrideMap.nonEmpty) => {
113
135
array.map {
114
136
case creator : CreatorProperty => {
115
- if (applyDefaultValues) {
116
- // Locate the constructor param that matches it
117
- descriptor.properties.find(_.param.exists(_.index == creator.getCreatorIndex)) match {
118
- case Some (PropertyDescriptor (name, Some (ConstructorParameter (_, _, Some (defaultValue))), _, _, _, _, _)) =>
119
- creator.withNullProvider(new NullValueProvider {
120
- override def getNullValue (ctxt : DeserializationContext ): AnyRef = defaultValue()
121
-
122
- override def getNullAccessPattern : AccessPattern = AccessPattern .DYNAMIC
123
- })
124
- case _ => creator
137
+ // Locate the constructor param that matches it
138
+ descriptor.properties.find(_.param.exists(_.index == creator.getCreatorIndex)) match {
139
+ case Some (pd) => {
140
+ if (applyDefaultValues) {
141
+ pd match {
142
+ case PropertyDescriptor (_, Some (ConstructorParameter (_, _, Some (defaultValue))), _, _, _, _, _) => {
143
+ creator.withNullProvider(new NullValueProvider {
144
+ override def getNullValue (ctxt : DeserializationContext ): AnyRef = defaultValue()
145
+
146
+ override def getNullAccessPattern : AccessPattern = AccessPattern .DYNAMIC
147
+ })
148
+ }
149
+ case _ => creator
150
+ }
151
+ } else {
152
+ creator
153
+ }
125
154
}
126
- } else {
127
- creator
155
+ case _ => creator
128
156
}
129
157
}
130
- case other => other
131
158
}
132
159
}
160
+ case Some (array) => array
133
161
case _ => Array .empty
134
162
}
135
163
}
0 commit comments