1
1
package io .swagger .v3 .parser .test ;
2
2
3
+ import io .swagger .v3 .core .util .Yaml ;
3
4
import io .swagger .v3 .core .util .Yaml31 ;
4
5
import io .swagger .v3 .oas .models .OpenAPI ;
5
6
import io .swagger .v3 .oas .models .media .Schema ;
@@ -20,11 +21,7 @@ public void testBasicOAS31() {
20
21
SwaggerParseResult result = new OpenAPIV3Parser ().readLocation ( "3.1.0/test/basicOAS31.yaml" , null , null );
21
22
assertNotNull (result .getOpenAPI ());
22
23
OpenAPI openAPI = result .getOpenAPI ();
23
- System .out .println ("Messages: \n " );
24
- result .getMessages ().forEach (System .out ::println );
25
24
26
- System .out .println ("\n \n Parsed: \n \n " );
27
- Yaml31 .prettyPrint (openAPI );
28
25
//JsonSchemaDialect
29
26
assertNotNull (openAPI .getJsonSchemaDialect ());
30
27
//change to bad uri and retest to show the error message
@@ -60,7 +57,7 @@ public void testBasicOAS31() {
60
57
//const
61
58
assertNotNull (((Schema ) openAPI .getComponents ().getSchemas ().get ("Pet" ).getProperties ().get ("testconst" )).getConst ());
62
59
assertFalse (result .getMessages ().contains ("attribute components.schemas.Pet.const is unexpected" ));
63
- //exclusiveMaximum-exclusiveMinimum
60
+ //exclusiveMaximum-exclusiveMinimum are numeric in 3.1
64
61
assertTrue (openAPI .getComponents ().getSchemas ().get ("Pets" ).getExclusiveMaximumValue ().intValue ()==12 );
65
62
assertTrue (openAPI .getComponents ().getSchemas ().get ("Pets" ).getExclusiveMinimumValue ().intValue ()==1 );
66
63
//Null type
@@ -75,6 +72,56 @@ public void testBasicOAS31() {
75
72
76
73
}
77
74
75
+ @ Test (description = "Test basic OAS30 deserialization/validation if added the fields of OAS3.1" )
76
+ public void testBasicOAS30_With31Fields () {
77
+ SwaggerParseResult result = new OpenAPIV3Parser ().readLocation ( "3.1.0/test/basicOAS30.yaml" , null , null );
78
+ assertNotNull (result .getOpenAPI ());
79
+ OpenAPI openAPI = result .getOpenAPI ();
80
+
81
+ //No JsonSchemaDialect
82
+ assertNull (openAPI .getJsonSchemaDialect ());
83
+ assertTrue (result .getMessages ().contains ("attribute jsonSchemaDialect is unexpected" ));
84
+ //info: only description is parsed
85
+ assertNotNull (openAPI .getInfo ().getDescription ());
86
+ assertEquals (openAPI .getInfo ().getDescription (), "description in info object" );
87
+ assertTrue (result .getMessages ().contains ("attribute info.summary is unexpected" ));
88
+ //license: No identifier
89
+ assertNull (openAPI .getInfo ().getLicense ().getIdentifier ());
90
+ assertTrue (result .getMessages ().contains ("attribute info.license.identifier is unexpected" ));
91
+ //No pathItems under components
92
+ assertNull (openAPI .getComponents ().getPathItems ());
93
+ assertTrue (result .getMessages ().contains ("attribute components.pathItems is unexpected" ));
94
+ //Type array without items but with error message
95
+ assertEquals (openAPI .getComponents ().getSchemas ().get ("ArrayWithoutItems" ).getType (),"array" );
96
+ assertNull (openAPI .getComponents ().getSchemas ().get ("ArrayWithoutItems" ).getItems ());
97
+ assertTrue (result .getMessages ().contains ("attribute components.schemas.ArrayWithoutItems.items is missing" ));
98
+ //Type object with items: not allowed, it will internally create an array (New Option setDefaultSchemaTypeObject )
99
+ assertNotEquals (openAPI .getComponents ().getSchemas ().get ("ItemsWithoutArrayType" ).getType (),"object" );
100
+ assertEquals (openAPI .getComponents ().getSchemas ().get ("ItemsWithoutArrayType" ).getType (),"array" );
101
+ assertNotNull (openAPI .getComponents ().getSchemas ().get ("ItemsWithoutArrayType" ).getItems ());
102
+ assertTrue (result .getMessages ().contains ("attribute components.schemas.ItemsWithoutArrayType.item1 is unexpected" ));
103
+ //Type field as array not deserialized
104
+ assertNull (openAPI .getComponents ().getSchemas ().get ("Pet" ).getTypes ());
105
+ //JsonSchema
106
+ //arbitrary keywords are not allowed
107
+ assertNull (openAPI .getComponents ().getSchemas ().get ("Pet" ).getExtensions ());
108
+ assertTrue (result .getMessages ().contains ("attribute components.schemas.Pet.arbitraryKeyword is unexpected" ));
109
+ //const
110
+ assertNull (((Schema ) openAPI .getComponents ().getSchemas ().get ("Pet" ).getProperties ().get ("testconst" )).getConst ());
111
+ assertTrue (result .getMessages ().contains ("attribute components.schemas.Pet.const is unexpected" ));
112
+ //exclusiveMaximum-exclusiveMinimum are boolean in 3.0
113
+ assertNull (openAPI .getComponents ().getSchemas ().get ("Pets" ).getExclusiveMaximum ());
114
+ assertNull (openAPI .getComponents ().getSchemas ().get ("Pets" ).getExclusiveMinimum ());
115
+ //Null type
116
+ assertNull (openAPI .getComponents ().getSchemas ().get ("Pets" ).getTypes ());
117
+ //default value independence
118
+ assertNull (openAPI .getComponents ().getSchemas ().get ("Pets" ).getDefault ());
119
+ //not setting the type by default
120
+ assertNull (openAPI .getComponents ().getSchemas ().get ("MapAnyValue" ).getTypes ());
121
+ //Not webhooks
122
+ assertTrue (result .getMessages ().contains ("attribute webhooks is unexpected" ));
123
+ }
124
+
78
125
@ Test
79
126
public void testDeserializeSimpleDefinition () throws Exception {
80
127
String json =
@@ -593,7 +640,6 @@ public void testSiblingsReferenceJSONSchema6() {
593
640
SwaggerParseResult result = new OpenAPIV3Parser ().readContents ( refSibling , null , options );
594
641
OpenAPI openAPI = result .getOpenAPI ();
595
642
assertNotNull (openAPI );
596
- Yaml31 .prettyPrint (openAPI );
597
643
//if - then - else
598
644
assertNotNull (openAPI .getComponents ().getSchemas ().get ("IfTest" ));
599
645
Schema itTest = openAPI .getComponents ().getSchemas ().get ("IfTest" );
@@ -829,4 +875,40 @@ public void testNotDefaultSchemaType() {
829
875
assertNull (schema .getItems ().getNullable ());
830
876
assertNotNull (schema .getItems ().getExtensions ().get ("nullable" ));
831
877
}
878
+
879
+ @ Test (description = "Test how 3.0 spec deserializes with ref schema and other fields" )
880
+ public void test30NonRefSiblingsBehavior () {
881
+ ParseOptions options = new ParseOptions ();
882
+ String refSibling = "openapi: 3.0.0\n " +
883
+ "info:\n " +
884
+ " title: siblings JSONSchema\n " +
885
+ " version: 1.0.0\n " +
886
+ "servers:\n " +
887
+ " - url: /\n " +
888
+ "paths: { }\n " +
889
+ "components:\n " +
890
+ " schemas:\n " +
891
+ " profile:\n " +
892
+ " description: siblings refs\n " +
893
+ " required:\n " +
894
+ " - login\n " +
895
+ " - password\n " +
896
+ " maxItems: 2\n " +
897
+ " $ref: ./ex.json#user-profile\n " +
898
+ " properties:\n " +
899
+ " login:\n " +
900
+ " type: string\n " +
901
+ " password:\n " +
902
+ " type: string" ;
903
+ SwaggerParseResult result = new OpenAPIV3Parser ().readContents ( refSibling , null , options );
904
+ OpenAPI openAPI = result .getOpenAPI ();
905
+ assertNotNull (openAPI );
906
+ Schema profile = openAPI .getComponents ().getSchemas ().get ("profile" );
907
+ assertNotNull (profile .get$ref ());
908
+ assertNull (profile .getMaxItems ());
909
+ assertNull (profile .getDescription ());
910
+ assertNull (profile .getRequired ());
911
+ assertNull (profile .getProperties ());
912
+
913
+ }
832
914
}
0 commit comments