`JsonProperty.Access.READ_ONLY` does not work with "getter-as-setter" `Collection`s
See original GitHub issueI could still encounter the problem that JsonProperty.Access.READ_ONLY
not work. I have read #1805 , and my jackson.databind version is 2.9.5. I also checked 2.9.6.
I have reduced my example as:
//AppTest.java
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
public class AppTest {
@Test
public void test() throws Exception {
String data ="{\"security_group_rules\": [{\"id\": \"id1\"}, {\"id\": \"id2\"}, {\"id\": \"id3\"}, {\"id\": \"id4\"}]}";
ObjectMapper mapper = new ObjectMapper();
SecurityGroup sg = mapper.readValue(data, SecurityGroup.class);
System.out.println(mapper.writeValueAsString(sg));
}
}
//SecurityGroup.java
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Lists;
import java.util.List;
public class SecurityGroup {
private List<SecurityGroupRule> securityGroupRules;
public SecurityGroup() {
this.securityGroupRules = Lists.newArrayList();
}
@JsonProperty(value="security_group_rules", access=JsonProperty.Access.READ_ONLY)
public List<SecurityGroupRule> getSecurityGroupRules() {
return securityGroupRules;
}
public SecurityGroup setSecurityGroupRules(List<SecurityGroupRule> securityGroupRules) {
this.securityGroupRules = securityGroupRules;
return this;
}
}
//SecurityGroupRule.java
import com.fasterxml.jackson.annotation.JsonProperty;
public class SecurityGroupRule {
private String id;
public SecurityGroupRule() {
}
@JsonProperty
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
I hope securityGroupRules
won’t be deserialized but it still is. If I change JsonProperty(value="security_group_rules", access=JsonProperty.Access.READ_ONLY)
to JsonIgnoreProperties(value="security_group_rules", allowGetters=true)
as metioned in #1805, it throws UnrecognizedPropertyException
instead of printing {"security_group_rules":[{"id":"id1"},{"id":"id2"},{"id":"id3"},{"id":"id4"}]}
. I’d like the former behaviour since in project I can set FAIL_ON_UNKNOWN_PROPERTIES
to avoid the error.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Security update for jackson-databind, jackson ... - SUSE
Maximize the value of open source with SUSE solution, backed by SUSE Support. ... Access.READ_ONLY' does not work with "getter-as-setter" ...
Read more >Security update for jackson-databind, jackson ... - Vulners
Access.READ_ONLY' does not work with "getter-as-setter" 'Collection's + ... Access.READ_ONLY' fails with collections when a property name is ...
Read more >com.fasterxml.jackson.databind.introspect ...
General data-binding functionality for Jackson: works on core streaming API. There is a newer version: 2.14.1. Show newest version. Maven; Gradle; Ivy; SBT....
Read more >StringArrayDeserializer (The Adobe AEM Quickstart and Web ...
Base implementation that does not assume specific type inclusion mechanism. ... readers" (for POJOs) and when Collections and Maps use "getter as setter"....
Read more >[jackson-databind] branch master created (now 2329833)
+ The Jackson Data Processor is a multi-purpose Java library for processing ... +* [JACKSON-795]: @JsonValue was not working for Maps, Collections
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jdussouillez I think you can achieve this by having both setter and getter, and configuring them separately (use
@JsonIgnore
on one you want to excluded,@JsonProperty
on one to include).Aside from trying to fix this issue (which I hope to achieve at some point; but fix is not trivial unfortunately), another possibility might be to add a setting for
@JsonFormat.Feature
that would allow enabling/disabling getter-as-setter on specific properties. Not sure if that would help here.I think I managed to fix this, although verification would be appreciated: fix in
2.12
branch, will be included in 2.12.0 (not backported since changes have non-trivial chance of regression so trying to avoid adding in a patch).