Skip to content

Commit 281c2d9

Browse files
TimMooreTim Moore
authored andcommitted
Ignore generated methods for default arguments (#603)
Co-authored-by: Tim Moore <[email protected]>
1 parent 65a89a4 commit 281c2d9

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/main/scala/tools/jackson/module/scala/introspect/ScalaAnnotationIntrospectorModule.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class ScalaAnnotationIntrospectorInstance(scalaAnnotationIntrospectorModule: Sca
6464
override def hasIgnoreMarker(mapperConfig: MapperConfig[_], m: AnnotatedMember): Boolean = {
6565
val name = m.getName
6666
//special cases to prevent shadow fields associated with lazy vals being serialized
67-
name == "0bitmap$1" || name.endsWith("$lzy1") || super.hasIgnoreMarker(mapperConfig, m)
67+
name == "0bitmap$1" || name.endsWith("$lzy1") || name.contains("$default$") || super.hasIgnoreMarker(mapperConfig, m)
6868
}
6969

7070
private def hasCreatorAnnotation(a: Annotated): Boolean = {

src/test/scala/tools/jackson/module/scala/introspect/ScalaAnnotationIntrospectorTest.scala

Lines changed: 9 additions & 0 deletions
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

@@ -251,6 +254,12 @@ class ScalaAnnotationIntrospectorTest extends FixtureAnyFlatSpec with Matchers {
251254
cache.get(new ClassKey(classOf[CaseClassWithDefault])) should not be(null)
252255
}
253256

257+
it should "ignore a generated default argument method" in { mapper =>
258+
val bean = new GeneratedDefaultArgumentClass
259+
val allProps = getProps(mapper, bean)
260+
allProps shouldBe empty
261+
}
262+
254263
private def getProps(mapper: ObjectMapper, bean: AnyRef) = {
255264
val classIntrospector = mapper.serializationConfig().classIntrospectorInstance()
256265
val beanDescription: BeanDescription = classIntrospector.introspectForSerialization(mapper.constructType(bean.getClass))

src/test/scala/tools/jackson/module/scala/ser/CaseClassSerializerTest.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,13 @@ class CaseClassSerializerTest extends SerializerTest {
204204
it should "serialize ClassWithOnlyUnitField" in {
205205
serialize(ClassWithOnlyUnitField(())) shouldEqual """{}"""
206206
}
207+
208+
it should "not find properties for default arguments" in {
209+
case class GeneratedDefaultArgumentClass() {
210+
def getValue(value: String = "default"): String = value
211+
}
212+
213+
serialize(GeneratedDefaultArgumentClass()) shouldEqual "{}"
214+
}
215+
207216
}

0 commit comments

Comments
 (0)