Skip to content

Commit d8da222

Browse files
TimMooreTim Moore
and
Tim Moore
authored
Ignore generated methods for default arguments (#603)
Co-authored-by: Tim Moore <[email protected]>
1 parent 36c7656 commit d8da222

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ object ScalaAnnotationIntrospector extends NopAnnotationIntrospector with ValueI
4747
override def hasIgnoreMarker(m: AnnotatedMember): Boolean = {
4848
val name = m.getName
4949
//special cases to prevent shadow fields associated with lazy vals being serialized
50-
name == "0bitmap$1" || name.endsWith("$lzy1") || super.hasIgnoreMarker(m)
50+
name == "0bitmap$1" || name.endsWith("$lzy1") || name.contains("$default$") || super.hasIgnoreMarker(m)
5151
}
5252

5353
override def hasCreatorAnnotation(a: Annotated): Boolean = {

src/test/scala/com/fasterxml/jackson/module/scala/introspect/ScalaAnnotationIntrospectorTest.scala

+9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ object ScalaAnnotationIntrospectorTest {
3131
@JsonProperty def getValue: Int = value
3232
@JsonProperty def setValue(value: Int): Unit = { this.value = value }
3333
}
34+
class GeneratedDefaultArgumentClass {
35+
def getValue(value: String = "default"): String = value
36+
}
3437

3538
case class CaseClassWithDefault(a: String = "defaultParam", b: Option[String] = Some("optionDefault"), c: Option[String])
3639

@@ -241,6 +244,12 @@ class ScalaAnnotationIntrospectorTest extends FixtureAnyFlatSpec with Matchers {
241244
cache.get(new ClassKey(classOf[CaseClassWithDefault])) should not be(null)
242245
}
243246

247+
it should "ignore a generated default argument method" in { mapper =>
248+
val bean = new GeneratedDefaultArgumentClass
249+
val allProps = getProps(mapper, bean)
250+
allProps shouldBe empty
251+
}
252+
244253
private def getProps(mapper: ObjectMapper, bean: AnyRef) = {
245254
val config = mapper.getSerializationConfig
246255
val beanDescription: BeanDescription = config.introspect(mapper.constructType(bean.getClass))

src/test/scala/com/fasterxml/jackson/module/scala/ser/CaseClassSerializerTest.scala

+9
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,13 @@ class CaseClassSerializerTest extends SerializerTest {
198198
it should "serialize ClassWithOnlyUnitField" in {
199199
serialize(ClassWithOnlyUnitField(())) shouldEqual """{}"""
200200
}
201+
202+
it should "not find properties for default arguments" in {
203+
case class GeneratedDefaultArgumentClass() {
204+
def getValue(value: String = "default"): String = value
205+
}
206+
207+
serialize(GeneratedDefaultArgumentClass()) shouldEqual "{}"
208+
}
209+
201210
}

0 commit comments

Comments
 (0)