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,122 +23,6 @@ import static com.alibaba.fastjson2.util.TypeUtils.*;
public class ContextAutoTypeBeforeHandler
implements JSONReader.AutoTypeBeforeHandler {
static final Class[] BASIC_TYPES = {
Object.class,
byte.class,
Byte.class,
short.class,
Short.class,
int.class,
Integer.class,
long.class,
Long.class,
float.class,
Float.class,
double.class,
Double.class,
Number.class,
BigInteger.class,
BigDecimal.class,
AtomicInteger.class,
AtomicLong.class,
AtomicBoolean.class,
AtomicIntegerArray.class,
AtomicLongArray.class,
AtomicReference.class,
boolean.class,
Boolean.class,
char.class,
Character.class,
String.class,
UUID.class,
Currency.class,
BitSet.class,
EnumSet.class,
// this is java.util.RegularEnumSet, java.util.JumboEnumSet need add manually ?
EnumSet.noneOf(TimeUnit.class).getClass(),
Date.class,
Calendar.class,
LocalTime.class,
LocalDate.class,
LocalDateTime.class,
Instant.class,
SimpleDateFormat.class,
DateTimeFormatter.class,
TimeUnit.class,
Set.class,
HashSet.class,
LinkedHashSet.class,
TreeSet.class,
List.class,
ArrayList.class,
LinkedList.class,
ConcurrentLinkedQueue.class,
ConcurrentSkipListSet.class,
CopyOnWriteArrayList.class,
Collections.emptyList().getClass(),
Collections.emptyMap().getClass(),
CLASS_SINGLE_SET,
CLASS_SINGLE_LIST,
CLASS_UNMODIFIABLE_COLLECTION,
CLASS_UNMODIFIABLE_LIST,
CLASS_UNMODIFIABLE_SET,
CLASS_UNMODIFIABLE_SORTED_SET,
CLASS_UNMODIFIABLE_NAVIGABLE_SET,
Collections.unmodifiableMap(new HashMap<>()).getClass(),
Collections.unmodifiableNavigableMap(new TreeMap<>()).getClass(),
Collections.unmodifiableSortedMap(new TreeMap<>()).getClass(),
Arrays.asList().getClass(),
Map.class,
HashMap.class,
Hashtable.class,
TreeMap.class,
LinkedHashMap.class,
WeakHashMap.class,
IdentityHashMap.class,
ConcurrentMap.class,
ConcurrentHashMap.class,
ConcurrentSkipListMap.class,
Exception.class,
IllegalAccessError.class,
IllegalAccessException.class,
IllegalArgumentException.class,
IllegalMonitorStateException.class,
IllegalStateException.class,
IllegalThreadStateException.class,
IndexOutOfBoundsException.class,
InstantiationError.class,
InstantiationException.class,
InternalError.class,
InterruptedException.class,
LinkageError.class,
NegativeArraySizeException.class,
NoClassDefFoundError.class,
NoSuchFieldError.class,
NoSuchFieldException.class,
NoSuchMethodError.class,
NoSuchMethodException.class,
NullPointerException.class,
NumberFormatException.class,
OutOfMemoryError.class,
RuntimeException.class,
SecurityException.class,
StackOverflowError.class,
StringIndexOutOfBoundsException.class,
TypeNotPresentException.class,
VerifyError.class,
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);
@ -180,10 +64,134 @@ public class ContextAutoTypeBeforeHandler
public ContextAutoTypeBeforeHandler(boolean includeBasic, String... acceptNames) {
Set<String> nameSet = new HashSet<>();
if (includeBasic) {
for (Class basicType : BASIC_TYPES) {
Class[] basicTypes = {
Object.class,
byte.class,
Byte.class,
short.class,
Short.class,
int.class,
Integer.class,
long.class,
Long.class,
float.class,
Float.class,
double.class,
Double.class,
Number.class,
BigInteger.class,
BigDecimal.class,
AtomicInteger.class,
AtomicLong.class,
AtomicBoolean.class,
AtomicIntegerArray.class,
AtomicLongArray.class,
AtomicReference.class,
boolean.class,
Boolean.class,
char.class,
Character.class,
String.class,
UUID.class,
Currency.class,
BitSet.class,
EnumSet.class,
// this is java.util.RegularEnumSet, java.util.JumboEnumSet need add manually ?
EnumSet.noneOf(TimeUnit.class).getClass(),
Date.class,
Calendar.class,
LocalTime.class,
LocalDate.class,
LocalDateTime.class,
Instant.class,
SimpleDateFormat.class,
DateTimeFormatter.class,
TimeUnit.class,
Set.class,
HashSet.class,
LinkedHashSet.class,
TreeSet.class,
List.class,
ArrayList.class,
LinkedList.class,
ConcurrentLinkedQueue.class,
ConcurrentSkipListSet.class,
CopyOnWriteArrayList.class,
Collections.emptyList().getClass(),
Collections.emptyMap().getClass(),
CLASS_SINGLE_SET,
CLASS_SINGLE_LIST,
CLASS_UNMODIFIABLE_COLLECTION,
CLASS_UNMODIFIABLE_LIST,
CLASS_UNMODIFIABLE_SET,
CLASS_UNMODIFIABLE_SORTED_SET,
CLASS_UNMODIFIABLE_NAVIGABLE_SET,
Collections.unmodifiableMap(new HashMap<>()).getClass(),
Collections.unmodifiableNavigableMap(new TreeMap<>()).getClass(),
Collections.unmodifiableSortedMap(new TreeMap<>()).getClass(),
Arrays.asList().getClass(),
Map.class,
HashMap.class,
Hashtable.class,
TreeMap.class,
LinkedHashMap.class,
WeakHashMap.class,
IdentityHashMap.class,
ConcurrentMap.class,
ConcurrentHashMap.class,
ConcurrentSkipListMap.class,
Exception.class,
IllegalAccessError.class,
IllegalAccessException.class,
IllegalArgumentException.class,
IllegalMonitorStateException.class,
IllegalStateException.class,
IllegalThreadStateException.class,
IndexOutOfBoundsException.class,
InstantiationError.class,
InstantiationException.class,
InternalError.class,
InterruptedException.class,
LinkageError.class,
NegativeArraySizeException.class,
NoClassDefFoundError.class,
NoSuchFieldError.class,
NoSuchFieldException.class,
NoSuchMethodError.class,
NoSuchMethodException.class,
NullPointerException.class,
NumberFormatException.class,
OutOfMemoryError.class,
RuntimeException.class,
SecurityException.class,
StackOverflowError.class,
StringIndexOutOfBoundsException.class,
TypeNotPresentException.class,
VerifyError.class,
StackTraceElement.class
};
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)
);
}
}