Skip to content

Commit 662a56e

Browse files
authored
Reproduce #5312 (#5327)
1 parent 9ccf8d6 commit 662a56e

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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

Comments
 (0)