Skip to content

Commit 6d839c7

Browse files
committed
Fix #3080; also support Shape.STRING for boolean/Boolean
1 parent 5bd538b commit 6d839c7

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

release-notes/CREDITS-2.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,3 +1315,8 @@ Miguel G (Migwel@github)
13151315
Jelle Voost (jellevoost@github)
13161316
* Reported #3038: Two cases of incorrect error reporting about DeserializationFeature
13171317
(2.12.2)
1318+
1319+
Asaf Romano (asaf-romano@github)
1320+
* Reported #3080: configOverrides(boolean.class) silently ignored, whereas .configOverride(Boolean.class)
1321+
works for both primitives and boxed boolean values
1322+
(2.13.0)

release-notes/VERSION-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ Project: jackson-databind
1717
#3035: Add `removeMixIn()` method in `MapperBuilder`
1818
#3036: Backport `MapperBuilder` lambda-taking methods: `withConfigOverride()`,
1919
`withCoercionConfig()`, `withCoercionConfigDefaults()`
20+
#3080: configOverrides(boolean.class) silently ignored, whereas .configOverride(Boolean.class)
21+
works for both primitives and boxed boolean values
22+
(reported by Asaf R)
2023

2124
2.12.2 (03-Mar-2021)
2225

src/main/java/com/fasterxml/jackson/databind/ser/std/BooleanSerializer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,18 @@ public BooleanSerializer(boolean forPrimitive) {
4747
public JsonSerializer<?> createContextual(SerializerProvider serializers,
4848
BeanProperty property) throws JsonMappingException
4949
{
50-
JsonFormat.Value format = findFormatOverrides(serializers,
51-
property, Boolean.class);
50+
// 16-Mar-2021, tatu: As per [databind#3080], was passing wrapper type
51+
// always; should not have.
52+
JsonFormat.Value format = findFormatOverrides(serializers, property,
53+
handledType());
5254
if (format != null) {
5355
JsonFormat.Shape shape = format.getShape();
5456
if (shape.isNumeric()) {
5557
return new AsNumber(_forPrimitive);
5658
}
59+
if (shape == JsonFormat.Shape.STRING) {
60+
return new ToStringSerializer(_handledType);
61+
}
5762
}
5863
return this;
5964
}

src/test/java/com/fasterxml/jackson/databind/format/BooleanFormatTest.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,24 @@ public BooleanWrapper() { }
3838
public BooleanWrapper(Boolean value) { b = value; }
3939
}
4040

41+
// [databind#3080]
42+
protected static class PrimitiveBooleanWrapper {
43+
public boolean b;
44+
45+
public PrimitiveBooleanWrapper() { }
46+
public PrimitiveBooleanWrapper(boolean value) { b = value; }
47+
}
48+
4149
static class AltBoolean extends BooleanWrapper
4250
{
4351
public AltBoolean() { }
4452
public AltBoolean(Boolean b) { super(b); }
4553
}
4654

4755
/*
48-
/**********************************************************
56+
/**********************************************************************
4957
/* Test methods
50-
/**********************************************************
58+
/**********************************************************************
5159
*/
5260

5361
private final static ObjectMapper MAPPER = newJsonMapper();
@@ -56,11 +64,39 @@ public void testShapeViaDefaults() throws Exception
5664
{
5765
assertEquals(aposToQuotes("{'b':true}"),
5866
MAPPER.writeValueAsString(new BooleanWrapper(true)));
59-
ObjectMapper m = newJsonMapper();
60-
m.configOverride(Boolean.class)
61-
.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER));
67+
ObjectMapper m = jsonMapperBuilder()
68+
.withConfigOverride(Boolean.class,
69+
cfg -> cfg.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER)
70+
)).build();
6271
assertEquals(aposToQuotes("{'b':1}"),
6372
m.writeValueAsString(new BooleanWrapper(true)));
73+
74+
m = jsonMapperBuilder()
75+
.withConfigOverride(Boolean.class,
76+
cfg -> cfg.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING)
77+
)).build();
78+
assertEquals(aposToQuotes("{'b':'true'}"),
79+
m.writeValueAsString(new BooleanWrapper(true)));
80+
}
81+
82+
// [databind#3080]
83+
public void testPrimitiveShapeViaDefaults() throws Exception
84+
{
85+
assertEquals(aposToQuotes("{'b':true}"),
86+
MAPPER.writeValueAsString(new PrimitiveBooleanWrapper(true)));
87+
ObjectMapper m = jsonMapperBuilder()
88+
.withConfigOverride(Boolean.TYPE, cfg ->
89+
cfg.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER))
90+
).build();
91+
assertEquals(aposToQuotes("{'b':1}"),
92+
m.writeValueAsString(new PrimitiveBooleanWrapper(true)));
93+
94+
m = jsonMapperBuilder()
95+
.withConfigOverride(Boolean.TYPE, cfg ->
96+
cfg.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING))
97+
).build();
98+
assertEquals(aposToQuotes("{'b':'true'}"),
99+
m.writeValueAsString(new PrimitiveBooleanWrapper(true)));
64100
}
65101

66102
public void testShapeOnProperty() throws Exception

0 commit comments

Comments
 (0)