Skip to content

Ignore generated methods for default arguments #603

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

Merged
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 @@ -47,7 +47,7 @@ object ScalaAnnotationIntrospector extends NopAnnotationIntrospector with ValueI
override def hasIgnoreMarker(m: AnnotatedMember): Boolean = {
val name = m.getName
//special cases to prevent shadow fields associated with lazy vals being serialized
name == "0bitmap$1" || name.endsWith("$lzy1") || super.hasIgnoreMarker(m)
name == "0bitmap$1" || name.endsWith("$lzy1") || name.contains("$default$") || super.hasIgnoreMarker(m)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this is really the best way to filter these methods out, but it's the best way I could find.

Based on the spec at https://docs.scala-lang.org/sips/named-and-default-arguments.html#default-arguments-1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks - probably the best way to go

}

override def hasCreatorAnnotation(a: Annotated): Boolean = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ object ScalaAnnotationIntrospectorTest {
@JsonProperty def getValue: Int = value
@JsonProperty def setValue(value: Int): Unit = { this.value = value }
}
class GeneratedDefaultArgumentClass {
def getValue(value: String = "default"): String = value
}

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

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

it should "ignore a generated default argument method" in { mapper =>
val bean = new GeneratedDefaultArgumentClass
val allProps = getProps(mapper, bean)
allProps shouldBe empty
}

private def getProps(mapper: ObjectMapper, bean: AnyRef) = {
val config = mapper.getSerializationConfig
val beanDescription: BeanDescription = config.introspect(mapper.constructType(bean.getClass))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,13 @@ class CaseClassSerializerTest extends SerializerTest {
it should "serialize ClassWithOnlyUnitField" in {
serialize(ClassWithOnlyUnitField(())) shouldEqual """{}"""
}

it should "not find properties for default arguments" in {
case class GeneratedDefaultArgumentClass() {
def getValue(value: String = "default"): String = value
}

serialize(GeneratedDefaultArgumentClass()) shouldEqual "{}"
}

}