Skip to content

Commit

Permalink
Fix #3080; also support Shape.STRING for boolean/Boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 16, 2021
1 parent 5bd538b commit 6d839c7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
5 changes: 5 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -1315,3 +1315,8 @@ Miguel G (Migwel@github)
Jelle Voost (jellevoost@github)
* Reported #3038: Two cases of incorrect error reporting about DeserializationFeature
(2.12.2)
Asaf Romano (asaf-romano@github)
* Reported #3080: configOverrides(boolean.class) silently ignored, whereas .configOverride(Boolean.class)
works for both primitives and boxed boolean values
(2.13.0)
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Project: jackson-databind
#3035: Add `removeMixIn()` method in `MapperBuilder`
#3036: Backport `MapperBuilder` lambda-taking methods: `withConfigOverride()`,
`withCoercionConfig()`, `withCoercionConfigDefaults()`
#3080: configOverrides(boolean.class) silently ignored, whereas .configOverride(Boolean.class)
works for both primitives and boxed boolean values
(reported by Asaf R)

2.12.2 (03-Mar-2021)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@ public BooleanSerializer(boolean forPrimitive) {
public JsonSerializer<?> createContextual(SerializerProvider serializers,
BeanProperty property) throws JsonMappingException
{
JsonFormat.Value format = findFormatOverrides(serializers,
property, Boolean.class);
// 16-Mar-2021, tatu: As per [databind#3080], was passing wrapper type
// always; should not have.
JsonFormat.Value format = findFormatOverrides(serializers, property,
handledType());
if (format != null) {
JsonFormat.Shape shape = format.getShape();
if (shape.isNumeric()) {
return new AsNumber(_forPrimitive);
}
if (shape == JsonFormat.Shape.STRING) {
return new ToStringSerializer(_handledType);
}
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,24 @@ public BooleanWrapper() { }
public BooleanWrapper(Boolean value) { b = value; }
}

// [databind#3080]
protected static class PrimitiveBooleanWrapper {
public boolean b;

public PrimitiveBooleanWrapper() { }
public PrimitiveBooleanWrapper(boolean value) { b = value; }
}

static class AltBoolean extends BooleanWrapper
{
public AltBoolean() { }
public AltBoolean(Boolean b) { super(b); }
}

/*
/**********************************************************
/**********************************************************************
/* Test methods
/**********************************************************
/**********************************************************************
*/

private final static ObjectMapper MAPPER = newJsonMapper();
Expand All @@ -56,11 +64,39 @@ public void testShapeViaDefaults() throws Exception
{
assertEquals(aposToQuotes("{'b':true}"),
MAPPER.writeValueAsString(new BooleanWrapper(true)));
ObjectMapper m = newJsonMapper();
m.configOverride(Boolean.class)
.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER));
ObjectMapper m = jsonMapperBuilder()
.withConfigOverride(Boolean.class,
cfg -> cfg.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER)
)).build();
assertEquals(aposToQuotes("{'b':1}"),
m.writeValueAsString(new BooleanWrapper(true)));

m = jsonMapperBuilder()
.withConfigOverride(Boolean.class,
cfg -> cfg.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING)
)).build();
assertEquals(aposToQuotes("{'b':'true'}"),
m.writeValueAsString(new BooleanWrapper(true)));
}

// [databind#3080]
public void testPrimitiveShapeViaDefaults() throws Exception
{
assertEquals(aposToQuotes("{'b':true}"),
MAPPER.writeValueAsString(new PrimitiveBooleanWrapper(true)));
ObjectMapper m = jsonMapperBuilder()
.withConfigOverride(Boolean.TYPE, cfg ->
cfg.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER))
).build();
assertEquals(aposToQuotes("{'b':1}"),
m.writeValueAsString(new PrimitiveBooleanWrapper(true)));

m = jsonMapperBuilder()
.withConfigOverride(Boolean.TYPE, cfg ->
cfg.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING))
).build();
assertEquals(aposToQuotes("{'b':'true'}"),
m.writeValueAsString(new PrimitiveBooleanWrapper(true)));
}

public void testShapeOnProperty() throws Exception
Expand Down

0 comments on commit 6d839c7

Please sign in to comment.