Unexpected @JsonIgnore Annotation Application on Custom isXxx()
Method with Non-Boolean Field
#257
-
Description of the IssueUpon utilizing the Jackson library for serialization of a Java Bean, it has been observed that a custom Steps to Reproduce
// Bean Class
@Data
public class MyBean {
private Integer enhanceChances;
@JsonIgnore
public boolean isEnhanceChances() {
// Method Implementation
}
}
// Serialization Code
ObjectMapper mapper = new ObjectMapper();
MyBean bean = new MyBean();
String json = mapper.writeValueAsString(bean); Expected Behavior
Actual Behavior
Environment Information
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
</dependencies> Relevant Code SnippetRefer to the for (AnnotatedMethod m : _classDef.memberMethods()) {
...
implName = ai.findImplicitPropertyName(m);
if (implName == null) {
implName = _accessorNaming.findNameForRegularGetter(m, m.getName());
}
if (implName == null) { // if not, must skip
implName = _accessorNaming.findNameForIsGetter(m, m.getName());
if (implName == null) {
return;
}
visible = _visibilityChecker.isIsGetterVisible(m);
} else {
visible = _visibilityChecker.isGetterVisible(m);
}
} Note: This code snippet highlights the potential source of the issue, where the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hmmm, this could be caused by Lombok-generated class files in a shape u do not expect. Could u try without Lombok and see how it does? |
Beta Was this translation helpful? Give feedback.
-
This is behaving as intended: only name of field needs to match accessor-implied name; types not. So this is behaving as designed. If you want to include field in such case, add separate |
Beta Was this translation helpful? Give feedback.
This is behaving as intended: only name of field needs to match accessor-implied name; types not.
(there is logic for requiring
boolean
orBoolean
for "is-getter"s, however -- but getter/setter/field types need not match; also note there isMapperFeature.ALLOW_IS_GETTERS_FOR_NON_BOOLEAN
to allow non-boolean-valued getters)So this is behaving as designed. If you want to include field in such case, add separate
@JsonProperty
(in addition to@JsonIgnore
on accessor).