|
| 1 | +package com.fasterxml.jackson.databind.tofix; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.annotation.*; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 10 | +import com.fasterxml.jackson.databind.testutil.failure.JacksonTestFailureExpected; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 17 | + |
| 18 | +// [databind#5342] JsonAnyGetter method serialization can override JsonProperty serialization on serialized name conflict |
| 19 | +public class AnyGetterNameConflictSerialization5342Test |
| 20 | + extends DatabindTestUtil |
| 21 | +{ |
| 22 | + public static class Pojo5342 { |
| 23 | + @JsonIgnore |
| 24 | + private Map<String, Object> additionalProperties; |
| 25 | + @JsonProperty(value = "additionalProperties") |
| 26 | + private Map<String, Object> hidden; |
| 27 | + |
| 28 | + @JsonAnySetter |
| 29 | + private void additionalProperties(String key, Object value) { |
| 30 | + if (additionalProperties == null) { |
| 31 | + additionalProperties = new HashMap<>(); |
| 32 | + } |
| 33 | + additionalProperties.put(key.replace("\\.", "."), value); |
| 34 | + } |
| 35 | + |
| 36 | + @JsonAnyGetter |
| 37 | + public Map<String, Object> additionalProperties() { |
| 38 | + return additionalProperties; |
| 39 | + } |
| 40 | + |
| 41 | + public Map<String, Object> hidden() { |
| 42 | + return hidden; |
| 43 | + } |
| 44 | + |
| 45 | + public void hidden(Map<String, Object> additionalPropertiesProperty) { |
| 46 | + this.hidden = additionalPropertiesProperty; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 51 | + |
| 52 | + @JacksonTestFailureExpected |
| 53 | + @Test |
| 54 | + public void anyGetter5342() |
| 55 | + throws Exception |
| 56 | + { |
| 57 | + Pojo5342 pojo = new Pojo5342(); |
| 58 | + pojo.additionalProperties("foo", "bar"); |
| 59 | + |
| 60 | + Map<String, Object> hidden = new HashMap<>(); |
| 61 | + hidden.put("fizz", "buzz"); |
| 62 | + pojo.hidden(hidden); |
| 63 | + |
| 64 | + |
| 65 | + String JSON = MAPPER.writeValueAsString(pojo); |
| 66 | + // was in 2.18 : {"foo":"bar","additionalProperties": {"fizz":"buzz"}} |
| 67 | + // now in 2.19 : {"foo":"bar"}... need FIX! |
| 68 | + // any-getter |
| 69 | + assertThat(JSON).contains("\"foo\":\"bar\""); |
| 70 | + // hidden field |
| 71 | + assertThat(JSON).contains("\"additionalProperties\":{\"fizz\":\"buzz\"}"); |
| 72 | + |
| 73 | + // Try deserializing back |
| 74 | + Pojo5342 actual = MAPPER.readValue(JSON, Pojo5342.class); |
| 75 | + assertNotNull(actual.additionalProperties()); |
| 76 | + assertEquals(1, actual.additionalProperties.size()); |
| 77 | + assertNotNull(actual.hidden()); |
| 78 | + assertEquals(1, actual.hidden().size()); |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments