Skip to content

Add new DefaultTyping.NON_FINAL_AND_ENUMS to allow Default Typing for Enums #4159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@ public boolean useForType(JavaType t)
t = t.getReferencedType();
}
// [databind#88] Should not apply to JSON tree models:
return !t.isFinal() && !TreeNode.class.isAssignableFrom(t.getRawClass());
return (!t.isFinal() && !TreeNode.class.isAssignableFrom(t.getRawClass()))
// [databind#3569] Allow use of default typing for Enums -- since 2.15.4
|| t.isEnumType();
case EVERYTHING:
// So, excluding primitives (handled earlier) and "Natural types" (handled
// before this method is called), applied to everything
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ protected static ObjectMapper newJsonMapper() {
}

// @since 2.10
protected static JsonMapper.Builder jsonMapperBuilder() {
public static JsonMapper.Builder jsonMapperBuilder() {
return JsonMapper.builder();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.fasterxml.jackson.databind.jsontype.deftyping;

import static com.fasterxml.jackson.databind.BaseMapTest.jsonMapperBuilder;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.DefaultBaseTypeLimitingValidator;
import org.junit.Test;

/**
* [databind#3569]: Unable to deserialize enum object with default-typed
* {@link com.fasterxml.jackson.annotation.JsonTypeInfo.As#WRAPPER_ARRAY} and {@link JsonCreator} together.
*
* @since 2.15.4
*/
public class AsWrapperArrayEnumDeser3569Test
{
static class Foo<T> {
public T item;
}

enum Bar {
ENABLED, DISABLED, HIDDEN;

@JsonCreator
public static Bar fromValue(String value) {
String upperVal = value.toUpperCase();
for (Bar enumValue : Bar.values()) {
if (enumValue.name().equals(upperVal)) {
return enumValue;
}
}
throw new IllegalArgumentException("Bad input [" + value + "]");
}
}

@Test
public void testEnumAsWrapperArrayWithCreator() throws JsonProcessingException {
ObjectMapper objectMapper = jsonMapperBuilder()
.activateDefaultTyping(
new DefaultBaseTypeLimitingValidator(),
ObjectMapper.DefaultTyping.NON_FINAL,
JsonTypeInfo.As.WRAPPER_ARRAY)
.build();

Foo<Bar> expected = new Foo<>();
expected.item = Bar.ENABLED;

// First, serialize
String serialized = objectMapper.writeValueAsString(expected);

// Then, deserialize with TypeReference
assertNotNull(objectMapper.readValue(serialized, new TypeReference<Foo<Bar>>() {}));
// And, also try as described in [databind#3569]
JavaType javaType = objectMapper.getTypeFactory().constructParametricType(Foo.class, new Class[]{Bar.class});
assertNotNull(objectMapper.readValue(serialized, javaType));
}
}