Skip to content

Unable to override DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE with JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE  #4489

@JooHyukKim

Description

@JooHyukKim

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.17Issues planned at earliest for 2.17

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions