Skip to content

EnumDeserializer fails to deserialize Enums with @JsonValue - uses table with name() key instead of @JsonValue key #5271

@mikereiche

Description

@mikereiche

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

EnumDeserializer fails to deserialize Enums with @jsonvalue - uses table with name() key instead of @jsonvalue key.

The difference between Jackson2 and Jackson3 is the choice of tables used for lookup (see debugger screen-shot).

	@Test
	void convertStringToEnum() {
		assertThat(new ObjectMapper().convertValue("10%", MyEnum.class)).isEqualTo(MyEnum.T10);
	}

	public enum MyEnum {
		T10("10%"), T20("20%"), T30("30%");

		private final String code;

		MyEnum(String code) {
			this.code = code;
		}

		@JsonValue
		public String getCode() {
			return code;
		}
	}
tools.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `org.springframework.data.couchbase.core.mapping.MappingCouchbaseConverterTests$MyEnum` from String "10%": not one of the values accepted for Enum class: [T30, T20, T10]
 at [No location information]

	at tools.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:37)
	at tools.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1977)
	at tools.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:1320)
	at tools.jackson.databind.deser.jdk.EnumDeserializer._deserializeAltString(EnumDeserializer.java:355)
	at tools.jackson.databind.deser.jdk.EnumDeserializer._fromString(EnumDeserializer.java:215)
	at tools.jackson.databind.deser.jdk.EnumDeserializer.deserialize(EnumDeserializer.java:184)
	at tools.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2391)
	at tools.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:2323)

In the debugger

With Jackson3, the lookup table has the name() as the key. Thus it does not find the corresponding Enum.

Image

With Jackson2 the lookup table has the @jsonvalue values as the key. It does find the corresponding Enum using the serialized value.

Image

Expected behavior

Test passes.

Additional context

Works in Jackson 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.20Issues planned at 2.20 or later3.0Issue planned for initial 3.0 releaseenumRelated to handling of Enum valueshas-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