JsonProperty.Access.READ_ONLY not work
See original GitHub issueI want to disable one field deserialization, so first I add annotaion JsonProperty to the field, but it still throw exception in deserialization process.
Then I tried JsonIgnoreProperties, and annotate the class with specified field, jackson ignores the field and works as expected.
Below is the code:
// User.java
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Arrays;
import java.util.List;
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public List<String> getRoles() {
return Arrays.asList("admin", "monitor");
}
}
// User1.java
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.Arrays;
import java.util.List;
@JsonIgnoreProperties(value={ "roles" }, allowGetters=true)
public class User1 {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getRoles() {
return Arrays.asList("admin", "monitor");
}
}
// AppTest.java
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class AppTest {
@Test
public void test1() throws Exception {
User user = new User();
user.setName("foo");
ObjectMapper objectMapper = new ObjectMapper();
String result;
result = objectMapper.writeValueAsString(user);
// will throw exception
User deserizeUser = objectMapper.readValue(result, User.class);
}
@Test
public void test2() throws Exception {
User1 user = new User1();
user.setName("foo");
ObjectMapper objectMapper = new ObjectMapper();
String result = objectMapper.writeValueAsString(user);
assertTrue(result.contains("roles"));
// Ok
User1 deserilizeUser = objectMapper.readValue(result, User1.class);
}
}
The first testcase will throw exception and it’s stacktrace is:
com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.UnsupportedOperationException) (through reference chain: kk.User["roles"]->java.util.Arrays$ArrayList[2])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:388)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:360)
at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:211)
at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:20)
at com.fasterxml.jackson.databind.deser.impl.SetterlessProperty.deserializeAndSet(SetterlessProperty.java:130)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:276)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3814)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2858)
at kk.AppTest.test1(AppTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)
at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:198)
... 29 more
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
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
No changes were made to compatibility (Java 6 baseline; Scala 2.10). ... setter/getter may or may not be visible (default value for `@JsonProperty.access` ......
Read more >Hide a Request Field in Swagger API - Baeldung
In such a scenario, we don't want the user to enter information related to the id field. ... @JsonProperty(access = JsonProperty.Access.
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
Yes, it sounds like this might be due to conflict between “getter-as-setter” (only used for
Collection
s) andREAD_ONLY
modifier. I agree in that READ_ONLY should work consistently and prevent assignment, as per Javadocs. So this sounds like a bug.I just released
2.9.4
so it should now be available generally (for anyone else reading).