|
| 1 | +package com.fasterxml.jackson.databind.tofix; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonUnwrapped; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 8 | +import com.fasterxml.jackson.databind.testutil.failure.JacksonTestFailureExpected; |
| 9 | + |
| 10 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 11 | + |
| 12 | +// [databind#5115] @JsonUnwrapped can't handle name collision #5115 |
| 13 | +public class RecordUnwrapped5115Test |
| 14 | + extends DatabindTestUtil |
| 15 | +{ |
| 16 | + record FooRecord5115(int a, int b) { } |
| 17 | + record BarRecordFail5115(@JsonUnwrapped FooRecord5115 a, int c) { } |
| 18 | + record BarRecordPass5115(@JsonUnwrapped FooRecord5115 foo, int c) { } |
| 19 | + |
| 20 | + static class FooPojo5115 { |
| 21 | + public int a; |
| 22 | + public int b; |
| 23 | + } |
| 24 | + |
| 25 | + static class BarPojo5115 { |
| 26 | + @JsonUnwrapped |
| 27 | + public FooPojo5115 a; |
| 28 | + public int c; |
| 29 | + } |
| 30 | + |
| 31 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 32 | + |
| 33 | + @Test |
| 34 | + void unwrappedPojoShouldRoundTrip() throws Exception |
| 35 | + { |
| 36 | + BarPojo5115 input = new BarPojo5115(); |
| 37 | + input.a = new FooPojo5115(); |
| 38 | + input.c = 4; |
| 39 | + input.a.a = 1; |
| 40 | + input.a.b = 2; |
| 41 | + |
| 42 | + String json = MAPPER.writeValueAsString(input); |
| 43 | + BarPojo5115 output = MAPPER.readValue(json, BarPojo5115.class); |
| 44 | + |
| 45 | + assertEquals(4, output.c); |
| 46 | + assertEquals(1, output.a.a); |
| 47 | + assertEquals(2, output.a.b); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void unwrappedRecordShouldRoundTripPass() throws Exception |
| 52 | + { |
| 53 | + BarRecordPass5115 input = new BarRecordPass5115(new FooRecord5115(1, 2), 3); |
| 54 | + |
| 55 | + // Serialize |
| 56 | + String json = MAPPER.writeValueAsString(input); |
| 57 | + |
| 58 | + // Deserialize (currently fails) |
| 59 | + BarRecordPass5115 output = MAPPER.readValue(json, BarRecordPass5115.class); |
| 60 | + |
| 61 | + // Should match after bug is fixed |
| 62 | + assertEquals(input, output); |
| 63 | + } |
| 64 | + |
| 65 | + @JacksonTestFailureExpected |
| 66 | + @Test |
| 67 | + void unwrappedRecordShouldRoundTrip() throws Exception |
| 68 | + { |
| 69 | + BarRecordFail5115 input = new BarRecordFail5115(new FooRecord5115(1, 2), 3); |
| 70 | + |
| 71 | + // Serialize |
| 72 | + String json = MAPPER.writeValueAsString(input); |
| 73 | + |
| 74 | + // Once the bug is fixed, this assertion will pass and the |
| 75 | + // @JacksonTestFailureExpected annotation can be removed. |
| 76 | + BarRecordFail5115 output = MAPPER.readValue(json, BarRecordFail5115.class); |
| 77 | + |
| 78 | + // Should match after bug is fixed |
| 79 | + assertEquals(input, output); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments