Skip to content

Commit 0809e7b

Browse files
authored
Allow non-boolean return type for "is-getters" with MapperFeature.ALLOW_IS_GETTERS_FOR_NON_BOOLEAN (#3609)
1 parent 0b04883 commit 0809e7b

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/main/java/com/fasterxml/jackson/databind/MapperFeature.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,15 @@ public enum MapperFeature implements ConfigFeature
486486
*/
487487
ALLOW_EXPLICIT_PROPERTY_RENAMING(false),
488488

489+
/**
490+
* Feature that when enabled will allow getters with is-Prefix also for non-boolean return types.
491+
* <p>
492+
* Feature is disabled by default.
493+
*
494+
* @since 2.14
495+
*/
496+
ALLOW_IS_GETTERS_FOR_NON_BOOLEAN(false),
497+
489498
/*
490499
/******************************************************
491500
/* Coercion features

src/main/java/com/fasterxml/jackson/databind/introspect/DefaultAccessorNamingStrategy.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public interface BaseNameValidator {
4141
protected final BaseNameValidator _baseNameValidator;
4242

4343
protected final boolean _stdBeanNaming;
44+
protected final boolean _isGettersNonBoolean;
4445

4546
protected final String _getterPrefix;
4647
protected final String _isGetterPrefix;
@@ -59,6 +60,7 @@ protected DefaultAccessorNamingStrategy(MapperConfig<?> config, AnnotatedClass f
5960
_forClass = forClass;
6061

6162
_stdBeanNaming = config.isEnabled(MapperFeature.USE_STD_BEAN_NAMING);
63+
_isGettersNonBoolean = config.isEnabled(MapperFeature.ALLOW_IS_GETTERS_FOR_NON_BOOLEAN);
6264
_mutatorPrefix = mutatorPrefix;
6365
_getterPrefix = getterPrefix;
6466
_isGetterPrefix = isGetterPrefix;
@@ -70,7 +72,7 @@ public String findNameForIsGetter(AnnotatedMethod am, String name)
7072
{
7173
if (_isGetterPrefix != null) {
7274
final Class<?> rt = am.getRawType();
73-
if (rt == Boolean.class || rt == Boolean.TYPE) {
75+
if (_isGettersNonBoolean || rt == Boolean.class || rt == Boolean.TYPE) {
7476
if (name.startsWith(_isGetterPrefix)) {
7577
return _stdBeanNaming
7678
? stdManglePropertyName(name, 2)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.fasterxml.jackson.databind.introspect;
2+
3+
import com.fasterxml.jackson.databind.BaseMapTest;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.SerializationFeature;
6+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
7+
8+
import java.util.Collections;
9+
import java.util.Map;
10+
11+
import static com.fasterxml.jackson.databind.MapperFeature.ALLOW_IS_GETTERS_FOR_NON_BOOLEAN;
12+
13+
public class IsGetterBoolean3609Test extends BaseMapTest {
14+
15+
static class POJO3609 {
16+
int isEnabled;
17+
18+
protected POJO3609() { }
19+
public POJO3609(int b) {
20+
isEnabled = b;
21+
}
22+
23+
public int isEnabled() { return isEnabled; }
24+
public void setEnabled(int b) { isEnabled = b; }
25+
}
26+
27+
public void testAllowIntIsGetter() throws Exception
28+
{
29+
ObjectMapper MAPPER = jsonMapperBuilder()
30+
.enable(ALLOW_IS_GETTERS_FOR_NON_BOOLEAN)
31+
.build();
32+
33+
POJO3609 input = new POJO3609(12);
34+
final String json = MAPPER.writeValueAsString(input);
35+
36+
Map<?, ?> props = MAPPER.readValue(json, Map.class);
37+
assertEquals(Collections.singletonMap("enabled", 12),
38+
props);
39+
40+
POJO3609 output = MAPPER.readValue(json, POJO3609.class);
41+
assertEquals(input.isEnabled, output.isEnabled);
42+
}
43+
44+
public void testDisallowIntIsGetter() throws Exception
45+
{
46+
ObjectMapper MAPPER = jsonMapperBuilder()
47+
.disable(ALLOW_IS_GETTERS_FOR_NON_BOOLEAN)
48+
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
49+
.build();
50+
51+
POJO3609 input = new POJO3609(12);
52+
final String json = MAPPER.writeValueAsString(input);
53+
54+
assertEquals("{}", json);
55+
56+
}
57+
}

0 commit comments

Comments
 (0)