|
| 1 | +package com.fasterxml.jackson.databind.records; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 4 | +import com.fasterxml.jackson.annotation.JsonProperty.Access; |
| 5 | +import com.fasterxml.jackson.databind.BaseMapTest; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | + |
| 8 | +public class RecordWithReadOnlyTest extends BaseMapTest { |
| 9 | + |
| 10 | + record RecordWithReadOnly(int id, @JsonProperty(access = Access.READ_ONLY) String name) { |
| 11 | + } |
| 12 | + |
| 13 | + record RecordWithReadOnlyNamedProperty(int id, @JsonProperty(value = "name", access = Access.READ_ONLY) String name) { |
| 14 | + } |
| 15 | + |
| 16 | + record RecordWithReadOnlyAccessor(int id, String name) { |
| 17 | + |
| 18 | + @JsonProperty(access = Access.READ_ONLY) |
| 19 | + @Override |
| 20 | + public String name() { |
| 21 | + return name; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + record RecordWithReadOnlyComponentOverriddenAccessor(int id, @JsonProperty(access = Access.READ_ONLY) String name) { |
| 26 | + |
| 27 | + // @JsonProperty on overridden method is not automatically inherited by overriding method |
| 28 | + @Override |
| 29 | + public String name() { |
| 30 | + return name; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + record RecordWithReadOnlyPrimitiveType(@JsonProperty(access = Access.READ_ONLY) int id, String name) { |
| 35 | + } |
| 36 | + |
| 37 | + record RecordWithReadOnlyAll(@JsonProperty(access = Access.READ_ONLY) int id, |
| 38 | + @JsonProperty(access = Access.READ_ONLY) String name) { |
| 39 | + } |
| 40 | + |
| 41 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 42 | + |
| 43 | + /* |
| 44 | + /********************************************************************** |
| 45 | + /* Test methods, JsonProperty.access=READ_ONLY |
| 46 | + /********************************************************************** |
| 47 | + */ |
| 48 | + |
| 49 | + public void testSerializeReadOnlyProperty() throws Exception { |
| 50 | + String json = MAPPER.writeValueAsString(new RecordWithReadOnly(123, "Bob")); |
| 51 | + assertEquals(a2q("{'id':123,'name':'Bob'}"), json); |
| 52 | + } |
| 53 | + |
| 54 | + public void testDeserializeReadOnlyProperty() throws Exception { |
| 55 | + RecordWithReadOnly value = MAPPER.readValue(a2q("{'id':123,'name':'Bob'}"), RecordWithReadOnly.class); |
| 56 | + assertEquals(new RecordWithReadOnly(123, null), value); |
| 57 | + } |
| 58 | + |
| 59 | + /* |
| 60 | + /********************************************************************** |
| 61 | + /* Test methods, JsonProperty.access=READ_ONLY + JsonProperty.value=... |
| 62 | + /********************************************************************** |
| 63 | + */ |
| 64 | + |
| 65 | + public void testSerializeReadOnlyNamedProperty() throws Exception { |
| 66 | + String json = MAPPER.writeValueAsString(new RecordWithReadOnlyNamedProperty(123, "Bob")); |
| 67 | + assertEquals(a2q("{'name':'Bob','id':123}"), json); |
| 68 | + } |
| 69 | + |
| 70 | + public void testDeserializeReadOnlyNamedProperty() throws Exception { |
| 71 | + RecordWithReadOnlyNamedProperty value = MAPPER.readValue(a2q("{'id':123,'name':'Bob'}"), RecordWithReadOnlyNamedProperty.class); |
| 72 | + |
| 73 | + // BUG: Deserialization should've ignored 'name'. Also reproducible in 2.14.x. |
| 74 | + // assertEquals(new RecordWithReadOnlyNamedProperty(123, null), value); |
| 75 | + } |
| 76 | + |
| 77 | + /* |
| 78 | + /********************************************************************** |
| 79 | + /* Test methods, JsonProperty.access=READ_ONLY accessor |
| 80 | + /********************************************************************** |
| 81 | + */ |
| 82 | + |
| 83 | + public void testSerializeReadOnlyAccessor() throws Exception { |
| 84 | + String json = MAPPER.writeValueAsString(new RecordWithReadOnlyAccessor(123, "Bob")); |
| 85 | + assertEquals(a2q("{'id':123,'name':'Bob'}"), json); |
| 86 | + } |
| 87 | + |
| 88 | + public void testDeserializeReadOnlyAccessor() throws Exception { |
| 89 | + RecordWithReadOnlyAccessor expected = new RecordWithReadOnlyAccessor(123, null); |
| 90 | + |
| 91 | + assertEquals(expected, MAPPER.readValue(a2q("{'id':123}"), RecordWithReadOnlyAccessor.class)); |
| 92 | + assertEquals(expected, MAPPER.readValue(a2q("{'id':123,'name':null}"), RecordWithReadOnlyAccessor.class)); |
| 93 | + assertEquals(expected, MAPPER.readValue(a2q("{'id':123,'name':'Bob'}"), RecordWithReadOnlyAccessor.class)); |
| 94 | + } |
| 95 | + |
| 96 | + /* |
| 97 | + /********************************************************************** |
| 98 | + /* Test methods, JsonProperty.access=READ_ONLY component, but accessor method was overridden without re-annotating with JsonProperty.access=READ_ONLY |
| 99 | + /********************************************************************** |
| 100 | + */ |
| 101 | + |
| 102 | + public void testSerializeReadOnlyComponentOverrideAccessor() throws Exception { |
| 103 | + String json = MAPPER.writeValueAsString(new RecordWithReadOnlyComponentOverriddenAccessor(123, "Bob")); |
| 104 | + assertEquals(a2q("{'id':123,'name':'Bob'}"), json); |
| 105 | + } |
| 106 | + |
| 107 | + public void testDeserializeReadOnlyComponentOverrideAccessor() throws Exception { |
| 108 | + RecordWithReadOnlyComponentOverriddenAccessor expected = new RecordWithReadOnlyComponentOverriddenAccessor(123, null); |
| 109 | + |
| 110 | + assertEquals(expected, MAPPER.readValue(a2q("{'id':123}"), RecordWithReadOnlyComponentOverriddenAccessor.class)); |
| 111 | + assertEquals(expected, MAPPER.readValue(a2q("{'id':123,'name':null}"), RecordWithReadOnlyComponentOverriddenAccessor.class)); |
| 112 | + assertEquals(expected, MAPPER.readValue(a2q("{'id':123,'name':'Bob'}"), RecordWithReadOnlyComponentOverriddenAccessor.class)); |
| 113 | + } |
| 114 | + |
| 115 | + /* |
| 116 | + /********************************************************************** |
| 117 | + /* Test methods, JsonProperty.access=READ_ONLY parameter of primitive type |
| 118 | + /********************************************************************** |
| 119 | + */ |
| 120 | + |
| 121 | + public void testSerializeReadOnlyPrimitiveTypeProperty() throws Exception { |
| 122 | + String json = MAPPER.writeValueAsString(new RecordWithReadOnlyPrimitiveType(123, "Bob")); |
| 123 | + assertEquals(a2q("{'id':123,'name':'Bob'}"), json); |
| 124 | + } |
| 125 | + |
| 126 | + public void testDeserializeReadOnlyPrimitiveTypeProperty() throws Exception { |
| 127 | + RecordWithReadOnlyPrimitiveType value = MAPPER.readValue(a2q("{'id':123,'name':'Bob'}"), RecordWithReadOnlyPrimitiveType.class); |
| 128 | + assertEquals(new RecordWithReadOnlyPrimitiveType(0, "Bob"), value); |
| 129 | + } |
| 130 | + |
| 131 | + /* |
| 132 | + /********************************************************************** |
| 133 | + /* Test methods, JsonProperty.access=READ_ONLY all parameters |
| 134 | + /********************************************************************** |
| 135 | + */ |
| 136 | + |
| 137 | + public void testSerializeReadOnlyAllProperties() throws Exception { |
| 138 | + String json = MAPPER.writeValueAsString(new RecordWithReadOnlyAll(123, "Bob")); |
| 139 | + assertEquals(a2q("{'id':123,'name':'Bob'}"), json); |
| 140 | + } |
| 141 | + |
| 142 | + public void testDeserializeReadOnlyAllProperties() throws Exception { |
| 143 | + RecordWithReadOnlyAll value = MAPPER.readValue(a2q("{'id':123,'name':'Bob'}"), RecordWithReadOnlyAll.class); |
| 144 | + assertEquals(new RecordWithReadOnlyAll(0, null), value); |
| 145 | + } |
| 146 | +} |
0 commit comments