Skip to content

Commit 9233c2d

Browse files
committed
add tests
1 parent 9cb2e39 commit 9233c2d

15 files changed

Lines changed: 495 additions & 23 deletions

modules/swagger-parser/src/test/java/io/swagger/parser/OpenAPIParserTest.java

Lines changed: 203 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void testNPE_1685() {
4040
SwaggerParseResult swaggerParseResult = openAPIParser.readLocation("issue1685.json", null, options);
4141
assertNull(swaggerParseResult.getOpenAPI());
4242
assertNotNull(swaggerParseResult.getMessages());
43-
assertTrue(swaggerParseResult.getMessages().size() == 2);
43+
assertTrue(swaggerParseResult.getMessages().size() == 2);
4444
assertEquals(swaggerParseResult.getMessages().get(0), "attribute notswagger is unexpected");
4545
assertEquals(swaggerParseResult.getMessages().get(1), "attribute swagger is missing");
4646
}
@@ -775,36 +775,24 @@ public void testIssue1552AdditionalProps() throws Exception {
775775

776776

777777
@Test
778-
public void testAdditionalPropertiesWithAllOfV1() {
778+
public void testIssue2157_AllOf_SingleRefPreservesBoolean() {
779779
ParseOptions options = new ParseOptions();
780780
options.setResolveFully(true);
781781
SwaggerParseResult result = new OpenAPIParser().readLocation(
782-
"additionalProperties_allOf_v1.yaml", null, options);
782+
"issue-2157/allOf-single-ref-boolean.yaml", null, options);
783783
assertNotNull(result.getOpenAPI());
784784
Schema schema = result.getOpenAPI().getComponents().getSchemas().get("TestSchema");
785785
assertNotNull(schema);
786786
assertNotNull(schema.getAdditionalProperties());
787787
assertTrue((Boolean) schema.getAdditionalProperties());
788788
}
789-
@Test
790-
public void testAdditionalPropertiesWithAllOfV2() {
791-
ParseOptions options = new ParseOptions();
792-
options.setResolveFully(true);
793-
SwaggerParseResult result = new OpenAPIParser().readLocation(
794-
"additionalProperties_allOf_v2.yaml", null, options);
795-
assertNotNull(result.getOpenAPI());
796-
Schema schema = result.getOpenAPI().getComponents().getSchemas().get("TestSchema");
797-
assertNotNull(schema);
798-
assertNotNull(schema.getAdditionalProperties());
799-
assertTrue((Boolean) schema.getAdditionalProperties());
800-
}
801789

802790
@Test
803-
public void testAdditionalPropertiesWithAllOfV3() {
791+
public void testIssue2157_AllOf_RefAndInlineMerge() {
804792
ParseOptions options = new ParseOptions();
805793
options.setResolveFully(true);
806794
SwaggerParseResult result = new OpenAPIParser().readLocation(
807-
"additionalProperties_allOf_v3.yaml", null, options);
795+
"issue-2157/allOf-ref-and-inline-merge.yaml", null, options);
808796
assertNotNull(result.getOpenAPI());
809797
Schema schema = result.getOpenAPI().getComponents().getSchemas().get("TestSchema");
810798
assertNotNull(schema);
@@ -813,16 +801,212 @@ public void testAdditionalPropertiesWithAllOfV3() {
813801
}
814802

815803
@Test
816-
public void testAdditionalPropertiesWithAllOfV4() {
804+
public void testIssue2157_AllOf_PreservesSchemaObject() {
805+
ParseOptions options = new ParseOptions();
806+
options.setResolveFully(true);
807+
SwaggerParseResult result = new OpenAPIParser().readLocation(
808+
"issue-2157/allOf-preserves-schema-object.yaml", null, options);
809+
assertNotNull(result.getOpenAPI());
810+
RequestBody requestBody = result.getOpenAPI().getPaths().get("/pet").getPut().getRequestBody();
811+
assertNotNull(requestBody);
812+
Schema schema = requestBody.getContent().get("application/json").getSchema();
813+
assertNotNull(schema);
814+
assertNotNull(schema.getAdditionalProperties());
815+
assertTrue(schema.getAdditionalProperties() instanceof Schema);
816+
Schema additionalPropsSchema = (Schema) schema.getAdditionalProperties();
817+
assertEquals(additionalPropsSchema.getType(), "string");
818+
assertNotNull(schema.getProperties());
819+
assertTrue(schema.getProperties().containsKey("id"));
820+
assertTrue(schema.getProperties().containsKey("name"));
821+
}
822+
823+
@Test
824+
public void testIssue2157_AllOf_PreservesSchemaWithConstraints() {
817825
ParseOptions options = new ParseOptions();
818826
options.setResolveFully(true);
819827
SwaggerParseResult result = new OpenAPIParser().readLocation(
820-
"additionalProperties_allOf_v4.yaml", null, options);
828+
"issue-2157/allOf-preserves-schema-with-constraints.yaml", null, options);
829+
assertNotNull(result.getOpenAPI());
830+
Schema schema = result.getOpenAPI().getComponents().getSchemas().get("TestSchema");
831+
assertNotNull(schema);
832+
assertNotNull(schema.getAdditionalProperties());
833+
assertTrue(schema.getAdditionalProperties() instanceof Schema);
834+
Schema additionalPropsSchema = (Schema) schema.getAdditionalProperties();
835+
assertEquals(additionalPropsSchema.getType(), "string");
836+
assertEquals(additionalPropsSchema.getMinLength(), Integer.valueOf(1));
837+
assertEquals(additionalPropsSchema.getMaxLength(), Integer.valueOf(100));
838+
assertNotNull(schema.getProperties());
839+
assertTrue(schema.getProperties().containsKey("id"));
840+
assertTrue(schema.getProperties().containsKey("name"));
841+
}
842+
843+
@Test
844+
public void testIssue2157_AllOf_ConflictLastWinsInline() {
845+
ParseOptions options = new ParseOptions();
846+
options.setResolveFully(true);
847+
SwaggerParseResult result = new OpenAPIParser().readLocation(
848+
"issue-2157/allOf-conflict-last-wins-inline.yaml", null, options);
821849
assertNotNull(result.getOpenAPI());
822850
Schema schema = result.getOpenAPI().getComponents().getSchemas().get("TestSchema");
823851
assertNotNull(schema);
824852
assertNotNull(schema.getAdditionalProperties());
825853
assertFalse((Boolean) schema.getAdditionalProperties());
826854
}
855+
856+
@Test
857+
public void testIssue2157_AllOf_ConflictLastWinsRef() {
858+
ParseOptions options = new ParseOptions();
859+
options.setResolveFully(true);
860+
SwaggerParseResult result = new OpenAPIParser().readLocation(
861+
"issue-2157/allOf-conflict-last-wins-ref.yaml", null, options);
862+
assertNotNull(result.getOpenAPI());
863+
Schema schema = result.getOpenAPI().getComponents().getSchemas().get("TestSchema");
864+
assertNotNull(schema);
865+
assertNotNull(schema.getAdditionalProperties());
866+
assertTrue((Boolean) schema.getAdditionalProperties());
867+
}
868+
869+
@Test
870+
public void testIssue2157_AllOf_NoAggregationPreservesStructure() {
871+
ParseOptions options = new ParseOptions();
872+
options.setResolveFully(true);
873+
options.setResolveCombinators(false);
874+
SwaggerParseResult result = new OpenAPIParser().readLocation(
875+
"issue-2157/allOf-no-aggregation-preserves-structure.yaml", null, options);
876+
assertNotNull(result.getOpenAPI());
877+
Schema schema = result.getOpenAPI().getComponents().getSchemas().get("TestSchema");
878+
assertNotNull(schema);
879+
assertNotNull(schema.getAllOf());
880+
assertEquals(schema.getAllOf().size(), 2);
881+
Schema firstBranch = (Schema) schema.getAllOf().get(0);
882+
assertNotNull(firstBranch.getAdditionalProperties());
883+
assertTrue((Boolean) firstBranch.getAdditionalProperties());
884+
}
885+
886+
@Test
887+
public void testIssue2157_AllOf_ConflictSchemaObjects() {
888+
ParseOptions options = new ParseOptions();
889+
options.setResolveFully(true);
890+
SwaggerParseResult result = new OpenAPIParser().readLocation(
891+
"issue-2157/allOf-conflict-schema-objects.yaml", null, options);
892+
assertNotNull(result.getOpenAPI());
893+
Schema schema = result.getOpenAPI().getComponents().getSchemas().get("TestSchema");
894+
assertNotNull(schema);
895+
assertNotNull(schema.getAdditionalProperties());
896+
assertTrue(schema.getAdditionalProperties() instanceof Schema);
897+
Schema additionalPropsSchema = (Schema) schema.getAdditionalProperties();
898+
assertEquals(additionalPropsSchema.getType(), "number");
899+
assertNotNull(schema.getProperties());
900+
assertTrue(schema.getProperties().containsKey("name"));
901+
assertTrue(schema.getProperties().containsKey("id"));
902+
}
903+
904+
@Test
905+
public void testIssue2157_OneOf_PreservesBoolean() {
906+
ParseOptions options = new ParseOptions();
907+
options.setResolveFully(true);
908+
SwaggerParseResult result = new OpenAPIParser().readLocation(
909+
"issue-2157/oneOf-preserves-boolean.yaml", null, options);
910+
assertNotNull(result.getOpenAPI());
911+
Schema schema = result.getOpenAPI().getComponents().getSchemas().get("TestSchema");
912+
assertNotNull(schema);
913+
assertNotNull(schema.getOneOf());
914+
assertEquals(schema.getOneOf().size(), 1);
915+
Schema oneOfBranch = (Schema) schema.getOneOf().get(0);
916+
assertNotNull(oneOfBranch.getAdditionalProperties());
917+
assertTrue((Boolean) oneOfBranch.getAdditionalProperties());
918+
}
919+
920+
@Test
921+
public void testIssue2157_OneOf_PreservesSchemaObject() {
922+
ParseOptions options = new ParseOptions();
923+
options.setResolveFully(true);
924+
SwaggerParseResult result = new OpenAPIParser().readLocation(
925+
"issue-2157/oneOf-preserves-schema-object.yaml", null, options);
926+
assertNotNull(result.getOpenAPI());
927+
Schema schema = result.getOpenAPI().getComponents().getSchemas().get("TestSchema");
928+
assertNotNull(schema);
929+
assertNotNull(schema.getOneOf());
930+
assertEquals(schema.getOneOf().size(), 1);
931+
Schema oneOfBranch = (Schema) schema.getOneOf().get(0);
932+
assertNotNull(oneOfBranch.getAdditionalProperties());
933+
assertTrue(oneOfBranch.getAdditionalProperties() instanceof Schema);
934+
Schema additionalPropsSchema = (Schema) oneOfBranch.getAdditionalProperties();
935+
assertEquals(additionalPropsSchema.getType(), "string");
936+
assertEquals(additionalPropsSchema.getMinLength(), Integer.valueOf(1));
937+
}
938+
939+
@Test
940+
public void testIssue2157_OneOf_MultipleBranchesIndependent() {
941+
ParseOptions options = new ParseOptions();
942+
options.setResolveFully(true);
943+
SwaggerParseResult result = new OpenAPIParser().readLocation(
944+
"issue-2157/oneOf-multiple-branches-independent.yaml", null, options);
945+
assertNotNull(result.getOpenAPI());
946+
Schema schema = result.getOpenAPI().getComponents().getSchemas().get("TestSchema");
947+
assertNotNull(schema);
948+
assertNotNull(schema.getOneOf());
949+
assertEquals(schema.getOneOf().size(), 2);
950+
Schema firstBranch = (Schema) schema.getOneOf().get(0);
951+
Schema secondBranch = (Schema) schema.getOneOf().get(1);
952+
assertNotNull(firstBranch.getAdditionalProperties());
953+
assertTrue((Boolean) firstBranch.getAdditionalProperties());
954+
assertNotNull(secondBranch.getAdditionalProperties());
955+
assertFalse((Boolean) secondBranch.getAdditionalProperties());
956+
}
957+
958+
@Test
959+
public void testIssue2157_AnyOf_PreservesBoolean() {
960+
ParseOptions options = new ParseOptions();
961+
options.setResolveFully(true);
962+
SwaggerParseResult result = new OpenAPIParser().readLocation(
963+
"issue-2157/anyOf-preserves-boolean.yaml", null, options);
964+
assertNotNull(result.getOpenAPI());
965+
Schema schema = result.getOpenAPI().getComponents().getSchemas().get("TestSchema");
966+
assertNotNull(schema);
967+
assertNotNull(schema.getAnyOf());
968+
assertEquals(schema.getAnyOf().size(), 1);
969+
Schema anyOfBranch = (Schema) schema.getAnyOf().get(0);
970+
assertNotNull(anyOfBranch.getAdditionalProperties());
971+
assertTrue((Boolean) anyOfBranch.getAdditionalProperties());
972+
}
973+
974+
@Test
975+
public void testIssue2157_AnyOf_PreservesSchemaObject() {
976+
ParseOptions options = new ParseOptions();
977+
options.setResolveFully(true);
978+
SwaggerParseResult result = new OpenAPIParser().readLocation(
979+
"issue-2157/anyOf-preserves-schema-object.yaml", null, options);
980+
assertNotNull(result.getOpenAPI());
981+
Schema schema = result.getOpenAPI().getComponents().getSchemas().get("TestSchema");
982+
assertNotNull(schema);
983+
assertNotNull(schema.getAnyOf());
984+
assertEquals(schema.getAnyOf().size(), 1);
985+
Schema anyOfBranch = (Schema) schema.getAnyOf().get(0);
986+
assertNotNull(anyOfBranch.getAdditionalProperties());
987+
assertTrue(anyOfBranch.getAdditionalProperties() instanceof Schema);
988+
Schema additionalPropsSchema = (Schema) anyOfBranch.getAdditionalProperties();
989+
assertEquals(additionalPropsSchema.getType(), "string");
990+
assertEquals(additionalPropsSchema.getMinLength(), Integer.valueOf(1));
991+
}
992+
993+
@Test
994+
public void testIssue2157_AnyOf_MultipleBranchesIndependent() {
995+
ParseOptions options = new ParseOptions();
996+
options.setResolveFully(true);
997+
SwaggerParseResult result = new OpenAPIParser().readLocation(
998+
"issue-2157/anyOf-multiple-branches-independent.yaml", null, options);
999+
assertNotNull(result.getOpenAPI());
1000+
Schema schema = result.getOpenAPI().getComponents().getSchemas().get("TestSchema");
1001+
assertNotNull(schema);
1002+
assertNotNull(schema.getAnyOf());
1003+
assertEquals(schema.getAnyOf().size(), 2);
1004+
Schema firstBranch = (Schema) schema.getAnyOf().get(0);
1005+
Schema secondBranch = (Schema) schema.getAnyOf().get(1);
1006+
assertNotNull(firstBranch.getAdditionalProperties());
1007+
assertTrue((Boolean) firstBranch.getAdditionalProperties());
1008+
assertNotNull(secondBranch.getAdditionalProperties());
1009+
assertFalse((Boolean) secondBranch.getAdditionalProperties());
1010+
}
8271011
}
8281012

modules/swagger-parser/src/test/resources/additionalProperties_allOf_v3.yaml renamed to modules/swagger-parser/src/test/resources/issue-2157/allOf-conflict-last-wins-inline.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
openapi: 3.0.1
22
info:
3-
title: Test API
3+
title: Test API - Conflict with $ref first (last non-null wins)
44
version: 1.0.0
55
paths:
66
/test:
@@ -19,9 +19,9 @@ components:
1919
additionalProperties: true
2020
TestSchema:
2121
allOf:
22+
- $ref: '#/components/schemas/ObjectWithAdditionalProperties'
2223
- type: object
2324
properties:
2425
name:
2526
type: string
2627
additionalProperties: false
27-
- $ref: '#/components/schemas/ObjectWithAdditionalProperties'

modules/swagger-parser/src/test/resources/additionalProperties_allOf_v4.yaml renamed to modules/swagger-parser/src/test/resources/issue-2157/allOf-conflict-last-wins-ref.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
openapi: 3.0.1
22
info:
3-
title: Test API
3+
title: Test API - Conflict with inline first (last non-null wins)
44
version: 1.0.0
55
paths:
66
/test:
@@ -19,9 +19,9 @@ components:
1919
additionalProperties: true
2020
TestSchema:
2121
allOf:
22-
- $ref: '#/components/schemas/ObjectWithAdditionalProperties'
2322
- type: object
2423
properties:
2524
name:
2625
type: string
2726
additionalProperties: false
27+
- $ref: '#/components/schemas/ObjectWithAdditionalProperties'
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Test API - Multiple schema-object additionalProperties conflict
4+
version: 1.0.0
5+
paths:
6+
/test:
7+
get:
8+
responses:
9+
'200':
10+
description: OK
11+
content:
12+
application/json:
13+
schema:
14+
$ref: '#/components/schemas/TestSchema'
15+
components:
16+
schemas:
17+
SchemaWithStringAdditional:
18+
type: object
19+
properties:
20+
name:
21+
type: string
22+
additionalProperties:
23+
type: string
24+
minLength: 1
25+
SchemaWithNumberAdditional:
26+
type: object
27+
properties:
28+
id:
29+
type: integer
30+
additionalProperties:
31+
type: number
32+
minimum: 0
33+
TestSchema:
34+
allOf:
35+
- $ref: '#/components/schemas/SchemaWithStringAdditional'
36+
- $ref: '#/components/schemas/SchemaWithNumberAdditional'
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Test API - resolveCombinators=false preserves structure
4+
version: 1.0.0
5+
paths:
6+
/test:
7+
get:
8+
responses:
9+
'200':
10+
description: OK
11+
content:
12+
application/json:
13+
schema:
14+
$ref: '#/components/schemas/TestSchema'
15+
components:
16+
schemas:
17+
BaseSchema:
18+
type: object
19+
properties:
20+
id:
21+
type: integer
22+
additionalProperties: true
23+
TestSchema:
24+
allOf:
25+
- $ref: '#/components/schemas/BaseSchema'
26+
- type: object
27+
properties:
28+
name:
29+
type: string

0 commit comments

Comments
 (0)