@JsonProperty(access = Access.READ_ONLY) does not work
See original GitHub issueFails to work with 2.11.1
Test to reproduce the issue:
Bug.java
public class Bug {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
Bean bean = new Bean();
bean.setWorks("works");
String json = objectMapper.writeValueAsString(bean);
Bean newBean = objectMapper.readValue(json, Bean.class);
}
}
Bean.java
import com.fasterxml.jackson.annotation.JsonProperty;
public class Bean {
private String works;
@JsonProperty(value = "t", access = JsonProperty.Access.READ_ONLY)
public String getDoesntWork() {
return "pleaseFixThisBug";
}
public String getWorks() {
return works;
}
public void setWorks(String works) {
this.works = works;
}
}
Exception:
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "t" (class Bean), not marked as ignorable (one known property: "works"])
at [Source: (String)"{"works":"works","t":"pleaseFixThisBug"}"; line: 1, column: 23] (through reference chain: Bean["t"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:855)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1212)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1604)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1582)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:299)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:156)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4482)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3434)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3402)
at Bug.main(Bug.java:11)
Expected behaviour: The property should only be serialized. However, it is being tried to deserialized.
This bug was supposed to be fixed with https://github.com/FasterXML/jackson-databind/issues/935
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top Results From Across the Web
JsonProperty.Access.READ_ONLY not work #1805 - GitHub
Then I tried JsonIgnoreProperties, and annotate the class with specified field, jackson ignores the field and works as expected.
Read more >Jackson read-only property in method - Stack Overflow
I tried @JsonIgnoreProperty , @JsonGetter and @JsonProperty(access = JsonProperty. Access. READ_ONLY) on the method but nothing works.
Read more >JsonProperty.Access (Jackson-annotations 2.6.0 API)
Access setting that means that the property may only be read for serialization, but not written (set) during deserialization. WRITE_ONLY. public static final ......
Read more >Jackson Readonly properties and swagger UI
If the property is marking as readonly, it will be ignored when something is posted, but it will be available on the response....
Read more >Best bits of Jackson 2.6 - cowtowncoder - Medium
AUTO: Default visibility rules are used, setter/getter may or may not be visible (default value for `@JsonProperty.access`. JsonPointer-filtering for JsonParser ...
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 Free
Top 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
May I know when is the release of 2.12.0 planned?
@RakshithVP earlier I was hoping to get release out by September 2020, but that may slip – I still hope the first release candidate would be out in september.