|
7 | 7 | import org.junit.jupiter.api.Test;
|
8 | 8 |
|
9 | 9 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
| 10 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertNull; |
10 | 12 |
|
11 | 13 | public class RecordWithReadOnlyTest extends DatabindTestUtil {
|
12 | 14 |
|
@@ -50,6 +52,27 @@ public RecordWithReadOnlyAllAndNoArgConstructor() {
|
50 | 52 | }
|
51 | 53 | }
|
52 | 54 |
|
| 55 | + |
| 56 | + static class ReadOnly5049Pojo |
| 57 | + { |
| 58 | + protected String a, b; |
| 59 | + |
| 60 | + ReadOnly5049Pojo( |
| 61 | + @JsonProperty(value = "a", access = JsonProperty.Access.READ_ONLY) String a, |
| 62 | + @JsonProperty(value = "b", access = JsonProperty.Access.READ_ONLY) String b) { |
| 63 | + this.a = a; |
| 64 | + this.b = b; |
| 65 | + } |
| 66 | + |
| 67 | + public String getA() { return a; } |
| 68 | + public String getB() { return b; } |
| 69 | + } |
| 70 | + |
| 71 | + record ReadOnly5049Record( |
| 72 | + @JsonProperty(value = "a", access = JsonProperty.Access.READ_ONLY) String a, |
| 73 | + @JsonProperty(value = "b", access = JsonProperty.Access.READ_ONLY) String b) { |
| 74 | + } |
| 75 | + |
53 | 76 | private final ObjectMapper MAPPER = newJsonMapper();
|
54 | 77 |
|
55 | 78 | /*
|
@@ -82,14 +105,31 @@ public void testSerializeReadOnlyNamedProperty() throws Exception {
|
82 | 105 | assertEquals(a2q("{'id':123,'name':'Bob'}"), json);
|
83 | 106 | }
|
84 | 107 |
|
85 |
| - /** |
86 |
| - * Currently documents a bug where a property was NOT ignored during deserialization if given an explicit name. |
87 |
| - * Also reproducible in 2.14.x. |
88 |
| - */ |
| 108 | + |
89 | 109 | @Test
|
90 | 110 | public void testDeserializeReadOnlyNamedProperty() throws Exception {
|
91 | 111 | RecordWithReadOnlyNamedProperty value = MAPPER.readValue(a2q("{'id':123,'name':'Bob'}"), RecordWithReadOnlyNamedProperty.class);
|
92 |
| - assertEquals(new RecordWithReadOnlyNamedProperty(123, "Bob"), value); // BUG: should be `null` instead of "Bob" |
| 112 | + assertEquals(new RecordWithReadOnlyNamedProperty(123, null), value); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void testRoundtripPOJO() throws Exception |
| 117 | + { |
| 118 | + String json = MAPPER.writeValueAsString(new ReadOnly5049Pojo("hello", "world")); |
| 119 | + ReadOnly5049Pojo pojo = MAPPER.readerFor(ReadOnly5049Pojo.class).readValue(json); |
| 120 | + assertNotNull(pojo); |
| 121 | + assertNull(pojo.a); |
| 122 | + assertNull(pojo.b); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void testRoundtripRecord() throws Exception |
| 127 | + { |
| 128 | + String json = MAPPER.writeValueAsString(new ReadOnly5049Record("hello", "world")); |
| 129 | + ReadOnly5049Record record = MAPPER.readValue(json, ReadOnly5049Record.class); |
| 130 | + assertNotNull(record); |
| 131 | + assertNull(record.a()); |
| 132 | + assertNull(record.b()); |
93 | 133 | }
|
94 | 134 |
|
95 | 135 | /*
|
|
0 commit comments