When deserializing, `JsonProperty.Access.READ_ONLY` not working in all cases
See original GitHub issuehi I can’t well english. please your understand.
until 2.9.0 successful. but fail from 2.9.1. 2.9.3 will still fail Has changed spec for access option in JsonProperty? or bug? I attach test code
Test Class
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
public class DeserializeTest {
private ObjectMapper objectMapper;
@Before
public void setUp(){
this.objectMapper = new ObjectMapper();
}
@Test
public void test() throws IOException {
String json = "{\"pubtrans\":\"\",\"name\":\"changyong\"}";
TestClass result = this.objectMapper.readValue(json, TestClass.class);
// jackson 2.9.0 is success, 2.9.1~2.9.3 is fail
assertThat(result.getPubtrans(), is(nullValue()));
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class TestClass{
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Pubtrans pubtrans;
private String name;
}
enum Pubtrans{
SUBWAY, BUS
}
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Jackson read-only property in method - Stack Overflow
How can I tell that I just want to 'departamentos' be serialized by the method value and be ignored in deserialization. I tried...
Read more >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 >JsonProperty.Access (Jackson-annotations 2.6.0 API)
Access setting that means that the property may only be written (set) for deserialization, but will not be read (get) on serialization, that...
Read more >SUSE-SU-2022:1678-1: important: Security update for jackson ...
As.EXISTING_PROPERTY + Can not deserialize json to enum value with Object-/Array-valued input, '@JsonCreator' + Fix to avoid problem with ...
Read more >Jackson – Decide What Fields Get Serialized/Deserialized
5. Make All Fields Globally Serializable ... In some cases where, for example, you might not actually be able to modify the source...
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
Hello,
I tested the DeserializeTest given by @LichKing-lee and it’s worked with version 2.9.6.
But if I set a value to the JsonProperty :
@JsonProperty(value = "pub_trans",access = JsonProperty.Access.READ_ONLY)
and of course change the json in the test :
String json = "{\"pub_trans\":\"A\",\"name\":\"changyong\"}";
The unit test fails with the exception
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "pub_trans"
It’s seems that I cannot combine access and value. I still found a workaround by adding
@JsonIgnoreProperties(value="pub_trans", allowGetters=true)
at class-level of the TestClassLooks like
READ_ONLY
is properly detected, added for Creator-property too. Not yet sure why it is still found – probably since creator property lookup is bit different in property-based approach, so maybe just need to add another pass over those. And then decide how to change behavior (remove entry in secondary lookup, change handler to no-operation).