Skip to content

Commit

Permalink
Fix: Avoid caching AnnotationIntrospector to support cusotm module lo…
Browse files Browse the repository at this point in the history
…ading

We do not cache the value of '_intr' because users can load custom modules later after swagger is configured, and we want to utilize their annotation inspection, just like jackson would do.
  • Loading branch information
sahil-ramagiri authored and frantuma committed Jan 2, 2025
1 parent 16c4f71 commit 388f4ca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
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 @@ -1185,7 +1185,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 @@ -1232,7 +1232,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 @@ -1258,7 +1258,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 @@ -1635,7 +1635,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 @@ -1771,7 +1771,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

0 comments on commit 388f4ca

Please sign in to comment.