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 all 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
24 changes: 24 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ public enum DefaultTyping {
*/
NON_FINAL,

/**
* Enables default typing for non-final types as {@link #NON_FINAL},
* but also includes Enums.
* Designed to allow default typing of Enums without resorting to
* {@link #EVERYTHING}, which has security implications.
*<p>
* @since 2.16
*/
NON_FINAL_AND_ENUMS,

/**
* Value that means that default typing will be used for
* all types, with exception of small number of
Expand Down Expand Up @@ -355,6 +365,20 @@ public boolean useForType(JavaType t)
}
// [databind#88] Should not apply to JSON tree models:
return !t.isFinal() && !TreeNode.class.isAssignableFrom(t.getRawClass());

case NON_FINAL_AND_ENUMS: // since 2.16
while (t.isArrayType()) {
t = t.getContentType();
}
// 19-Apr-2016, tatu: ReferenceType like Optional also requires similar handling:
while (t.isReferenceType()) {
t = t.getReferencedType();
}
// [databind#88] Should not apply to JSON tree models:
return (!t.isFinal() && !TreeNode.class.isAssignableFrom(t.getRawClass()))
// [databind#3569] Allow use of default typing for Enums
|| 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
@@ -1,5 +1,11 @@
package com.fasterxml.jackson.databind.jsontype.deftyping;

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.jsontype.DefaultBaseTypeLimitingValidator;
import java.util.concurrent.TimeUnit;

import com.fasterxml.jackson.databind.BaseMapTest;
Expand All @@ -26,6 +32,25 @@ protected static class TimeUnitBean {
public TimeUnit timeUnit;
}

static class Foo3569<T> {
public T item;
}

enum Bar3569 {
ENABLED, DISABLED, HIDDEN;

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

/*
/**********************************************************
/* Test methods
Expand Down Expand Up @@ -78,4 +103,32 @@ public void testSimpleEnumsAsField() throws Exception
EnumHolder holder = m.readValue(json, EnumHolder.class);
assertSame(TestEnum.B, holder.value);
}

/**
* [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.16
*/
public void testEnumAsWrapperArrayWithCreator() throws JsonProcessingException
{
ObjectMapper objectMapper = jsonMapperBuilder()
.activateDefaultTyping(
new DefaultBaseTypeLimitingValidator(),
ObjectMapper.DefaultTyping.NON_FINAL_AND_ENUMS,
JsonTypeInfo.As.WRAPPER_ARRAY)
.build();

Foo3569<Bar3569> expected = new Foo3569<>();
expected.item = Bar3569.ENABLED;

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

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