|
| 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 RecordWithIgnoreAccessor(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 RecordWithIgnoreComponentOverriddenAccessor(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 RecordWithIgnorePrimitiveType(@JsonProperty(access = Access.READ_ONLY) int id, String name) { |
| 35 | + } |
| 36 | + |
| 37 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 38 | + |
| 39 | + /* |
| 40 | + /********************************************************************** |
| 41 | + /* Test methods, JsonProperty.access=READ_ONLY |
| 42 | + /********************************************************************** |
| 43 | + */ |
| 44 | + |
| 45 | + public void testSerializeReadOnlyProperty() throws Exception { |
| 46 | + String json = MAPPER.writeValueAsString(new RecordWithReadOnly(123, "Bob")); |
| 47 | + assertEquals("{\"id\":123}", json); |
| 48 | + } |
| 49 | + |
| 50 | + public void testDeserializeReadOnlyProperty() throws Exception { |
| 51 | + RecordWithReadOnly value = MAPPER.readValue("{\"id\":123,\"name\":\"Bob\"}", |
| 52 | + RecordWithReadOnly.class); |
| 53 | + assertEquals(new RecordWithReadOnly(123, null), value); |
| 54 | + } |
| 55 | + |
| 56 | + /* |
| 57 | + /********************************************************************** |
| 58 | + /* Test methods, JsonProperty.access=READ_ONLY + JsonProperty.value=... |
| 59 | + /********************************************************************** |
| 60 | + */ |
| 61 | + |
| 62 | + public void testSerializeReadOnlyNamedProperty() throws Exception { |
| 63 | + String json = MAPPER.writeValueAsString(new RecordWithReadOnlyNamedProperty(123, "Bob")); |
| 64 | + assertEquals("{\"id\":123}", json); |
| 65 | + } |
| 66 | + |
| 67 | + public void testDeserializeReadOnlyNamedProperty() throws Exception { |
| 68 | + RecordWithReadOnlyNamedProperty value = MAPPER.readValue("{\"id\":123,\"name\":\"Bob\"}", RecordWithReadOnlyNamedProperty.class); |
| 69 | + assertEquals(new RecordWithReadOnlyNamedProperty(123, "Bob"), value); |
| 70 | + } |
| 71 | + |
| 72 | + /* |
| 73 | + /********************************************************************** |
| 74 | + /* Test methods, JsonProperty.access=READ_ONLY accessor |
| 75 | + /********************************************************************** |
| 76 | + */ |
| 77 | + |
| 78 | + public void testSerializeReadOnlyAccessor() throws Exception { |
| 79 | + String json = MAPPER.writeValueAsString(new RecordWithIgnoreAccessor(123, "Bob")); |
| 80 | + assertEquals("{\"id\":123}", json); |
| 81 | + } |
| 82 | + |
| 83 | + public void testDeserializeReadOnlyAccessor() throws Exception { |
| 84 | + RecordWithIgnoreAccessor expected = new RecordWithIgnoreAccessor(123, null); |
| 85 | + |
| 86 | + assertEquals(expected, MAPPER.readValue("{\"id\":123}", RecordWithIgnoreAccessor.class)); |
| 87 | + assertEquals(expected, MAPPER.readValue("{\"id\":123,\"name\":null}", RecordWithIgnoreAccessor.class)); |
| 88 | + assertEquals(expected, MAPPER.readValue("{\"id\":123,\"name\":\"Bob\"}", RecordWithIgnoreAccessor.class)); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /* |
| 93 | + /********************************************************************** |
| 94 | + /* Test methods, JsonProperty.access=READ_ONLY component, but accessor method was overridden without re-annotating with JsonProperty.access=READ_ONLY |
| 95 | + /********************************************************************** |
| 96 | + */ |
| 97 | + |
| 98 | + public void testSerializeReadOnlyComponentOverrideAccessor() throws Exception { |
| 99 | + String json = MAPPER.writeValueAsString(new RecordWithIgnoreComponentOverriddenAccessor(123, "Bob")); |
| 100 | + assertEquals("{\"id\":123}", json); |
| 101 | + } |
| 102 | + |
| 103 | + public void testDeserializeReadOnlyComponentOverrideAccessor() throws Exception { |
| 104 | + RecordWithIgnoreComponentOverriddenAccessor expected = new RecordWithIgnoreComponentOverriddenAccessor(123, null); |
| 105 | + |
| 106 | + assertEquals(expected, MAPPER.readValue("{\"id\":123}", RecordWithIgnoreComponentOverriddenAccessor.class)); |
| 107 | + assertEquals(expected, MAPPER.readValue("{\"id\":123,\"name\":null}", RecordWithIgnoreComponentOverriddenAccessor.class)); |
| 108 | + assertEquals(expected, MAPPER.readValue("{\"id\":123,\"name\":\"Bob\"}", RecordWithIgnoreComponentOverriddenAccessor.class)); |
| 109 | + } |
| 110 | + |
| 111 | + /* |
| 112 | + /********************************************************************** |
| 113 | + /* Test methods, JsonProperty.access=READ_ONLY parameter of primitive type |
| 114 | + /********************************************************************** |
| 115 | + */ |
| 116 | + |
| 117 | + public void testSerializeReadOnlyPrimitiveTypeProperty() throws Exception { |
| 118 | + String json = MAPPER.writeValueAsString(new RecordWithIgnorePrimitiveType(123, "Bob")); |
| 119 | + assertEquals("{\"name\":\"Bob\"}", json); |
| 120 | + } |
| 121 | + |
| 122 | + public void testDeserializeReadOnlyPrimitiveTypeProperty() throws Exception { |
| 123 | + RecordWithIgnorePrimitiveType value = MAPPER.readValue("{\"id\":123,\"name\":\"Bob\"}", RecordWithIgnorePrimitiveType.class); |
| 124 | + assertEquals(new RecordWithIgnorePrimitiveType(0, "Bob"), value); |
| 125 | + } |
| 126 | +} |
0 commit comments