FasterXML / jackson-databind

General data-binding package for Jackson (2.x): works on streaming API (core) implementation(s)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to override `DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE` with `JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE`

JooHyukKim opened this issue · comments

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

After #4481, I checked if we have anything else in EnumDeserializer that won't override.
READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE config does not work also.

Version Information

2.17.0

Reproduction

(Copied from my PR)

    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
    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);
    }

Expected behavior

Be able to override DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE with JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE

Additional context

Filed a PR to fix this issue.