Skip to content
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

Fix: Avoid caching AnnotationIntrospector to support custom module loading #4728

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

public abstract class AbstractModelConverter implements ModelConverter {
protected final ObjectMapper _mapper;
protected final AnnotationIntrospector _intr;
protected final TypeNameResolver _typeNameResolver;
/**
* Minor optimization: no need to keep on resolving same types over and over
Expand All @@ -43,7 +42,6 @@ public void setupModule(SetupContext context) {
});
_mapper = mapper;
_typeNameResolver = typeNameResolver;
_intr = mapper.getSerializationConfig().getAnnotationIntrospector();
}

@Override
Expand All @@ -55,6 +53,17 @@ public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterato
}
}

/**
* Retrieves the current AnnotationIntrospector from the ObjectMapper's serialization configuration.
* We do not cache the value of _intr because users can load jackson modules later,
* and we want to use their annotation inspection.
*
* @return the current AnnotationIntrospector
*/
protected AnnotationIntrospector _intr() {
return _mapper.getSerializationConfig().getAnnotationIntrospector();
}

protected String _typeName(JavaType type) {
return _typeName(type, null);
}
Expand Down Expand Up @@ -89,7 +98,7 @@ protected String _findTypeName(JavaType type, BeanDescription beanDesc) {
beanDesc = _mapper.getSerializationConfig().introspectClassAnnotations(type);
}

PropertyName rootName = _intr.findRootName(beanDesc.getClassInfo());
PropertyName rootName = _intr().findRootName(beanDesc.getClassInfo());
if (rootName != null && rootName.hasSimpleName()) {
return rootName.getSimpleName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@ private Schema clone(Schema property) {

private boolean isSubtype(AnnotatedClass childClass, Class<?> parentClass) {
final BeanDescription parentDesc = _mapper.getSerializationConfig().introspectClassAnnotations(parentClass);
List<NamedType> subTypes =_intr.findSubtypes(parentDesc.getClassInfo());
List<NamedType> subTypes = _intr().findSubtypes(parentDesc.getClassInfo());
if (subTypes == null) {
return false;
}
Expand Down Expand Up @@ -1189,7 +1189,7 @@ protected void _addEnumProps(Class<?> propClass, Schema property) {
Enum<?>[] enumConstants = enumClass.getEnumConstants();

if (enumConstants != null) {
String[] enumValues = _intr.findEnumValues(propClass, enumConstants,
String[] enumValues = _intr().findEnumValues(propClass, enumConstants,
new String[enumConstants.length]);

for (Enum<?> en : enumConstants) {
Expand All @@ -1215,7 +1215,7 @@ protected void _addEnumProps(Class<?> propClass, Schema property) {
} else if (useToString) {
n = en.toString();
} else {
n = _intr.findEnumValue(en);
n = _intr().findEnumValue(en);
}
if (property instanceof StringSchema) {
StringSchema sp = (StringSchema) property;
Expand Down Expand Up @@ -1592,7 +1592,7 @@ protected void applyBeanValidatorAnnotations(Schema property, Annotation[] annot
}

private boolean resolveSubtypes(Schema model, BeanDescription bean, ModelConverterContext context, JsonView jsonViewAnnotation) {
final List<NamedType> types = _intr.findSubtypes(bean.getClassInfo());
final List<NamedType> types = _intr().findSubtypes(bean.getClassInfo());
if (types == null) {
return false;
}
Expand Down Expand Up @@ -1728,7 +1728,7 @@ private void removeSuperClassAndInterfaceSubTypes(List<NamedType> types, BeanDes
private void removeSuperSubTypes(List<NamedType> resultTypes, Class<?> superClass) {
JavaType superType = _mapper.constructType(superClass);
BeanDescription superBean = _mapper.getSerializationConfig().introspect(superType);
final List<NamedType> superTypes = _intr.findSubtypes(superBean.getClassInfo());
final List<NamedType> superTypes = _intr().findSubtypes(superBean.getClassInfo());
if (superTypes != null) {
resultTypes.removeAll(superTypes);
}
Expand Down
Loading