9
9
import io .swagger .v3 .oas .models .media .StringSchema ;
10
10
import org .testng .annotations .Test ;
11
11
12
- import java .util .ArrayList ;
13
- import java .util .Arrays ;
14
- import java .util .Collection ;
12
+ import java .util .*;
13
+ import java .util .stream .Collectors ;
15
14
16
- import static org .testng .Assert .assertEquals ;
17
- import static org .testng .Assert .assertNotNull ;
18
- import static org .testng .Assert .assertTrue ;
15
+ import static org .testng .Assert .*;
16
+ import static org .testng .AssertJUnit .assertFalse ;
19
17
20
18
public class EnumTest extends SwaggerTestBase {
21
19
@@ -33,7 +31,6 @@ public void testEnum() {
33
31
new ArrayList <String >(Collections2 .transform (Arrays .asList (Currency .values ()), Functions .toStringFunction ()));
34
32
assertEquals (strModel .getEnum (), modelValues );
35
33
36
-
37
34
final Schema property = context .resolve (new AnnotatedType ().type (Currency .class ).schemaProperty (true ));
38
35
assertNotNull (property );
39
36
assertTrue (property instanceof StringSchema );
@@ -50,40 +47,213 @@ public void testEnumGenerics() {
50
47
final ModelConverterContextImpl context = new ModelConverterContextImpl (modelResolver );
51
48
52
49
final Schema model = context .resolve ((new AnnotatedType ().type (Contract .class )));
53
- assertNotNull (model );
54
- assertEquals (model .getName (), "Contract" );
55
- assertTrue (model .getProperties ().containsKey ("type" ));
56
- assertNotNull (model .getProperties ().get ("type" ));
50
+ assertBasicModelStructure (model , "Contract" );
51
+ assertPropertyExists (model , "type" );
57
52
}
58
53
59
54
@ Test
60
- public void testEnumPropertyWithSchemaAnnotation () {
55
+ public void testEnumPropertyWithSchemaAnnotation31 () {
61
56
final ModelResolver modelResolver = new ModelResolver (mapper ()).openapi31 (true );
62
57
final ModelConverterContextImpl context = new ModelConverterContextImpl (modelResolver );
63
58
64
59
final Schema model = context .resolve (new AnnotatedType ().type (ClassWithEnumAsRefProperty .class ));
65
- assertNotNull (model );
66
- assertEquals (model .getName (), "ClassWithEnumAsRefProperty" );
67
- assertTrue (model .getProperties ().containsKey ("enumWithSchemaProperty" ));
60
+ assertBasicModelStructure (model , "ClassWithEnumAsRefProperty" );
61
+ assertPropertyExists (model , "enumWithSchemaProperty" );
62
+
63
+ final Schema enumPropertySchema = (Schema ) model .getProperties ().get ("enumWithSchemaProperty" );
64
+ assertEnumAsRefProperty (enumPropertySchema , "#/components/schemas/EnumWithSchemaProperty" );
65
+ assertEquals (enumPropertySchema .getDescription (), "Property description" );
66
+ }
67
+
68
+ @ Test
69
+ public void testEnumPropertyWithSchemaAnnotation30 () {
70
+ final ModelResolver modelResolver = new ModelResolver (mapper ()).openapi31 (false );
71
+ final ModelConverterContextImpl context = new ModelConverterContextImpl (modelResolver );
72
+
73
+ final Schema model = context .resolve (new AnnotatedType ().type (ClassWithEnumAsRefProperty .class ));
74
+ assertBasicModelStructure (model , "ClassWithEnumAsRefProperty" );
75
+ assertPropertyExists (model , "enumWithSchemaProperty" );
76
+
68
77
final Schema enumPropertySchema = (Schema ) model .getProperties ().get ("enumWithSchemaProperty" );
78
+ assertEnumAsRefProperty (enumPropertySchema , "#/components/schemas/EnumWithSchemaProperty" );
79
+
80
+ }
81
+
82
+ @ Test
83
+ public void testEnumPropertyWithGlobalSwitchOnlyOpenApi31 () {
84
+ ModelResolver .enumsAsRef = true ;
85
+ final ModelResolver modelResolver = new ModelResolver (mapper ()).openapi31 (true );
86
+ final ModelConverterContextImpl context = new ModelConverterContextImpl (modelResolver );
87
+
88
+ final Schema model = context .resolve (new AnnotatedType ().type (ClassWithPlainEnum .class ));
89
+ assertBasicModelStructure (model , "ClassWithPlainEnum" );
90
+ assertPropertyExists (model , "plainEnum" );
91
+
92
+ final Schema enumPropertySchema = (Schema ) model .getProperties ().get ("plainEnum" );
69
93
assertNotNull (enumPropertySchema .get$ref ());
94
+ assertNull (enumPropertySchema .getEnum ());
95
+ assertEquals (enumPropertySchema .getDescription (), "Plain enum property" );
96
+
97
+ assertEnumComponentExists (context , ClassWithPlainEnum .PlainEnum .values (), null );
98
+
99
+ // Reset the static field
100
+ ModelResolver .enumsAsRef = false ;
101
+ }
102
+
103
+ @ Test
104
+ public void testControlTestNoRefOpenApi31 () {
105
+ ModelResolver .enumsAsRef = false ;
106
+ final ModelResolver modelResolver = new ModelResolver (mapper ()).openapi31 (true );
107
+ final ModelConverterContextImpl context = new ModelConverterContextImpl (modelResolver );
108
+
109
+ final Schema model = context .resolve (new AnnotatedType ().type (ClassWithPlainEnum .class ));
110
+ assertBasicModelStructure (model , "ClassWithPlainEnum" );
111
+ assertPropertyExists (model , "plainEnum" );
112
+
113
+ final Schema enumPropertySchema = (Schema ) model .getProperties ().get ("plainEnum" );
114
+ assertInlineEnumProperty (enumPropertySchema );
115
+
116
+ // Apply broad assertions - verify no components are created for inline enums
117
+ Map <String , Schema > components = context .getDefinedModels ();
118
+ if (components != null && !components .isEmpty ()) {
119
+ Set <String > expected = Arrays .stream (ClassWithPlainEnum .PlainEnum .values ())
120
+ .map (Enum ::name )
121
+ .collect (Collectors .toSet ());
122
+ Schema enumComponent = findEnumComponent (components , expected );
123
+ assertNull (enumComponent , "No enum component should exist for inline enums" );
124
+ }
125
+ }
126
+
127
+ @ Test
128
+ public void testControlTestNoRefOpenApi30 () {
129
+ ModelResolver .enumsAsRef = false ;
130
+ final ModelResolver modelResolver = new ModelResolver (mapper ()).openapi31 (false );
131
+ final ModelConverterContextImpl context = new ModelConverterContextImpl (modelResolver );
132
+
133
+ final Schema model = context .resolve (new AnnotatedType ().type (ClassWithPlainEnum .class ));
134
+ assertBasicModelStructure (model , "ClassWithPlainEnum" );
135
+ assertPropertyExists (model , "plainEnum" );
136
+
137
+ final Schema enumPropertySchema = (Schema ) model .getProperties ().get ("plainEnum" );
138
+ assertInlineEnumProperty (enumPropertySchema );
139
+
140
+ // Apply broad assertions - verify no components are created for inline enums
141
+ Map <String , Schema > components = context .getDefinedModels ();
142
+ if (components != null && !components .isEmpty ()) {
143
+ Set <String > expected = Arrays .stream (ClassWithPlainEnum .PlainEnum .values ())
144
+ .map (Enum ::name )
145
+ .collect (Collectors .toSet ());
146
+ Schema enumComponent = findEnumComponent (components , expected );
147
+ assertNull (enumComponent , "No enum component should exist for inline enums" );
148
+ }
149
+ }
150
+
151
+ @ Test
152
+ public void testEnumWithAllOfSchemaResolutionOpenApi30 () {
153
+ final ModelResolver modelResolver = new ModelResolver (mapper ())
154
+ .openapi31 (false )
155
+ .schemaResolution (Schema .SchemaResolution .ALL_OF );
156
+ final ModelConverterContextImpl context = new ModelConverterContextImpl (modelResolver );
157
+
158
+ final Schema model = context .resolve (new AnnotatedType ().type (ClassWithEnumAsRefProperty .class ));
159
+ assertBasicModelStructure (model , "ClassWithEnumAsRefProperty" );
160
+ assertPropertyExists (model , "enumWithSchemaProperty" );
161
+
162
+ final Schema enumPropertySchema = (Schema ) model .getProperties ().get ("enumWithSchemaProperty" );
163
+
164
+ boolean hasEnumRef = false ;
165
+ for (Object allOfItem : enumPropertySchema .getAllOf ()) {
166
+ if (allOfItem instanceof Schema ) {
167
+ Schema allOfSchema = (Schema ) allOfItem ;
168
+ if ("#/components/schemas/EnumWithSchemaProperty" .equals (allOfSchema .get$ref ())) {
169
+ hasEnumRef = true ;
170
+ break ;
171
+ }
172
+ }
173
+ }
174
+ assertTrue (hasEnumRef , "AllOf should contain reference to enum component" );
175
+ assertEnumComponentExists (context , ClassWithEnumAsRefProperty .EnumWithSchemaProperty .values (), "Enum description" );
176
+ }
177
+
178
+ private void assertBasicModelStructure (Schema model , String expectedName ) {
179
+ assertNotNull (model );
180
+ assertEquals (model .getName (), expectedName );
181
+ }
182
+
183
+ private void assertPropertyExists (Schema model , String propertyName ) {
184
+ assertTrue (model .getProperties ().containsKey (propertyName ));
185
+ assertNotNull (model .getProperties ().get (propertyName ));
186
+ }
187
+
188
+ private void assertEnumComponentExists (ModelConverterContextImpl context , Enum <?>[] enumValues , String expectedDescription ) {
189
+ Map <String , Schema > components = context .getDefinedModels ();
190
+ assertNotNull (components );
191
+ assertFalse (components .isEmpty ());
192
+
193
+ Set <String > expected = Arrays .stream (enumValues )
194
+ .map (Enum ::name )
195
+ .collect (Collectors .toCollection (LinkedHashSet ::new ));
196
+
197
+ Schema enumComponent = findEnumComponent (components , expected );
198
+ assertNotNull (enumComponent );
199
+ assertEquals (enumComponent .getDescription (), expectedDescription );
200
+ }
201
+
202
+ private Schema findEnumComponent (Map <String , Schema > components , Set <String > expectedValues ) {
203
+ return components .values ().stream ()
204
+ .filter (Objects ::nonNull )
205
+ .filter (s -> s .getEnum () != null )
206
+ .filter (s -> {
207
+ List <?> ev = s .getEnum ();
208
+ Set <String > vals = ev .stream ().map (Object ::toString ).collect (Collectors .toSet ());
209
+ return vals .containsAll (expectedValues ) && expectedValues .containsAll (vals );
210
+ })
211
+ .findFirst ()
212
+ .orElse (null );
213
+ }
214
+
215
+ private void assertEnumAsRefProperty (Schema propertySchema , String expectedRef ) {
216
+ assertEquals (propertySchema .get$ref (), expectedRef );
217
+ assertNull (propertySchema .getEnum ());
218
+ }
219
+
220
+ private void assertInlineEnumProperty (Schema propertySchema ) {
221
+ assertNotNull (propertySchema .getEnum ());
222
+ assertNull (propertySchema .get$ref ());
70
223
}
71
224
72
225
public static class ClassWithEnumAsRefProperty {
73
226
74
- @ io .swagger .v3 .oas .annotations .media .Schema (enumAsRef = true )
227
+ @ io .swagger .v3 .oas .annotations .media .Schema (enumAsRef = true , description = "Property description" , maximum = "1923234" )
75
228
public final EnumWithSchemaProperty enumWithSchemaProperty ;
76
229
77
230
public ClassWithEnumAsRefProperty (EnumWithSchemaProperty enumWithSchemaProperty ) {
78
231
this .enumWithSchemaProperty = enumWithSchemaProperty ;
79
232
}
80
233
234
+ @ io .swagger .v3 .oas .annotations .media .Schema (description = "Enum description" )
81
235
public enum EnumWithSchemaProperty {
82
236
VALUE1 ,
83
237
VALUE2
84
238
}
85
239
}
86
240
241
+ public static class ClassWithPlainEnum {
242
+
243
+ @ io .swagger .v3 .oas .annotations .media .Schema (description = "Plain enum property" )
244
+ public final PlainEnum plainEnum ;
245
+
246
+ public ClassWithPlainEnum (PlainEnum plainEnum ) {
247
+ this .plainEnum = plainEnum ;
248
+ }
249
+
250
+ public enum PlainEnum {
251
+ ONE ,
252
+ TWO ,
253
+ THREE
254
+ }
255
+ }
256
+
87
257
public enum Currency {
88
258
USA , CANADA
89
259
}
0 commit comments