99import io .swagger .v3 .oas .models .media .StringSchema ;
1010import org .testng .annotations .Test ;
1111
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 ;
1514
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 ;
1917
2018public class EnumTest extends SwaggerTestBase {
2119
@@ -33,7 +31,6 @@ public void testEnum() {
3331 new ArrayList <String >(Collections2 .transform (Arrays .asList (Currency .values ()), Functions .toStringFunction ()));
3432 assertEquals (strModel .getEnum (), modelValues );
3533
36-
3734 final Schema property = context .resolve (new AnnotatedType ().type (Currency .class ).schemaProperty (true ));
3835 assertNotNull (property );
3936 assertTrue (property instanceof StringSchema );
@@ -50,10 +47,8 @@ public void testEnumGenerics() {
5047 final ModelConverterContextImpl context = new ModelConverterContextImpl (modelResolver );
5148
5249 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" );
5752 }
5853
5954 @ Test
@@ -62,28 +57,254 @@ public void testEnumPropertyWithSchemaAnnotation() {
6257 final ModelConverterContextImpl context = new ModelConverterContextImpl (modelResolver );
6358
6459 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+
6863 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 testEnumPropertyWithGlobalSwitchOnlyOpenApi31 () {
70+ ModelResolver .enumsAsRef = true ;
71+ final ModelResolver modelResolver = new ModelResolver (mapper ()).openapi31 (true );
72+ final ModelConverterContextImpl context = new ModelConverterContextImpl (modelResolver );
73+
74+ final Schema model = context .resolve (new AnnotatedType ().type (ClassWithPlainEnum .class ));
75+ assertBasicModelStructure (model , "ClassWithPlainEnum" );
76+ assertPropertyExists (model , "plainEnum" );
77+
78+ final Schema enumPropertySchema = (Schema ) model .getProperties ().get ("plainEnum" );
6979 assertNotNull (enumPropertySchema .get$ref ());
80+ assertNull (enumPropertySchema .getEnum ());
81+ assertEquals (enumPropertySchema .getDescription (), "Plain enum property" );
82+
83+ assertEnumComponentExists (context , ClassWithPlainEnum .PlainEnum .values (), null );
84+
85+ // Reset the static field
86+ ModelResolver .enumsAsRef = false ;
87+ }
88+
89+ @ Test
90+ public void testArrayOfEnumWithSchemaAnnotationOpenApi31 () {
91+ final ModelResolver modelResolver = new ModelResolver (mapper ()).openapi31 (true );
92+ final ModelConverterContextImpl context = new ModelConverterContextImpl (modelResolver );
93+
94+ final Schema model = context .resolve (new AnnotatedType ().type (ClassWithEnumArray .class ));
95+ assertBasicModelStructure (model , "ClassWithEnumArray" );
96+ assertPropertyExists (model , "enumArray" );
97+
98+ final Schema arrayPropertySchema = (Schema ) model .getProperties ().get ("enumArray" );
99+ assertArrayWithEnumRef (arrayPropertySchema );
100+
101+ assertEnumComponentExists (context , ClassWithEnumArray .ArrayEnum .values (), "Enum description" );
102+ }
103+
104+ @ Test
105+ public void testArrayOfEnumWithSchemaAnnotationOpenApi30 () {
106+ final ModelResolver modelResolver = new ModelResolver (mapper ()).openapi31 (false );
107+ final ModelConverterContextImpl context = new ModelConverterContextImpl (modelResolver );
108+
109+ final Schema model = context .resolve (new AnnotatedType ().type (ClassWithEnumArray .class ));
110+ assertBasicModelStructure (model , "ClassWithEnumArray" );
111+ assertPropertyExists (model , "enumArray" );
112+
113+ final Schema arrayPropertySchema = (Schema ) model .getProperties ().get ("enumArray" );
114+ assertArrayWithEnumRef (arrayPropertySchema );
115+
116+ assertEnumComponentExists (context , ClassWithEnumArray .ArrayEnum .values (), "Enum description" );
70117 }
71118
119+ @ Test
120+ public void testControlTestNoRefOpenApi31 () {
121+ ModelResolver .enumsAsRef = false ;
122+ final ModelResolver modelResolver = new ModelResolver (mapper ()).openapi31 (true );
123+ final ModelConverterContextImpl context = new ModelConverterContextImpl (modelResolver );
124+
125+ final Schema model = context .resolve (new AnnotatedType ().type (ClassWithPlainEnum .class ));
126+ assertBasicModelStructure (model , "ClassWithPlainEnum" );
127+ assertPropertyExists (model , "plainEnum" );
128+
129+ final Schema enumPropertySchema = (Schema ) model .getProperties ().get ("plainEnum" );
130+ assertInlineEnumProperty (enumPropertySchema );
131+
132+ // Apply broad assertions - verify no components are created for inline enums
133+ Map <String , Schema > components = context .getDefinedModels ();
134+ if (components != null && !components .isEmpty ()) {
135+ Set <String > expected = Arrays .stream (ClassWithPlainEnum .PlainEnum .values ())
136+ .map (Enum ::name )
137+ .collect (Collectors .toSet ());
138+ Schema enumComponent = findEnumComponent (components , expected );
139+ assertNull (enumComponent , "No enum component should exist for inline enums" );
140+ }
141+ }
142+
143+ @ Test
144+ public void testControlTestNoRefOpenApi30 () {
145+ ModelResolver .enumsAsRef = false ;
146+ final ModelResolver modelResolver = new ModelResolver (mapper ()).openapi31 (false );
147+ final ModelConverterContextImpl context = new ModelConverterContextImpl (modelResolver );
148+
149+ final Schema model = context .resolve (new AnnotatedType ().type (ClassWithPlainEnum .class ));
150+ assertBasicModelStructure (model , "ClassWithPlainEnum" );
151+ assertPropertyExists (model , "plainEnum" );
152+
153+ final Schema enumPropertySchema = (Schema ) model .getProperties ().get ("plainEnum" );
154+ assertInlineEnumProperty (enumPropertySchema );
155+
156+ // Apply broad assertions - verify no components are created for inline enums
157+ Map <String , Schema > components = context .getDefinedModels ();
158+ if (components != null && !components .isEmpty ()) {
159+ Set <String > expected = Arrays .stream (ClassWithPlainEnum .PlainEnum .values ())
160+ .map (Enum ::name )
161+ .collect (Collectors .toSet ());
162+ Schema enumComponent = findEnumComponent (components , expected );
163+ assertNull (enumComponent , "No enum component should exist for inline enums" );
164+ }
165+ }
166+
167+ @ Test
168+ public void testEnumWithAllOfSchemaResolutionOpenApi30 () {
169+ final ModelResolver modelResolver = new ModelResolver (mapper ())
170+ .openapi31 (false )
171+ .schemaResolution (Schema .SchemaResolution .ALL_OF );
172+ final ModelConverterContextImpl context = new ModelConverterContextImpl (modelResolver );
173+
174+ final Schema model = context .resolve (new AnnotatedType ().type (ClassWithEnumAsRefProperty .class ));
175+ assertBasicModelStructure (model , "ClassWithEnumAsRefProperty" );
176+ assertPropertyExists (model , "enumWithSchemaProperty" );
177+
178+ final Schema enumPropertySchema = (Schema ) model .getProperties ().get ("enumWithSchemaProperty" );
179+
180+ boolean hasEnumRef = false ;
181+ for (Object allOfItem : enumPropertySchema .getAllOf ()) {
182+ if (allOfItem instanceof Schema ) {
183+ Schema allOfSchema = (Schema ) allOfItem ;
184+ if ("#/components/schemas/EnumWithSchemaProperty" .equals (allOfSchema .get$ref ())) {
185+ hasEnumRef = true ;
186+ break ;
187+ }
188+ }
189+ }
190+ assertTrue (hasEnumRef , "AllOf should contain reference to enum component" );
191+ assertEnumComponentExists (context , ClassWithEnumAsRefProperty .EnumWithSchemaProperty .values (), "Enum description" );
192+ }
193+
194+ private void assertBasicModelStructure (Schema model , String expectedName ) {
195+ assertNotNull (model );
196+ assertEquals (model .getName (), expectedName );
197+ }
198+
199+ private void assertPropertyExists (Schema model , String propertyName ) {
200+ assertTrue (model .getProperties ().containsKey (propertyName ));
201+ assertNotNull (model .getProperties ().get (propertyName ));
202+ }
203+
204+ private void assertEnumComponentExists (ModelConverterContextImpl context , Enum <?>[] enumValues , String expectedDescription ) {
205+ Map <String , Schema > components = context .getDefinedModels ();
206+ assertNotNull (components );
207+ assertFalse (components .isEmpty ());
208+
209+ Set <String > expected = Arrays .stream (enumValues )
210+ .map (Enum ::name )
211+ .collect (Collectors .toCollection (LinkedHashSet ::new ));
212+
213+ Schema enumComponent = findEnumComponent (components , expected );
214+ assertNotNull (enumComponent );
215+ assertEquals (enumComponent .getDescription (), expectedDescription );
216+ }
217+
218+ private void assertEnumComponentExistsWithDefault (ModelConverterContextImpl context , Enum <?>[] enumValues , String expectedDescription , String expectedDefault ) {
219+ assertEnumComponentExists (context , enumValues , expectedDescription );
220+
221+ Map <String , Schema > components = context .getDefinedModels ();
222+ Set <String > expected = Arrays .stream (enumValues )
223+ .map (Enum ::name )
224+ .collect (Collectors .toCollection (LinkedHashSet ::new ));
225+
226+ Schema enumComponent = findEnumComponent (components , expected );
227+ assertEquals (enumComponent .getDefault (), expectedDefault );
228+ }
229+
230+ private Schema findEnumComponent (Map <String , Schema > components , Set <String > expectedValues ) {
231+ return components .values ().stream ()
232+ .filter (Objects ::nonNull )
233+ .filter (s -> s .getEnum () != null )
234+ .filter (s -> {
235+ List <?> ev = s .getEnum ();
236+ Set <String > vals = ev .stream ().map (Object ::toString ).collect (Collectors .toSet ());
237+ return vals .containsAll (expectedValues ) && expectedValues .containsAll (vals );
238+ })
239+ .findFirst ()
240+ .orElse (null );
241+ }
242+
243+ private void assertEnumAsRefProperty (Schema propertySchema , String expectedRef ) {
244+ assertEquals (propertySchema .get$ref (), expectedRef );
245+ assertNull (propertySchema .getEnum ());
246+ }
247+
248+ private void assertInlineEnumProperty (Schema propertySchema ) {
249+ assertNotNull (propertySchema .getEnum ());
250+ assertNull (propertySchema .get$ref ());
251+ }
252+
253+ private void assertArrayWithEnumRef (Schema arrayPropertySchema ) {
254+ assertNotNull (arrayPropertySchema .getItems ());
255+ assertNotNull (arrayPropertySchema .getItems ().get$ref ());
256+ }
257+
258+
72259 public static class ClassWithEnumAsRefProperty {
73260
74- @ io .swagger .v3 .oas .annotations .media .Schema (enumAsRef = true )
261+ @ io .swagger .v3 .oas .annotations .media .Schema (enumAsRef = true , description = "Property description" , maximum = "1923234" )
75262 public final EnumWithSchemaProperty enumWithSchemaProperty ;
76263
77264 public ClassWithEnumAsRefProperty (EnumWithSchemaProperty enumWithSchemaProperty ) {
78265 this .enumWithSchemaProperty = enumWithSchemaProperty ;
79266 }
80267
268+ @ io .swagger .v3 .oas .annotations .media .Schema (description = "Enum description" )
81269 public enum EnumWithSchemaProperty {
82270 VALUE1 ,
83271 VALUE2
84272 }
85273 }
86274
275+ public static class ClassWithEnumArray {
276+
277+ @ io .swagger .v3 .oas .annotations .media .ArraySchema (schema = @ io .swagger .v3 .oas .annotations .media .Schema (enumAsRef = true , description = "Property Description" ))
278+ public final ArrayEnum [] enumArray ;
279+
280+ public ClassWithEnumArray (ArrayEnum [] enumArray ) {
281+ this .enumArray = enumArray ;
282+ }
283+
284+ @ io .swagger .v3 .oas .annotations .media .Schema (description = "Enum description" )
285+ public enum ArrayEnum {
286+ FIRST ,
287+ SECOND ,
288+ THIRD
289+ }
290+ }
291+
292+ public static class ClassWithPlainEnum {
293+
294+ @ io .swagger .v3 .oas .annotations .media .Schema (description = "Plain enum property" )
295+ public final PlainEnum plainEnum ;
296+
297+ public ClassWithPlainEnum (PlainEnum plainEnum ) {
298+ this .plainEnum = plainEnum ;
299+ }
300+
301+ public enum PlainEnum {
302+ ONE ,
303+ TWO ,
304+ THREE
305+ }
306+ }
307+
87308 public enum Currency {
88309 USA , CANADA
89310 }
0 commit comments