|
| 1 | +package com.fasterxml.jackson.databind.tofix; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.testutil.failure.JacksonTestFailureExpected; |
| 4 | +import org.junit.jupiter.api.Assertions; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 8 | +import com.fasterxml.jackson.annotation.JsonValue; |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | +import com.fasterxml.jackson.databind.cfg.MutableConfigOverride; |
| 11 | + |
| 12 | +import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_DEFAULT; |
| 13 | +import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL; |
| 14 | + |
| 15 | +// [databind#5312] Include.NON_DEFAULT regression for objects with @JsonValue |
| 16 | +public class JsonIncludeNonDefaultOnRecord5312Test |
| 17 | +{ |
| 18 | + record StringValue(String value) { |
| 19 | + @Override |
| 20 | + @JsonValue |
| 21 | + public String value() { |
| 22 | + return value; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + record Pojo1(StringValue value) { } |
| 27 | + |
| 28 | + @JsonInclude(JsonInclude.Include.NON_DEFAULT) |
| 29 | + record Pojo2(StringValue value) { } |
| 30 | + |
| 31 | + record Pojo3(@JsonInclude(JsonInclude.Include.NON_DEFAULT) StringValue value) { } |
| 32 | + |
| 33 | + |
| 34 | + @JacksonTestFailureExpected |
| 35 | + @Test |
| 36 | + void testSerialization() |
| 37 | + throws Exception |
| 38 | + { |
| 39 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 40 | + objectMapper.setSerializationInclusion(NON_DEFAULT); |
| 41 | + |
| 42 | + //might be relevant for analysis, but does not affect test outcome |
| 43 | + MutableConfigOverride objectStringConfigOverride = objectMapper.configOverride(String.class); |
| 44 | + objectStringConfigOverride.setIncludeAsProperty(JsonInclude.Value.construct(NON_NULL, NON_NULL)); |
| 45 | + |
| 46 | + //FAIL on jackson 2.18.2 / 2.20.0 |
| 47 | + Assertions.assertEquals("{\"value\":\"\"}", objectMapper.writeValueAsString(new Pojo1(new StringValue("")))); |
| 48 | + //PASS |
| 49 | + Assertions.assertEquals("{\"value\":\"\"}", objectMapper.writeValueAsString(new Pojo2(new StringValue("")))); |
| 50 | + //FAIL on jackson 2.18.2 / 2.20.0 |
| 51 | + Assertions.assertEquals("{\"value\":\"\"}", objectMapper.writeValueAsString(new Pojo3(new StringValue("")))); |
| 52 | + } |
| 53 | +} |
0 commit comments