1
+ package io .swagger .v3 .core .converting ;
2
+
3
+ import io .swagger .v3 .core .converter .AnnotatedType ;
4
+ import io .swagger .v3 .core .converter .ModelConverter ;
5
+ import io .swagger .v3 .core .converter .ModelConverterContext ;
6
+ import io .swagger .v3 .core .converter .ModelConverterContextImpl ;
7
+ import io .swagger .v3 .oas .models .media .Schema ;
8
+ import org .testng .annotations .Test ;
9
+
10
+ import java .lang .reflect .Field ;
11
+ import java .util .Iterator ;
12
+ import java .util .Set ;
13
+
14
+ import static org .testng .Assert .assertEquals ;
15
+ import static org .testng .Assert .assertNotNull ;
16
+
17
+ public class AnnotatedTypeCachingTest {
18
+
19
+ @ Test
20
+ public void testAnnotatedTypeEqualityIgnoresContextualFields () {
21
+ AnnotatedType type1 = new AnnotatedType (String .class )
22
+ .propertyName ("userStatus" );
23
+ AnnotatedType type2 = new AnnotatedType (String .class )
24
+ .propertyName ("city" );
25
+ assertEquals (type1 , type2 , "AnnotatedType objects with different contextual fields (e.g., propertyName) should be equal." );
26
+ assertEquals (type1 .hashCode (), type2 .hashCode (), "The hash codes of equal AnnotatedType objects must be the same." );
27
+ }
28
+
29
+ static class User {
30
+ public String username ;
31
+ public String email ;
32
+ public Address address ;
33
+ }
34
+
35
+ static class Address {
36
+ public String street ;
37
+ public String city ;
38
+ }
39
+
40
+ private static class DummyModelConverter implements ModelConverter {
41
+ @ Override
42
+ public Schema resolve (AnnotatedType type , ModelConverterContext context , Iterator <ModelConverter > chain ) {
43
+ if (type .getType ().equals (User .class )) {
44
+ context .resolve (new AnnotatedType (String .class ).propertyName ("username" ));
45
+ context .resolve (new AnnotatedType (String .class ).propertyName ("email" ));
46
+ context .resolve (new AnnotatedType (Address .class ).propertyName ("address" ));
47
+ return new Schema ();
48
+ }
49
+ if (type .getType ().equals (Address .class )) {
50
+ context .resolve (new AnnotatedType (String .class ).propertyName ("street" ));
51
+ context .resolve (new AnnotatedType (String .class ).propertyName ("city" ));
52
+ return new Schema ();
53
+ }
54
+ return new Schema ();
55
+ }
56
+ }
57
+
58
+ @ Test
59
+ @ SuppressWarnings ("unchecked" )
60
+ public void testCacheHitsForRepeatedStringTypeWithCorrectedEquals () throws Exception {
61
+ ModelConverterContextImpl context = new ModelConverterContextImpl (new DummyModelConverter ());
62
+ Schema userSchema = context .resolve (new AnnotatedType (User .class ));
63
+ assertNotNull (userSchema );
64
+ Field processedTypesField = ModelConverterContextImpl .class .getDeclaredField ("processedTypes" );
65
+ processedTypesField .setAccessible (true );
66
+ Set <AnnotatedType > processedTypes = (Set <AnnotatedType >) processedTypesField .get (context );
67
+ long stringTypeCount = processedTypes .stream ()
68
+ .filter (annotatedType -> annotatedType .getType ().equals (String .class ))
69
+ .count ();
70
+ assertEquals (stringTypeCount , 1 , "With the correct equals/hashCode, String type should be added to the cache only once." );
71
+ }
72
+ }
0 commit comments