Skip to content

Commit da9926f

Browse files
committed
add tests
1 parent 9cb2e39 commit da9926f

13 files changed

+395
-23
lines changed

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

Lines changed: 168 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,177 @@ public void testAdditionalPropertiesWithAllOfV3() {
813801
}
814802

815803
@Test
816-
public void testAdditionalPropertiesWithAllOfV4() {
804+
public void testIssue2157_AllOf_PreservesSchemaObject() {
817805
ParseOptions options = new ParseOptions();
818806
options.setResolveFully(true);
819807
SwaggerParseResult result = new OpenAPIParser().readLocation(
820-
"additionalProperties_allOf_v4.yaml", null, options);
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() {
825+
ParseOptions options = new ParseOptions();
826+
options.setResolveFully(true);
827+
SwaggerParseResult result = new OpenAPIParser().readLocation(
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_OneOf_PreservesBoolean() {
871+
ParseOptions options = new ParseOptions();
872+
options.setResolveFully(true);
873+
SwaggerParseResult result = new OpenAPIParser().readLocation(
874+
"issue-2157/oneOf-preserves-boolean.yaml", null, options);
875+
assertNotNull(result.getOpenAPI());
876+
Schema schema = result.getOpenAPI().getComponents().getSchemas().get("TestSchema");
877+
assertNotNull(schema);
878+
assertNotNull(schema.getOneOf());
879+
assertEquals(schema.getOneOf().size(), 1);
880+
Schema oneOfBranch = (Schema) schema.getOneOf().get(0);
881+
assertNotNull(oneOfBranch.getAdditionalProperties());
882+
assertTrue((Boolean) oneOfBranch.getAdditionalProperties());
883+
}
884+
885+
@Test
886+
public void testIssue2157_OneOf_PreservesSchemaObject() {
887+
ParseOptions options = new ParseOptions();
888+
options.setResolveFully(true);
889+
SwaggerParseResult result = new OpenAPIParser().readLocation(
890+
"issue-2157/oneOf-preserves-schema-object.yaml", null, options);
891+
assertNotNull(result.getOpenAPI());
892+
Schema schema = result.getOpenAPI().getComponents().getSchemas().get("TestSchema");
893+
assertNotNull(schema);
894+
assertNotNull(schema.getOneOf());
895+
assertEquals(schema.getOneOf().size(), 1);
896+
Schema oneOfBranch = (Schema) schema.getOneOf().get(0);
897+
assertNotNull(oneOfBranch.getAdditionalProperties());
898+
assertTrue(oneOfBranch.getAdditionalProperties() instanceof Schema);
899+
Schema additionalPropsSchema = (Schema) oneOfBranch.getAdditionalProperties();
900+
assertEquals(additionalPropsSchema.getType(), "string");
901+
assertEquals(additionalPropsSchema.getMinLength(), Integer.valueOf(1));
902+
}
903+
904+
@Test
905+
public void testIssue2157_OneOf_MultipleBranchesIndependent() {
906+
ParseOptions options = new ParseOptions();
907+
options.setResolveFully(true);
908+
SwaggerParseResult result = new OpenAPIParser().readLocation(
909+
"issue-2157/oneOf-multiple-branches-independent.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(), 2);
915+
Schema firstBranch = (Schema) schema.getOneOf().get(0);
916+
Schema secondBranch = (Schema) schema.getOneOf().get(1);
917+
assertNotNull(firstBranch.getAdditionalProperties());
918+
assertTrue((Boolean) firstBranch.getAdditionalProperties());
919+
assertNotNull(secondBranch.getAdditionalProperties());
920+
assertFalse((Boolean) secondBranch.getAdditionalProperties());
921+
}
922+
923+
@Test
924+
public void testIssue2157_AnyOf_PreservesBoolean() {
925+
ParseOptions options = new ParseOptions();
926+
options.setResolveFully(true);
927+
SwaggerParseResult result = new OpenAPIParser().readLocation(
928+
"issue-2157/anyOf-preserves-boolean.yaml", null, options);
929+
assertNotNull(result.getOpenAPI());
930+
Schema schema = result.getOpenAPI().getComponents().getSchemas().get("TestSchema");
931+
assertNotNull(schema);
932+
assertNotNull(schema.getAnyOf());
933+
assertEquals(schema.getAnyOf().size(), 1);
934+
Schema anyOfBranch = (Schema) schema.getAnyOf().get(0);
935+
assertNotNull(anyOfBranch.getAdditionalProperties());
936+
assertTrue((Boolean) anyOfBranch.getAdditionalProperties());
937+
}
938+
939+
@Test
940+
public void testIssue2157_AnyOf_PreservesSchemaObject() {
941+
ParseOptions options = new ParseOptions();
942+
options.setResolveFully(true);
943+
SwaggerParseResult result = new OpenAPIParser().readLocation(
944+
"issue-2157/anyOf-preserves-schema-object.yaml", null, options);
945+
assertNotNull(result.getOpenAPI());
946+
Schema schema = result.getOpenAPI().getComponents().getSchemas().get("TestSchema");
947+
assertNotNull(schema);
948+
assertNotNull(schema.getAnyOf());
949+
assertEquals(schema.getAnyOf().size(), 1);
950+
Schema anyOfBranch = (Schema) schema.getAnyOf().get(0);
951+
assertNotNull(anyOfBranch.getAdditionalProperties());
952+
assertTrue(anyOfBranch.getAdditionalProperties() instanceof Schema);
953+
Schema additionalPropsSchema = (Schema) anyOfBranch.getAdditionalProperties();
954+
assertEquals(additionalPropsSchema.getType(), "string");
955+
assertEquals(additionalPropsSchema.getMinLength(), Integer.valueOf(1));
956+
}
957+
958+
@Test
959+
public void testIssue2157_AnyOf_MultipleBranchesIndependent() {
960+
ParseOptions options = new ParseOptions();
961+
options.setResolveFully(true);
962+
SwaggerParseResult result = new OpenAPIParser().readLocation(
963+
"issue-2157/anyOf-multiple-branches-independent.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(), 2);
969+
Schema firstBranch = (Schema) schema.getAnyOf().get(0);
970+
Schema secondBranch = (Schema) schema.getAnyOf().get(1);
971+
assertNotNull(firstBranch.getAdditionalProperties());
972+
assertTrue((Boolean) firstBranch.getAdditionalProperties());
973+
assertNotNull(secondBranch.getAdditionalProperties());
974+
assertFalse((Boolean) secondBranch.getAdditionalProperties());
975+
}
827976
}
828977

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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Test API for Issue 2157
4+
version: 1.0.0
5+
paths:
6+
/pet:
7+
put:
8+
requestBody:
9+
description: Update an existent pet in the store
10+
content:
11+
application/json:
12+
schema:
13+
allOf:
14+
- $ref: '#/components/schemas/Pet'
15+
responses:
16+
'200':
17+
description: OK
18+
components:
19+
schemas:
20+
Pet:
21+
type: object
22+
properties:
23+
id:
24+
type: integer
25+
format: int64
26+
example: 10
27+
name:
28+
type: string
29+
example: doggie
30+
additionalProperties:
31+
type: string
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Test API - Schema Object additionalProperties
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:
23+
type: string
24+
minLength: 1
25+
maxLength: 100
26+
TestSchema:
27+
allOf:
28+
- $ref: '#/components/schemas/BaseSchema'
29+
- type: object
30+
properties:
31+
name:
32+
type: string

modules/swagger-parser/src/test/resources/additionalProperties_allOf_v2.yaml renamed to modules/swagger-parser/src/test/resources/issue-2157/allOf-ref-and-inline-merge.yaml

File renamed without changes.

modules/swagger-parser/src/test/resources/additionalProperties_allOf_v1.yaml renamed to modules/swagger-parser/src/test/resources/issue-2157/allOf-single-ref-boolean.yaml

File renamed without changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Test API - anyOf conflict last wins
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+
ObjectWithAdditionalPropertiesTrue:
18+
type: object
19+
additionalProperties: true
20+
ObjectWithAdditionalPropertiesFalse:
21+
type: object
22+
additionalProperties: false
23+
TestSchema:
24+
anyOf:
25+
- $ref: '#/components/schemas/ObjectWithAdditionalPropertiesTrue'
26+
- $ref: '#/components/schemas/ObjectWithAdditionalPropertiesFalse'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Test API - additionalProperties with anyOf
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+
ObjectWithAdditionalProperties:
18+
type: object
19+
additionalProperties: true
20+
properties:
21+
name:
22+
type: string
23+
TestSchema:
24+
anyOf:
25+
- $ref: '#/components/schemas/ObjectWithAdditionalProperties'

0 commit comments

Comments
 (0)