ContextAutoTypeBeforeHandler basicTypes add:

javax.validation.ValidationException
javax.validation.NoProviderFoundException
This commit is contained in:
shaojin.wensj 2023-05-19 04:23:38 +08:00
parent d1182945ec
commit ecd9964c55
3 changed files with 165 additions and 117 deletions

View File

@ -173,6 +173,12 @@
<artifactId>springfox-spring-web</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.money</groupId>
<artifactId>money-api</artifactId>

View File

@ -23,7 +23,48 @@ import static com.alibaba.fastjson2.util.TypeUtils.*;
public class ContextAutoTypeBeforeHandler
implements JSONReader.AutoTypeBeforeHandler {
static final Class[] BASIC_TYPES = {
final long[] acceptHashCodes;
final ConcurrentMap<Integer, ConcurrentHashMap<Long, Class>> tclHashCaches = new ConcurrentHashMap<>();
final Map<Long, Class> classCache = new ConcurrentHashMap<>(16, 0.75f, 1);
public ContextAutoTypeBeforeHandler(Class... types) {
this(false, types);
}
public ContextAutoTypeBeforeHandler(boolean includeBasic, Class... types) {
this(
includeBasic,
names(
Arrays.asList(types)
)
);
}
public ContextAutoTypeBeforeHandler(String... acceptNames) {
this(false, acceptNames);
}
public ContextAutoTypeBeforeHandler(boolean includeBasic) {
this(includeBasic, new String[0]);
}
static String[] names(Collection<Class> types) {
Set<String> nameSet = new HashSet<>();
for (Class type : types) {
if (type == null) {
continue;
}
String name = TypeUtils.getTypeName(type);
nameSet.add(name);
}
return nameSet.toArray(new String[nameSet.size()]);
}
public ContextAutoTypeBeforeHandler(boolean includeBasic, String... acceptNames) {
Set<String> nameSet = new HashSet<>();
if (includeBasic) {
Class[] basicTypes = {
Object.class,
byte.class,
Byte.class,
@ -139,51 +180,18 @@ public class ContextAutoTypeBeforeHandler
StackTraceElement.class
};
final long[] acceptHashCodes;
final ConcurrentMap<Integer, ConcurrentHashMap<Long, Class>> tclHashCaches = new ConcurrentHashMap<>();
final Map<Long, Class> classCache = new ConcurrentHashMap<>(16, 0.75f, 1);
public ContextAutoTypeBeforeHandler(Class... types) {
this(false, types);
}
public ContextAutoTypeBeforeHandler(boolean includeBasic, Class... types) {
this(
includeBasic,
names(
Arrays.asList(types)
)
);
}
public ContextAutoTypeBeforeHandler(String... acceptNames) {
this(false, acceptNames);
}
public ContextAutoTypeBeforeHandler(boolean includeBasic) {
this(includeBasic, new String[0]);
}
static String[] names(Collection<Class> types) {
Set<String> nameSet = new HashSet<>();
for (Class type : types) {
if (type == null) {
continue;
}
String name = TypeUtils.getTypeName(type);
nameSet.add(name);
}
return nameSet.toArray(new String[nameSet.size()]);
}
public ContextAutoTypeBeforeHandler(boolean includeBasic, String... acceptNames) {
Set<String> nameSet = new HashSet<>();
if (includeBasic) {
for (Class basicType : BASIC_TYPES) {
for (Class basicType : basicTypes) {
String name = TypeUtils.getTypeName(basicType);
nameSet.add(name);
}
String[] basicTypeNames = {
"javax.validation.ValidationException",
"javax.validation.NoProviderFoundException"
};
for (String basicType : basicTypeNames) {
nameSet.add(basicType);
}
}
for (String name : acceptNames) {

View File

@ -0,0 +1,34 @@
package com.alibaba.fastjson2.issues_1000;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.filter.ContextAutoTypeBeforeHandler;
import org.junit.jupiter.api.Test;
import javax.validation.NoProviderFoundException;
import javax.validation.ValidationException;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class Issue1485 {
@Test
public void test() {
ContextAutoTypeBeforeHandler typeFilter = new ContextAutoTypeBeforeHandler(true);
assertEquals(
NoProviderFoundException.class,
typeFilter.apply("javax.validation.NoProviderFoundException", Object.class, JSONReader.Feature.SupportAutoType.mask)
);
assertEquals(
ValidationException.class,
typeFilter.apply("javax.validation.ValidationException", Object.class, JSONReader.Feature.SupportAutoType.mask)
);
assertEquals(
NoProviderFoundException.class,
typeFilter.apply("javax.validation.NoProviderFoundException", Exception.class, 0)
);
assertEquals(
ValidationException.class,
typeFilter.apply("javax.validation.ValidationException", Exception.class, 0)
);
}
}