Skip to content

Commit

Permalink
Allow override `DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING…
Browse files Browse the repository at this point in the history
…_DEFAULT_VALUE` with `JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE` (#4490)
  • Loading branch information
JooHyukKim authored Apr 20, 2024
1 parent 648fde5 commit e088857
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
4 changes: 4 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ Project: jackson-databind
#4481: Unable to override `DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL`
with `JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_AS_NULL`
(reported by @luozhenyu)
#4489: Unable to override `DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE`
with `JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE`
(reported by Joo-Hyuk K)
(fix by Joo-Hyuk K)
2.17.0 (12-Mar-2024)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,16 @@ protected boolean useNullForUnknownEnum(DeserializationContext ctxt) {

// @since 2.15
protected boolean useDefaultValueForUnknownEnum(DeserializationContext ctxt) {
return (_enumDefaultValue != null)
&& (Boolean.TRUE.equals(_useDefaultValueForUnknownEnum)
|| ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE));
// If we have a default value...
if (_enumDefaultValue != null) {
// Check if FormatFeature overrides exist first
if (_useDefaultValueForUnknownEnum != null) {
return _useDefaultValueForUnknownEnum;
}
// Otherwise, check the global setting
return ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE);
}
// No default value? then false
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
import com.fasterxml.jackson.annotation.JsonFormat;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -103,6 +101,22 @@ static class Book4481 {
public Color color;
}

enum Types {
@JsonEnumDefaultValue
DEFAULT_TYPE,
FAST, SLOW
}

static class SpeedWithoutDefaultOverride {
@JsonFormat(without = JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)
public Types type;
}

static class SpeedWithDefaultOverride {
@JsonFormat(with = JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)
public Types type;
}

/*
/**********************************************************
/* Test methods, basic
Expand Down Expand Up @@ -315,6 +329,38 @@ public void testEnumWithNullForUnknownValueEnumSet() throws Exception {
assertTrue(pojo.value.contains(MyEnum2352_3.B));
}

/**
* Test to verify that configuration via
* {@link com.fasterxml.jackson.annotation.JsonFormat.Feature#READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE}
* takes precedence over global configuration.
*/
@Test
public void testJsonEnumDefaultValueOverrideOverGlobalConfig() throws Exception {
final String UNKNOWN_JSON = a2q("{'type':'OOPS!'}");

// First, global configuration is ENABLED and JsonFeature configuration is DISABLED
// So the test should fail
try {
JsonMapper.builder()
.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)
.build()
.readValue(UNKNOWN_JSON, SpeedWithoutDefaultOverride.class);
fail();
} catch (InvalidFormatException e) {
verifyException(e, "Cannot deserialize value of type");
verifyException(e, "not one of the values accepted for Enum class");
}

// Second, global configuration is DISABLED and JsonFeature configuration is ENABLED
// So the test should pass
SpeedWithDefaultOverride pojo = JsonMapper.builder()
.disable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)
.build()
.readValue(UNKNOWN_JSON, SpeedWithDefaultOverride.class);

assertEquals(Types.DEFAULT_TYPE, pojo.type);
}

/*
/**********************************************************
/* Test methods, other
Expand Down

0 comments on commit e088857

Please sign in to comment.