Skip to content

Commit 9279ae3

Browse files
sahil-ramagirifrantuma
authored andcommitted
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 16c4f71 commit 9279ae3

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

Lines changed: 12 additions & 3 deletions
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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ private Schema clone(Schema property) {
11851185

11861186
private boolean isSubtype(AnnotatedClass childClass, Class<?> parentClass) {
11871187
final BeanDescription parentDesc = _mapper.getSerializationConfig().introspectClassAnnotations(parentClass);
1188-
List<NamedType> subTypes =_intr.findSubtypes(parentDesc.getClassInfo());
1188+
List<NamedType> subTypes = _intr().findSubtypes(parentDesc.getClassInfo());
11891189
if (subTypes == null) {
11901190
return false;
11911191
}
@@ -1232,7 +1232,7 @@ protected void _addEnumProps(Class<?> propClass, Schema property) {
12321232
Enum<?>[] enumConstants = enumClass.getEnumConstants();
12331233

12341234
if (enumConstants != null) {
1235-
String[] enumValues = _intr.findEnumValues(propClass, enumConstants,
1235+
String[] enumValues = _intr().findEnumValues(propClass, enumConstants,
12361236
new String[enumConstants.length]);
12371237

12381238
for (Enum<?> en : enumConstants) {
@@ -1258,7 +1258,7 @@ protected void _addEnumProps(Class<?> propClass, Schema property) {
12581258
} else if (useToString) {
12591259
n = en.toString();
12601260
} else {
1261-
n = _intr.findEnumValue(en);
1261+
n = _intr().findEnumValue(en);
12621262
}
12631263
if (property instanceof StringSchema) {
12641264
StringSchema sp = (StringSchema) property;
@@ -1635,7 +1635,7 @@ protected void applyBeanValidatorAnnotations(Schema property, Annotation[] annot
16351635
}
16361636

16371637
private boolean resolveSubtypes(Schema model, BeanDescription bean, ModelConverterContext context, JsonView jsonViewAnnotation) {
1638-
final List<NamedType> types = _intr.findSubtypes(bean.getClassInfo());
1638+
final List<NamedType> types = _intr().findSubtypes(bean.getClassInfo());
16391639
if (types == null) {
16401640
return false;
16411641
}
@@ -1771,7 +1771,7 @@ private void removeSuperClassAndInterfaceSubTypes(List<NamedType> types, BeanDes
17711771
private void removeSuperSubTypes(List<NamedType> resultTypes, Class<?> superClass) {
17721772
JavaType superType = _mapper.constructType(superClass);
17731773
BeanDescription superBean = _mapper.getSerializationConfig().introspect(superType);
1774-
final List<NamedType> superTypes = _intr.findSubtypes(superBean.getClassInfo());
1774+
final List<NamedType> superTypes = _intr().findSubtypes(superBean.getClassInfo());
17751775
if (superTypes != null) {
17761776
resultTypes.removeAll(superTypes);
17771777
}

0 commit comments

Comments
 (0)