Skip to content

Commit

Permalink
A fix for #1498 implementation to prevent regression failures wrt Exc…
Browse files Browse the repository at this point in the history
…eption deserialization
  • Loading branch information
cowtowncoder committed Sep 18, 2020
1 parent c0cba51 commit a1674bd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public ConstructorDetector withRequireAnnotation(boolean state) {
state, _allowJDKTypeCtors);
}

public ConstructorDetector withAllowJDKTypes(boolean state) {
public ConstructorDetector withAllowJDKTypeConstructors(boolean state) {
return new ConstructorDetector(_singleArgMode,
_requireCtorAnnotation, state);
}
Expand Down Expand Up @@ -186,12 +186,31 @@ public boolean singleArgCreatorDefaultsToProperties() {
return _singleArgMode == SingleArgConstructor.PROPERTIES;
}

public boolean allowImplicitCreators(Class<?> rawType) {
/**
* Accessor that combines calls to {@link #allowImplicitCreators} and
* {@link #allowJDKTypeConstructors} to determine whether implicit constructor
* detection should be enabled or not.
*
* @param rawType Value type to consider
*
* @return True if implicit constructor detection should be enabled; false if not
*/
public boolean shouldIntrospectorImplicitConstructors(Class<?> rawType) {
// May not allow implicit creator introspection at all:
if (_requireCtorAnnotation) {
return false;
}
// But if it is allowed, may further limit use for JDK types
return _allowJDKTypeCtors || !ClassUtil.isJDKClass(rawType);
if (!_allowJDKTypeCtors) {
if (ClassUtil.isJDKClass(rawType)) {
// 18-Sep-2020, tatu: Looks like must make an exception for Exception
// types (ha!) -- at this point, single-String-arg constructor
// is to be auto-detected
if (!Throwable.class.isAssignableFrom(rawType)) {
return false;
}
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,14 @@ protected ValueInstantiator _constructDefaultValueInstantiator(DeserializationCo
throws JsonMappingException
{
final CreatorCollectionState ccState;
final boolean findImplicit;
final ConstructorDetector ctorDetector;

{
final DeserializationConfig config = ctxt.getConfig();
// need to construct suitable visibility checker:
final VisibilityChecker<?> vchecker = config.getDefaultVisibilityChecker(beanDesc.getBeanClass(),
beanDesc.getClassInfo());
// 18-Sep-2020, tatu: Although by default implicit introspection is allowed, 2.12
// has settings to prevent that either generally, or at least for JDK types
findImplicit = config.getConstructorDetector().allowImplicitCreators(beanDesc.getBeanClass());
ctorDetector = config.getConstructorDetector();

// 24-Sep-2014, tatu: Tricky part first; need to merge resolved property information
// (which has creator parameters sprinkled around) with actual creator
Expand All @@ -278,7 +276,7 @@ protected ValueInstantiator _constructDefaultValueInstantiator(DeserializationCo
}

// Start with explicitly annotated factory methods
_addExplicitFactoryCreators(ctxt, ccState, findImplicit);
_addExplicitFactoryCreators(ctxt, ccState, !ctorDetector.requireCtorAnnotation());

// constructors only usable on concrete types:
if (beanDesc.getType().isConcrete()) {
Expand All @@ -300,6 +298,9 @@ protected ValueInstantiator _constructDefaultValueInstantiator(DeserializationCo
// TODO: look for `@JsonCreator` annotated ones, throw explicit exception?
;
} else {
// 18-Sep-2020, tatu: Although by default implicit introspection is allowed, 2.12
// has settings to prevent that either generally, or at least for JDK types
final boolean findImplicit = ctorDetector.shouldIntrospectorImplicitConstructors(beanDesc.getBeanClass());
_addExplicitConstructorCreators(ctxt, ccState, findImplicit);
if (ccState.hasImplicitConstructorCandidates()
&& !ccState.hasExplicitFactories() && !ccState.hasExplicitConstructors()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ static class MyNoArgException extends Exception
public void testIOException() throws IOException
{
IOException ioe = new IOException("TEST");
String json = MAPPER.writeValueAsString(ioe);
String json = MAPPER.writerWithDefaultPrettyPrinter()
.writeValueAsString(ioe);
IOException result = MAPPER.readValue(json, IOException.class);
assertEquals(ioe.getMessage(), result.getMessage());
}
Expand Down

0 comments on commit a1674bd

Please sign in to comment.