1
1
package io .swagger .v3 .parser .test ;
2
2
3
- import io .swagger .v3 .core .util .Yaml ;
4
3
import io .swagger .v3 .core .util .Yaml31 ;
5
4
import io .swagger .v3 .oas .models .OpenAPI ;
6
5
import io .swagger .v3 .oas .models .media .Schema ;
10
9
import io .swagger .v3 .parser .core .models .SwaggerParseResult ;
11
10
import org .testng .annotations .Test ;
12
11
13
- import java .util .ArrayList ;
14
12
import java .util .List ;
15
13
16
14
import static org .testng .Assert .*;
17
15
18
16
public class OAI31DeserializationTest {
17
+
18
+ @ Test (description = "Test basic OAS31 deserialization/validation" )
19
+ public void testBasicOAS31 () {
20
+ SwaggerParseResult result = new OpenAPIV3Parser ().readLocation ( "3.1.0/test/basicOAS31.yaml" , null , null );
21
+ assertNotNull (result .getOpenAPI ());
22
+ OpenAPI openAPI = result .getOpenAPI ();
23
+ System .out .println ("Messages: " +result .getMessages ());
24
+ Yaml31 .prettyPrint (openAPI );
25
+ //JsonSchemaDialect
26
+ assertNotNull (openAPI .getJsonSchemaDialect ());
27
+ //change to bad uri and retest to show the error message
28
+ assertEquals (openAPI .getJsonSchemaDialect (), "https://json-schema.org/draft/2020-12/schema" );
29
+ //info: description and summary
30
+ assertNotNull (openAPI .getInfo ().getSummary ());
31
+ assertEquals (openAPI .getInfo ().getSummary (), "test summary in info object" );
32
+ assertNotNull (openAPI .getInfo ().getDescription ());
33
+ assertEquals (openAPI .getInfo ().getDescription (), "description in info object" );
34
+ //license: identifier
35
+ assertNotNull (openAPI .getInfo ().getLicense ().getIdentifier ());
36
+ assertEquals (openAPI .getInfo ().getLicense ().getIdentifier (), "test identifier" );
37
+ //pathItems under components
38
+ assertNotNull (openAPI .getComponents ().getPathItems ());
39
+ assertNotNull (openAPI .getComponents ().getPathItems ().get ("pets" ));
40
+ //Type array without items
41
+ assertTrue (openAPI .getComponents ().getSchemas ().get ("ArrayWithoutItems" ).getTypes ().contains ("array" ));
42
+ assertNull (openAPI .getComponents ().getSchemas ().get ("ArrayWithoutItems" ).getItems ());
43
+ assertFalse (result .getMessages ().contains ("attribute components.schemas.ArrayWithoutItems.items is missing" ));
44
+ //Type object with items
45
+ assertTrue (openAPI .getComponents ().getSchemas ().get ("ItemsWithoutArrayType" ).getTypes ().contains ("object" ));
46
+ assertNotNull (openAPI .getComponents ().getSchemas ().get ("ItemsWithoutArrayType" ).getItems ());
47
+ assertFalse (result .getMessages ().contains ("attribute components.schemas.ItemsWithoutArrayType.item1 is unexpected" ));
48
+ //Type as array
49
+ assertTrue (openAPI .getComponents ().getSchemas ().get ("Pet" ).getTypes ().size () == 3 );
50
+ assertTrue (openAPI .getComponents ().getSchemas ().get ("Pet" ).getTypes ().contains ("array" ));
51
+ assertTrue (openAPI .getComponents ().getSchemas ().get ("Pet" ).getTypes ().contains ("string" ));
52
+ assertTrue (openAPI .getComponents ().getSchemas ().get ("Pet" ).getTypes ().contains ("object" ));
53
+ //JsonSchema
54
+ //arbitrary keywords are now allowed
55
+ assertNotNull (openAPI .getComponents ().getSchemas ().get ("Pet" ).getExtensions ().get ("arbitraryKeyword" ));
56
+ assertFalse (result .getMessages ().contains ("attribute components.schemas.Pet.arbitraryKeyword is unexpected" ));
57
+ //const
58
+ assertNotNull (((Schema ) openAPI .getComponents ().getSchemas ().get ("Pet" ).getProperties ().get ("testconst" )).getConst ());
59
+ assertFalse (result .getMessages ().contains ("attribute components.schemas.Pet.const is unexpected" ));
60
+ //exclusiveMaximum-exclusiveMinimum
61
+ assertTrue (openAPI .getComponents ().getSchemas ().get ("Pets" ).getExclusiveMaximumValue ().intValue ()==12 );
62
+ assertTrue (openAPI .getComponents ().getSchemas ().get ("Pets" ).getExclusiveMinimumValue ().intValue ()==1 );
63
+ //Null type
64
+ assertTrue (openAPI .getComponents ().getSchemas ().get ("Pets" ).getTypes ().contains ("null" ));
65
+ //default value independence
66
+ assertEquals (openAPI .getComponents ().getSchemas ().get ("Pets" ).getDefault (), "I'm a string" );
67
+ //not setting the type by default
68
+ assertNull (openAPI .getComponents ().getSchemas ().get ("MapAnyValue" ).getTypes ());
69
+ //$ref siblings
70
+ assertNotNull (openAPI .getComponents ().getSchemas ().get ("Pet" ).get$ref ());
71
+ assertNotNull (openAPI .getComponents ().getSchemas ().get ("Pet" ).getProperties ());
72
+
73
+ }
74
+
19
75
@ Test
20
76
public void testDeserializeSimpleDefinition () throws Exception {
21
77
String json =
@@ -41,6 +97,7 @@ public void testDeserializeSimpleDefinition() throws Exception {
41
97
assertNotNull (result .getOpenAPI ());
42
98
}
43
99
100
+
44
101
@ Test
45
102
public void testBasic () {
46
103
SwaggerParseResult result = new OpenAPIV3Parser ().readLocation ( "3.1.0/basic.yaml" , null , null );
@@ -306,7 +363,7 @@ public void testSiblingsReferenceJSONSchema2() {
306
363
assertTrue (profile .getPatternProperties ().containsKey ("^S_" ));
307
364
}
308
365
309
- @ Test (description = "Test siblings with $ref for patternProperties, pattern, additionalProperties" )
366
+ @ Test (description = "Test siblings with $ref for patternProperties, pattern, additionalProperties,exclusiveMaximum,exclusiveMinimum " )
310
367
public void testSiblingsReferenceJSONSchema3 () {
311
368
ParseOptions options = new ParseOptions ();
312
369
String refSibling = "openapi: 3.1.0\n " +
@@ -721,9 +778,46 @@ public void testNotDefaultSchemaType() {
721
778
SwaggerParseResult result = new OpenAPIV3Parser ().readContents (defaultSchemaType , null , options );
722
779
OpenAPI openAPI = result .getOpenAPI ();
723
780
assertNotNull (openAPI );
781
+
782
+ //map_any_value as object when it should be null
724
783
assertNotNull (openAPI .getComponents ().getSchemas ().get ("AnyValueModelInline" ).getProperties ().get ("map_any_value" ));
725
784
assertTrue (openAPI .getComponents ().getSchemas ().get ("AnyValueModelInline" ).getProperties ().get ("map_any_value" ) instanceof Schema );
726
785
Schema mapProperty = (Schema )openAPI .getComponents ().getSchemas ().get ("AnyValueModelInline" ).getProperties ().get ("map_any_value" );
727
786
assertNull (mapProperty .getType ());
787
+
788
+ //map_any_value_with_desc as object when it should be null
789
+ assertNotNull (openAPI .getComponents ().getSchemas ().get ("AnyValueModelInline" ).getProperties ().get ("map_any_value_with_desc" ));
790
+ assertTrue (openAPI .getComponents ().getSchemas ().get ("AnyValueModelInline" ).getProperties ().get ("map_any_value_with_desc" ) instanceof Schema );
791
+ mapProperty = (Schema )openAPI .getComponents ().getSchemas ().get ("AnyValueModelInline" ).getProperties ().get ("map_any_value_with_desc" );
792
+ assertNull (mapProperty .getType ());
793
+
794
+ //map_any_value_nullable as object when it should be null
795
+ assertNotNull (openAPI .getComponents ().getSchemas ().get ("AnyValueModelInline" ).getProperties ().get ("map_any_value_nullable" ));
796
+ assertTrue (openAPI .getComponents ().getSchemas ().get ("AnyValueModelInline" ).getProperties ().get ("map_any_value_nullable" ) instanceof Schema );
797
+ mapProperty = (Schema )openAPI .getComponents ().getSchemas ().get ("AnyValueModelInline" ).getProperties ().get ("map_any_value_nullable" );
798
+ assertNull (mapProperty .getType ());
799
+
800
+ //array_any_value as array when it should be null
801
+ assertNotNull (openAPI .getComponents ().getSchemas ().get ("AnyValueModelInline" ).getProperties ().get ("array_any_value" ));
802
+ assertTrue (openAPI .getComponents ().getSchemas ().get ("AnyValueModelInline" ).getProperties ().get ("array_any_value" ) instanceof Schema );
803
+ mapProperty = (Schema )openAPI .getComponents ().getSchemas ().get ("AnyValueModelInline" ).getProperties ().get ("array_any_value" );
804
+ assertNull (mapProperty .getType ());
805
+
806
+ //array_any_value_with_desc as array when it should be null
807
+ assertNotNull (openAPI .getComponents ().getSchemas ().get ("AnyValueModelInline" ).getProperties ().get ("array_any_value_with_desc" ));
808
+ assertTrue (openAPI .getComponents ().getSchemas ().get ("AnyValueModelInline" ).getProperties ().get ("array_any_value_with_desc" ) instanceof Schema );
809
+ mapProperty = (Schema )openAPI .getComponents ().getSchemas ().get ("AnyValueModelInline" ).getProperties ().get ("array_any_value_with_desc" );
810
+ assertNull (mapProperty .getType ());
811
+
812
+ //array_any_value_nullable
813
+ assertNotNull (openAPI .getComponents ().getSchemas ().get ("AnyValueModelInline" ).getProperties ().get ("array_any_value_nullable" ));
814
+ assertTrue (openAPI .getComponents ().getSchemas ().get ("AnyValueModelInline" ).getProperties ().get ("array_any_value_nullable" ) instanceof Schema );
815
+ mapProperty = (Schema )openAPI .getComponents ().getSchemas ().get ("AnyValueModelInline" ).getProperties ().get ("array_any_value_nullable" );
816
+ assertNull (mapProperty .getType ());
817
+ assertNull (mapProperty .getItems ().getNullable ());
818
+ Yaml31 .prettyPrint (mapProperty );
819
+ assertNotNull (mapProperty .getItems ().getExtensions ().get ("nullable" ));
820
+ //TODO check the toString Method difference between SOUT and YAML31 prettyprint
821
+ System .out .println (mapProperty );
728
822
}
729
823
}
0 commit comments