Skip to content

Commit b136c63

Browse files
Add test showing that we get two cache entries if a field has a deprecated annotation
1 parent 177d85e commit b136c63

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

modules/swagger-core/src/test/java/io/swagger/v3/core/converting/AnnotatedTypeCachingTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.swagger.v3.oas.models.media.Schema;
88
import org.testng.annotations.Test;
99

10+
import java.lang.annotation.Annotation;
1011
import java.lang.reflect.Field;
1112
import java.util.Iterator;
1213
import java.util.Set;
@@ -69,4 +70,61 @@ public void testCacheHitsForRepeatedStringTypeWithCorrectedEquals() throws Excep
6970
.count();
7071
assertEquals(stringTypeCount, 1, "With the correct equals/hashCode, String type should be added to the cache only once.");
7172
}
73+
74+
@Test
75+
@SuppressWarnings("unchecked")
76+
public void testNoCacheHitForAFieldThatIsMarkedAsDeprecated() throws Exception {
77+
ModelConverterContextImpl context = new ModelConverterContextImpl(new FooBarDummyModelConverter());
78+
Schema fooSchema = context.resolve(new AnnotatedType(Foo.class));
79+
assertNotNull(fooSchema);
80+
Field processedTypesField = ModelConverterContextImpl.class.getDeclaredField("processedTypes");
81+
processedTypesField.setAccessible(true);
82+
Set<AnnotatedType> processedTypes = (Set<AnnotatedType>) processedTypesField.get(context);
83+
long stringTypeCount = processedTypes.stream()
84+
.filter(annotatedType -> annotatedType.getType().equals(String.class))
85+
.count();
86+
assertEquals(stringTypeCount, 2, "With the correct equals/hashCode, String type should be added to the cache twice, since one of them is deprecated.");
87+
}
88+
89+
private static class FooBarDummyModelConverter implements ModelConverter {
90+
@Override
91+
public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
92+
if (type.getType().equals(Foo.class)) {
93+
context.resolve(new AnnotatedType(String.class).propertyName("fizz").ctxAnnotations(new Annotation[]{getAnnotationInstance(Deprecated.class)}));
94+
context.resolve(new AnnotatedType(String.class).propertyName("buzz"));
95+
context.resolve(new AnnotatedType(Bar.class).propertyName("bar"));
96+
return new Schema();
97+
}
98+
if (type.getType().equals(Bar.class)) {
99+
context.resolve(new AnnotatedType(String.class).propertyName("fizz"));
100+
context.resolve(new AnnotatedType(String.class).propertyName("buzz"));
101+
return new Schema();
102+
}
103+
return new Schema();
104+
}
105+
}
106+
107+
private static Annotation getAnnotationInstance(Class<? extends Annotation> clazz) {
108+
try {
109+
return Foo.class.getDeclaredField("fizz").getAnnotation(clazz);
110+
} catch (NoSuchFieldException e) {
111+
throw new RuntimeException(e);
112+
}
113+
}
114+
115+
static class Foo {
116+
@Deprecated
117+
public String fizz;
118+
public String buzz;
119+
public Bar bar;
120+
121+
public Foo(String fizz) {
122+
this.fizz = fizz;
123+
}
124+
}
125+
126+
static class Bar {
127+
public String fizz;
128+
public String buzz;
129+
}
72130
}

0 commit comments

Comments
 (0)