|
| 1 | +package com.fasterxml.jackson.databind.tofix; |
| 2 | + |
| 3 | +import java.util.Optional; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.annotation.JsonIgnore; |
| 8 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 11 | +import com.fasterxml.jackson.databind.testutil.failure.JacksonTestFailureExpected; |
| 12 | + |
| 13 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 14 | + |
| 15 | +public class RecordWithJsonIgnoredMethod5184Test |
| 16 | + extends DatabindTestUtil |
| 17 | +{ |
| 18 | + record TestData5184(@JsonProperty("test_property") String value) { |
| 19 | + @JsonIgnore |
| 20 | + public Optional<String> getValue() { |
| 21 | + return Optional.ofNullable(value); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + record TestData5184Alternate(@JsonProperty("test_property") String value) { |
| 26 | + @JsonIgnore |
| 27 | + public Optional<String> optionalValue() { |
| 28 | + return Optional.ofNullable(value); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + static final class TestData5184Class { |
| 33 | + private final String value; |
| 34 | + |
| 35 | + public TestData5184Class(@JsonProperty("test_property") String value) { |
| 36 | + this.value = value; |
| 37 | + } |
| 38 | + |
| 39 | + @JsonIgnore |
| 40 | + public Optional<String> getValue() { |
| 41 | + return Optional.ofNullable(value); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private static final ObjectMapper MAPPER = newJsonMapper(); |
| 46 | + |
| 47 | + @JacksonTestFailureExpected |
| 48 | + @Test |
| 49 | + void should_deserialize_json_to_test_data() throws Exception { |
| 50 | + String json = """ |
| 51 | + {"test_property":"test value"} |
| 52 | + """; |
| 53 | + |
| 54 | + var testData = MAPPER.readValue(json, TestData5184.class); |
| 55 | + |
| 56 | + assertThat(testData.value()).isEqualTo("test value"); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void should_deserialize_json_to_test_data_class() throws Exception { |
| 61 | + String json = """ |
| 62 | + {"test_property":"test value"} |
| 63 | + """; |
| 64 | + |
| 65 | + var testData = MAPPER.readValue(json, TestData5184Class.class); |
| 66 | + |
| 67 | + assertThat(testData.getValue()).contains("test value"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void should_deserialize_json_to_test_data_alternate() throws Exception { |
| 72 | + String json = """ |
| 73 | + {"test_property":"test value"} |
| 74 | + """; |
| 75 | + |
| 76 | + var testData = MAPPER.readValue(json, TestData5184Alternate.class); |
| 77 | + |
| 78 | + assertThat(testData.value()).isEqualTo("test value"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void should_not_deserialize_wrong_json_model_to_test_data() throws Exception { |
| 83 | + String json = """ |
| 84 | + {"value":"test value"} |
| 85 | + """; |
| 86 | + |
| 87 | + TestData5184 testData = MAPPER.readValue(json, TestData5184.class); |
| 88 | + |
| 89 | + assertThat(testData.value()).isNull(); |
| 90 | + } |
| 91 | +} |
0 commit comments