Skip to content

Commit 4a24f4d

Browse files
Fix: Avoid caching AnnotationIntrospector to support cusotm module loading
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.
1 parent ed6be13 commit 4a24f4d

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/AbstractModelConverter.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
public abstract class AbstractModelConverter implements ModelConverter {
2323
protected final ObjectMapper _mapper;
24-
protected final AnnotationIntrospector _intr;
2524
protected final TypeNameResolver _typeNameResolver;
2625
/**
2726
* Minor optimization: no need to keep on resolving same types over and over
@@ -43,7 +42,6 @@ public void setupModule(SetupContext context) {
4342
});
4443
_mapper = mapper;
4544
_typeNameResolver = typeNameResolver;
46-
_intr = mapper.getSerializationConfig().getAnnotationIntrospector();
4745
}
4846

4947
@Override
@@ -55,6 +53,17 @@ public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterato
5553
}
5654
}
5755

56+
/**
57+
* Retrieves the current AnnotationIntrospector from the ObjectMapper's serialization configuration.
58+
* We do not cache the value of _intr because users can load jackson modules later,
59+
* and we want to use their annotation inspection.
60+
*
61+
* @return the current AnnotationIntrospector
62+
*/
63+
protected AnnotationIntrospector _intr() {
64+
return _mapper.getSerializationConfig().getAnnotationIntrospector();
65+
}
66+
5867
protected String _typeName(JavaType type) {
5968
return _typeName(type, null);
6069
}
@@ -89,7 +98,7 @@ protected String _findTypeName(JavaType type, BeanDescription beanDesc) {
8998
beanDesc = _mapper.getSerializationConfig().introspectClassAnnotations(type);
9099
}
91100

92-
PropertyName rootName = _intr.findRootName(beanDesc.getClassInfo());
101+
PropertyName rootName = _intr().findRootName(beanDesc.getClassInfo());
93102
if (rootName != null && rootName.hasSimpleName()) {
94103
return rootName.getSimpleName();
95104
}

modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ private Schema clone(Schema property) {
10831083

10841084
private boolean isSubtype(AnnotatedClass childClass, Class<?> parentClass) {
10851085
final BeanDescription parentDesc = _mapper.getSerializationConfig().introspectClassAnnotations(parentClass);
1086-
List<NamedType> subTypes =_intr.findSubtypes(parentDesc.getClassInfo());
1086+
List<NamedType> subTypes = _intr().findSubtypes(parentDesc.getClassInfo());
10871087
if (subTypes == null) {
10881088
return false;
10891089
}
@@ -1130,7 +1130,7 @@ protected void _addEnumProps(Class<?> propClass, Schema property) {
11301130
Enum<?>[] enumConstants = enumClass.getEnumConstants();
11311131

11321132
if (enumConstants != null) {
1133-
String[] enumValues = _intr.findEnumValues(propClass, enumConstants,
1133+
String[] enumValues = _intr().findEnumValues(propClass, enumConstants,
11341134
new String[enumConstants.length]);
11351135

11361136
for (Enum<?> en : enumConstants) {
@@ -1156,7 +1156,7 @@ protected void _addEnumProps(Class<?> propClass, Schema property) {
11561156
} else if (useToString) {
11571157
n = en.toString();
11581158
} else {
1159-
n = _intr.findEnumValue(en);
1159+
n = _intr().findEnumValue(en);
11601160
}
11611161
if (property instanceof StringSchema) {
11621162
StringSchema sp = (StringSchema) property;
@@ -1526,7 +1526,7 @@ protected void applyBeanValidatorAnnotations(Schema property, Annotation[] annot
15261526
}
15271527

15281528
private boolean resolveSubtypes(Schema model, BeanDescription bean, ModelConverterContext context, JsonView jsonViewAnnotation) {
1529-
final List<NamedType> types = _intr.findSubtypes(bean.getClassInfo());
1529+
final List<NamedType> types = _intr().findSubtypes(bean.getClassInfo());
15301530
if (types == null) {
15311531
return false;
15321532
}
@@ -1662,7 +1662,7 @@ private void removeSuperClassAndInterfaceSubTypes(List<NamedType> types, BeanDes
16621662
private void removeSuperSubTypes(List<NamedType> resultTypes, Class<?> superClass) {
16631663
JavaType superType = _mapper.constructType(superClass);
16641664
BeanDescription superBean = _mapper.getSerializationConfig().introspect(superType);
1665-
final List<NamedType> superTypes = _intr.findSubtypes(superBean.getClassInfo());
1665+
final List<NamedType> superTypes = _intr().findSubtypes(superBean.getClassInfo());
16661666
if (superTypes != null) {
16671667
resultTypes.removeAll(superTypes);
16681668
}

0 commit comments

Comments
 (0)