Skip to content

Problem deserializing some type of Enums when using PropertyNamingStrategy #4302

@Badbond

Description

@Badbond

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

When using a mapper with a PropertyNamingStrategy configured, the following exception is thrown when trying to deserialize an enum that contains a field with the same name as one of the enum constants:


com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Multiple fields representing property "foo": tech.picnic.config.util.EnumDeserializationTest$SomeEnum#FOO vs tech.picnic.config.util.EnumDeserializationTest$SomeEnum#foo
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 1]
	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
	at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1887)
	at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:289)
	at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:265)
	at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:163)
[...]

It seems that now enum constants are also considered fields, which can clash with an enum's field when they are renamed. See also 2134584.

Version Information

2.16.1

Reproduction

  @Test
  void shouldDeserialize() throws IOException {
    var objectMapper =
            JsonMapper.builder()
              .propertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
              .build();
    assertThat(objectMapper.readValue("\"FOO\"", SomeEnum.class))
            .isEqualTo(SomeEnum.FOO);
  }

  enum SomeEnum {
    FOO(0);

    public final int foo;

    SomeEnum(int foo) {
      this.foo = foo;
    }
  }

Expected behavior

Similar to Jackson 2.15.3, I would expect this enum to be deserializable given we don't specify any mixins on the constants.

Additional context

The reproduction case above has a public field, but the issue is also apparent if the field is private and the following visibility is configured:

  .visibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
  .visibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.ANY)
  .visibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)

Metadata

Metadata

Assignees

No one assigned

    Labels

    has-failing-testIndicates that there exists a test case (under `failing/`) to reproduce the issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions