|
| 1 | +package com.fasterxml.jackson.databind.introspect; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.*; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.databind.*; |
| 8 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 9 | + |
| 10 | +import static org.junit.jupiter.api.Assertions.*; |
| 11 | + |
| 12 | +/** |
| 13 | + * Tests covering the [databind#5398] follow-up: when a property's setter is |
| 14 | + * {@code @JsonIgnore}d (and the getter carries {@code @JsonProperty} that |
| 15 | + * keeps the property non-ignored as a whole), the inferred field mutator |
| 16 | + * retained via {@code MapperFeature.INFER_PROPERTY_MUTATORS} must NOT be |
| 17 | + * used to bypass the ignored setter. The property should remain |
| 18 | + * serialization-only (read-only). |
| 19 | + */ |
| 20 | +public class JsonIgnoreSetterFieldBypass5398Test extends DatabindTestUtil |
| 21 | +{ |
| 22 | + static class RenamedGetterIgnoredSetter { |
| 23 | + private String prop; |
| 24 | + |
| 25 | + @JsonProperty("renamedProp") |
| 26 | + public String getProp() { |
| 27 | + return prop; |
| 28 | + } |
| 29 | + |
| 30 | + @JsonIgnore |
| 31 | + public void setProp(String prop) { |
| 32 | + this.prop = prop; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + static class StandardGetterIgnoredSetter { |
| 37 | + private String prop; |
| 38 | + |
| 39 | + @JsonProperty |
| 40 | + public String getProp() { |
| 41 | + return prop; |
| 42 | + } |
| 43 | + |
| 44 | + @JsonIgnore |
| 45 | + public void setProp(String prop) { |
| 46 | + this.prop = prop; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Sibling shape: READ_ONLY access on getter rather than @JsonIgnore on setter. |
| 51 | + static class ReadOnlyRenamedGetter { |
| 52 | + private String prop; |
| 53 | + |
| 54 | + @JsonProperty(value = "renamedProp", access = JsonProperty.Access.READ_ONLY) |
| 55 | + public String getProp() { |
| 56 | + return prop; |
| 57 | + } |
| 58 | + |
| 59 | + public void setProp(String prop) { |
| 60 | + this.prop = prop; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // 2.x defaults FAIL_ON_UNKNOWN_PROPERTIES to true; disable so the "not written" |
| 65 | + // outcome (rather than an exception) is what we assert on read paths. |
| 66 | + private final ObjectMapper MAPPER = jsonMapperBuilder() |
| 67 | + .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) |
| 68 | + .build(); |
| 69 | + |
| 70 | + @Test |
| 71 | + public void renamedGetterStillSerializes() throws Exception |
| 72 | + { |
| 73 | + RenamedGetterIgnoredSetter bean = new RenamedGetterIgnoredSetter(); |
| 74 | + bean.setProp("someValue"); |
| 75 | + assertEquals("{\"renamedProp\":\"someValue\"}", |
| 76 | + MAPPER.writeValueAsString(bean)); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void standardGetterStillSerializes() throws Exception |
| 81 | + { |
| 82 | + StandardGetterIgnoredSetter bean = new StandardGetterIgnoredSetter(); |
| 83 | + bean.setProp("someValue"); |
| 84 | + assertEquals("{\"prop\":\"someValue\"}", |
| 85 | + MAPPER.writeValueAsString(bean)); |
| 86 | + } |
| 87 | + |
| 88 | + // The renamed property must be read-only: @JsonIgnore on the setter |
| 89 | + // blocks the write, and the inferred field mutator must not bypass it. |
| 90 | + @Test |
| 91 | + public void renamedIgnoredSetterDoesNotBypassViaField() throws Exception |
| 92 | + { |
| 93 | + RenamedGetterIgnoredSetter result = MAPPER.readValue( |
| 94 | + "{\"renamedProp\":\"someValue\"}", |
| 95 | + RenamedGetterIgnoredSetter.class); |
| 96 | + assertNotNull(result); |
| 97 | + assertNull(result.getProp()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void standardIgnoredSetterDoesNotBypassViaField() throws Exception |
| 102 | + { |
| 103 | + StandardGetterIgnoredSetter result = MAPPER.readValue( |
| 104 | + "{\"prop\":\"someValue\"}", |
| 105 | + StandardGetterIgnoredSetter.class); |
| 106 | + assertNotNull(result); |
| 107 | + assertNull(result.getProp()); |
| 108 | + } |
| 109 | + |
| 110 | + // Control: feeding the implicit field name (rather than the renamed |
| 111 | + // public name) also does not write the field. |
| 112 | + @Test |
| 113 | + public void implicitFieldNameDoesNotWriteWhenSetterIgnored() throws Exception |
| 114 | + { |
| 115 | + RenamedGetterIgnoredSetter result = MAPPER.readerFor(RenamedGetterIgnoredSetter.class) |
| 116 | + .without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) |
| 117 | + .readValue("{\"prop\":\"someValue\"}"); |
| 118 | + assertNotNull(result); |
| 119 | + assertNull(result.getProp()); |
| 120 | + } |
| 121 | + |
| 122 | + // Bypass must also stay closed when INFER_PROPERTY_MUTATORS is disabled, |
| 123 | + // i.e. the fix is not just an artifact of inference being on. |
| 124 | + @Test |
| 125 | + public void inferMutatorsDisabledStillBlocksWrite() throws Exception |
| 126 | + { |
| 127 | + ObjectMapper mapper = jsonMapperBuilder() |
| 128 | + .disable(MapperFeature.INFER_PROPERTY_MUTATORS) |
| 129 | + .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) |
| 130 | + .build(); |
| 131 | + RenamedGetterIgnoredSetter result = mapper.readValue( |
| 132 | + "{\"renamedProp\":\"someValue\"}", |
| 133 | + RenamedGetterIgnoredSetter.class); |
| 134 | + assertNotNull(result); |
| 135 | + assertNull(result.getProp()); |
| 136 | + } |
| 137 | + |
| 138 | + // Sibling: @JsonProperty(access = READ_ONLY) on the renamed getter must |
| 139 | + // produce the same outcome — serialize under the new name, not write |
| 140 | + // back via the inferred field mutator. |
| 141 | + @Test |
| 142 | + public void readOnlyRenamedGetterIsSerializeOnly() throws Exception |
| 143 | + { |
| 144 | + ReadOnlyRenamedGetter bean = new ReadOnlyRenamedGetter(); |
| 145 | + bean.setProp("someValue"); |
| 146 | + assertEquals("{\"renamedProp\":\"someValue\"}", |
| 147 | + MAPPER.writeValueAsString(bean)); |
| 148 | + |
| 149 | + ReadOnlyRenamedGetter result = MAPPER.readValue( |
| 150 | + "{\"renamedProp\":\"someValue\"}", |
| 151 | + ReadOnlyRenamedGetter.class); |
| 152 | + assertNotNull(result); |
| 153 | + assertNull(result.getProp()); |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments