Skip to content

Allow non-boolean return type for "is-getters" with MapperFeature.ALLOW_IS_GETTERS_FOR_NON_BOOLEAN #3609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,15 @@ public enum MapperFeature implements ConfigFeature
*/
ALLOW_EXPLICIT_PROPERTY_RENAMING(false),

/**
* Feature that when enabled will allow getters with is-Prefix also for non-boolean return types.
* <p>
* Feature is disabled by default.
*
* @since 2.14
*/
ALLOW_IS_GETTERS_FOR_NON_BOOLEAN(false),

/*
/******************************************************
/* Coercion features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public interface BaseNameValidator {
protected final BaseNameValidator _baseNameValidator;

protected final boolean _stdBeanNaming;
protected final boolean _isGettersNonBoolean;

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

_stdBeanNaming = config.isEnabled(MapperFeature.USE_STD_BEAN_NAMING);
_isGettersNonBoolean = config.isEnabled(MapperFeature.ALLOW_IS_GETTERS_FOR_NON_BOOLEAN);
_mutatorPrefix = mutatorPrefix;
_getterPrefix = getterPrefix;
_isGetterPrefix = isGetterPrefix;
Expand All @@ -70,7 +72,7 @@ public String findNameForIsGetter(AnnotatedMethod am, String name)
{
if (_isGetterPrefix != null) {
final Class<?> rt = am.getRawType();
if (rt == Boolean.class || rt == Boolean.TYPE) {
if (_isGettersNonBoolean || rt == Boolean.class || rt == Boolean.TYPE) {
if (name.startsWith(_isGetterPrefix)) {
return _stdBeanNaming
? stdManglePropertyName(name, 2)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.fasterxml.jackson.databind.introspect;

import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;

import java.util.Collections;
import java.util.Map;

import static com.fasterxml.jackson.databind.MapperFeature.ALLOW_IS_GETTERS_FOR_NON_BOOLEAN;

public class IsGetterBoolean3609Test extends BaseMapTest {

static class POJO3609 {
int isEnabled;

protected POJO3609() { }
public POJO3609(int b) {
isEnabled = b;
}

public int isEnabled() { return isEnabled; }
public void setEnabled(int b) { isEnabled = b; }
}

public void testAllowIntIsGetter() throws Exception
{
ObjectMapper MAPPER = jsonMapperBuilder()
.enable(ALLOW_IS_GETTERS_FOR_NON_BOOLEAN)
.build();

POJO3609 input = new POJO3609(12);
final String json = MAPPER.writeValueAsString(input);

Map<?, ?> props = MAPPER.readValue(json, Map.class);
assertEquals(Collections.singletonMap("enabled", 12),
props);

POJO3609 output = MAPPER.readValue(json, POJO3609.class);
assertEquals(input.isEnabled, output.isEnabled);
}

public void testDisallowIntIsGetter() throws Exception
{
ObjectMapper MAPPER = jsonMapperBuilder()
.disable(ALLOW_IS_GETTERS_FOR_NON_BOOLEAN)
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.build();

POJO3609 input = new POJO3609(12);
final String json = MAPPER.writeValueAsString(input);

assertEquals("{}", json);

}
}