Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit 7ec2d19

Browse files
committed
2 parents fbe4f4c + 266b89d commit 7ec2d19

7 files changed

Lines changed: 102 additions & 43 deletions

File tree

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ComponentsProcessor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ private void processParameters(Set<String> ParametersKey, Map<String, Parameter>
190190
for (String parametersName : ParametersKey) {
191191
final Parameter parameter = parameters.get(parametersName);
192192
parameterProcessor.processParameter(parameter);
193-
194193
}
195194
}
196195

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
182182
return newRef;
183183
}
184184

185-
private void processProperty(Schema property, String file) {
185+
private void processSchema(Schema property, String file) {
186186
if (property != null) {
187187
if (StringUtils.isNotBlank(property.get$ref())) {
188188
processRefSchema(property, file);
@@ -191,10 +191,10 @@ private void processProperty(Schema property, String file) {
191191
processProperties(property.getProperties(), file);
192192
}
193193
if (property instanceof ArraySchema) {
194-
processProperty(((ArraySchema) property).getItems(), file);
194+
processSchema(((ArraySchema) property).getItems(), file);
195195
}
196196
if (property.getAdditionalProperties() instanceof Schema) {
197-
processProperty(((Schema) property.getAdditionalProperties()), file);
197+
processSchema(((Schema) property.getAdditionalProperties()), file);
198198
}
199199
if (property instanceof ComposedSchema) {
200200
ComposedSchema composed = (ComposedSchema) property;
@@ -208,7 +208,7 @@ private void processProperty(Schema property, String file) {
208208
private void processProperties(Collection<Schema> properties, String file) {
209209
if (properties != null) {
210210
for (Schema property : properties) {
211-
processProperty(property, file);
211+
processSchema(property, file);
212212
}
213213
}
214214
}
@@ -269,6 +269,8 @@ public String processRefToExternalResponse(String $ref, RefFormat refFormat) {
269269
} else {
270270
processRefToExternalSchema(file + schema.get$ref(), RefFormat.RELATIVE);
271271
}
272+
}else{
273+
processSchema(schema,file);
272274
}
273275
}
274276
}

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,6 +2262,9 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
22622262
schema.setType(type);
22632263
}
22642264
}
2265+
if("array".equals( schema.getType()) && !(schema instanceof ArraySchema && ((ArraySchema) schema).getItems() != null)) {
2266+
result.missing(location, "items");
2267+
}
22652268
}
22662269

22672270
ObjectNode notObj = getObject("not", node, false, location, result);

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,6 @@ public void testIssue811_RefSchema_ToRefSchema() {
275275

276276
Assert.assertNotNull(openAPI);
277277
Assert.assertEquals(openAPI.getPaths().get("/").getGet().getResponses().get("200").getContent().get("application/json").getSchema().get$ref(),"#/components/schemas/schema-with-reference");
278-
279-
280278
}
281279

282280
@Test
@@ -286,7 +284,6 @@ public void testIssue811() {
286284
final OpenAPI openAPI = new OpenAPIV3Parser().readLocation("oapi-reference-test/index.yaml", null, options).getOpenAPI();
287285

288286
Assert.assertNotNull(openAPI);
289-
290287
Assert.assertEquals(openAPI.getPaths().get("/").getGet().getResponses().get("200").getContent().get("application/json").getSchema().get$ref(),"#/components/schemas/schema-with-reference");
291288

292289
}
@@ -296,7 +293,6 @@ public void testIssue719() {
296293
final OpenAPI openAPI = new OpenAPIV3Parser().readLocation("extensions-responses.yaml", null, new ParseOptions()).getOpenAPI();
297294

298295
Assert.assertNotNull(openAPI);
299-
300296
Assert.assertNotNull(openAPI.getPaths().getExtensions());
301297
Assert.assertNotNull(openAPI.getPaths().get("/something").getGet().getResponses().getExtensions());
302298

@@ -483,8 +479,6 @@ public void test30(@Injectable final List<AuthorizationValue> auths) throws Exce
483479

484480
@Test
485481
public void testResolveFully() throws Exception{
486-
487-
488482
String pathFile = FileUtils.readFileToString(new File("src/test/resources/oas3.yaml.template"));
489483
pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
490484
ParseOptions options = new ParseOptions();
@@ -514,12 +508,9 @@ public void testResolveEmpty(@Injectable final List<AuthorizationValue> auths) t
514508

515509
@Test
516510
public void testResolveFullyExample() throws Exception{
517-
518-
519511
String pathFile = FileUtils.readFileToString(new File("src/test/resources/oas3.yaml.template"));
520512
pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
521513
ParseOptions options = new ParseOptions();
522-
//options.setResolve(true);
523514
options.setResolveFully(true);
524515

525516
SwaggerParseResult result = new OpenAPIV3Parser().readContents(pathFile, new ArrayList<>(), options );
@@ -561,19 +552,16 @@ public void testInlineModelResolver(@Injectable final List<AuthorizationValue> a
561552

562553
@Test
563554
public void testRemotePathItemIssue1103(@Injectable final List<AuthorizationValue> auths) throws Exception{
564-
565-
566555
OpenAPI result = new OpenAPIV3Parser().read("issue-1103/remote-pathItem-swagger.yaml");
567-
568556
Assert.assertNotNull(result);
569557
Assert.assertNotNull(result.getPaths().get("/Translation/{lang}"));
570558
Assert.assertEquals(result.getPaths().get("/Translation/{lang}").getPut().getParameters().get(0).getName(), "lang");
571559
}
572560

573561

562+
574563
@Test
575564
public void testRemoteParameterIssue1103(@Injectable final List<AuthorizationValue> auths) throws Exception{
576-
577565
OpenAPI result = new OpenAPIV3Parser().read("issue-1103/remote-parameter-swagger.yaml");
578566
Assert.assertNotNull(result);
579567
Assert.assertEquals(result.getPaths().get("/Translation/{lang}").getPut().getParameters().get(0).getName(), "lang");
@@ -582,7 +570,6 @@ public void testRemoteParameterIssue1103(@Injectable final List<AuthorizationVal
582570

583571
@Test
584572
public void test30NoOptions(@Injectable final List<AuthorizationValue> auths) throws Exception{
585-
586573
String pathFile = FileUtils.readFileToString(new File("src/test/resources/oas3.yaml.template"));
587574
pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
588575

@@ -596,7 +583,6 @@ public void test30NoOptions(@Injectable final List<AuthorizationValue> auths) th
596583

597584
@Test
598585
public void testShellMethod(@Injectable final List<AuthorizationValue> auths){
599-
600586
String url = "http://localhost:${dynamicPort}/remote/spec";
601587
url = url.replace("${dynamicPort}", String.valueOf(this.serverPort));
602588

@@ -607,7 +593,6 @@ public void testShellMethod(@Injectable final List<AuthorizationValue> auths){
607593

608594
@Test
609595
public void testInlineModelResolverByUrl(){
610-
611596
String url = "http://localhost:${dynamicPort}/remote/json";
612597
url = url.replace("${dynamicPort}", String.valueOf(this.serverPort));
613598

@@ -631,6 +616,13 @@ public void testInlineModelResolverByUrl(){
631616
assertNotNull(userAddress.getProperties().get("street"));
632617
}
633618

619+
@Test
620+
public void testIssue1105() throws Exception {
621+
OpenAPI openAPI = new OpenAPIV3Parser().read("issue-1105/swagger-api.yaml");
622+
Assert.assertNotNull(openAPI);
623+
Assert.assertNotNull(openAPI.getComponents().getSchemas().get("ErrorCodeDescription"));
624+
}
625+
634626
@Test
635627
public void testRefAdditionalProperties() throws Exception {
636628
OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/relative/additionalProperties.yaml");
@@ -681,7 +673,6 @@ public void testComposedSchemaAdjacent(@Injectable final List<AuthorizationValue
681673
OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/composedSchemaRef.yaml", auths, options);
682674

683675
Assert.assertNotNull(openAPI);
684-
685676
Assert.assertTrue(openAPI.getComponents().getSchemas().size() == 5);
686677
Schema schema = openAPI.getPaths().get("/path").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
687678
Assert.assertTrue(schema instanceof ComposedSchema);
@@ -724,9 +715,7 @@ public void testRefPaths() throws Exception {
724715
" $ref: '#/paths/~1foo'";
725716

726717
OpenAPIV3Parser parser = new OpenAPIV3Parser();
727-
728718
OpenAPI openAPI = (parser.readContents(yaml,null,null)).getOpenAPI();
729-
730719
assertEquals(openAPI.getPaths().get("foo"),openAPI.getPaths().get("foo2"));
731720

732721

@@ -754,7 +743,6 @@ public void testModelParameters() throws Exception {
754743
" description: ok";
755744

756745
OpenAPIV3Parser parser = new OpenAPIV3Parser();
757-
758746
OpenAPI openAPI = (parser.readContents(yaml,null,null)).getOpenAPI();
759747

760748
}
@@ -902,7 +890,6 @@ public void testUniqueParameters() throws Exception {
902890
@Test
903891
public void testLoadRelativeFileTree_Json() throws Exception {
904892
final OpenAPI openAPI = doRelativeFileTest("src/test/resources/relative-file-references/json/parent.json");
905-
//Json.mapper().writerWithDefaultPrettyPrinter().writeValue(new File("resolved.json"), openAPI);
906893
}
907894

908895
@Test
@@ -1015,7 +1002,6 @@ public void testIssue75() {
10151002

10161003
assertNotNull(model);
10171004
assertTrue(model instanceof ArraySchema);
1018-
10191005
ArraySchema am = (ArraySchema) model;
10201006
assertTrue(am.getItems() instanceof ByteArraySchema);
10211007
assertEquals(am.getItems().getFormat(), "byte");
@@ -1025,7 +1011,6 @@ public void testIssue75() {
10251011
public void testIssue62() {
10261012
OpenAPIV3Parser parser = new OpenAPIV3Parser();
10271013
final OpenAPI openAPI = parser.read("https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/fixtures/v2.0/json/resources/resourceWithLinkedDefinitions.json");
1028-
10291014
assertNotNull(openAPI.getPaths().get("/pets/{petId}").getGet());
10301015
}
10311016

@@ -1044,7 +1029,6 @@ public void testParameterRequired() {
10441029
OpenAPIV3Parser parser = new OpenAPIV3Parser();
10451030
final OpenAPI openAPI = parser.read("src/test/resources/petstore.yaml");
10461031
final List<Parameter> operationParams = openAPI.getPaths().get("/pet/{petId}").getPost().getParameters();
1047-
10481032
final PathParameter pathParameter = (PathParameter) operationParams.get(0);
10491033
Assert.assertTrue(pathParameter.getRequired());
10501034
}
@@ -1053,7 +1037,6 @@ public void testParameterRequired() {
10531037
public void testIssue108() {
10541038
OpenAPIV3Parser parser = new OpenAPIV3Parser();
10551039
final OpenAPI openAPI = parser.read("src/test/resources/issue_108.yaml");
1056-
10571040
assertNotNull(openAPI);
10581041
}
10591042

@@ -1122,15 +1105,13 @@ public void testIssue292WithCSVCollectionFormat() {
11221105
@Test
11231106
public void testIssue255() {
11241107
OpenAPIV3Parser parser = new OpenAPIV3Parser();
1125-
11261108
OpenAPI openAPI = parser.read("objectExample.yaml");
11271109
assertEquals(openAPI.getComponents().getSchemas().get("SamplePayload").getExample().toString(), "[{\"op\":\"replace\",\"path\":\"/s\",\"v\":\"w\"}]");
11281110
}
11291111

11301112
@Test
11311113
public void testIssue286() {
11321114
OpenAPIV3Parser parser = new OpenAPIV3Parser();
1133-
11341115
OpenAPI openAPI = parser.read("issue_286.yaml");
11351116
Schema response = openAPI.getPaths().get("/").getGet().getResponses().get("200").getContent().get("*/*").getSchema();
11361117
assertTrue(response.get$ref() != null);
@@ -1176,15 +1157,10 @@ public void testIssue360() {
11761157

11771158
assertEquals(sbpModel.getType(), "string");
11781159
assertEquals(sbpModel.getFormat(), "uuid");
1179-
11801160
RequestBody bodyParameter = openAPI.getPaths().get("/evenMorePets").getPost().getRequestBody();
1181-
11821161
assertTrue(bodyParameter.getRequired());
1183-
11841162
Schema refModel = bodyParameter.getContent().get("application/json").getSchema();
11851163
assertTrue(refModel.get$ref() != null);
1186-
1187-
11881164
assertEquals(refModel.get$ref(), "#/components/schemas/Pet");
11891165
}
11901166

@@ -1198,10 +1174,7 @@ private OpenAPI doRelativeFileTest(String location) {
11981174
Json.prettyPrint(readResult.getMessages());
11991175
}
12001176
final OpenAPI openAPI = readResult.getOpenAPI();
1201-
1202-
12031177
final PathItem path = openAPI.getPaths().get("/health");
1204-
12051178
assertEquals(path.getClass(), PathItem.class); //we successfully converted the RefPath to a Path
12061179

12071180
final List<Parameter> parameters = path.getParameters();
@@ -1213,7 +1186,6 @@ private OpenAPI doRelativeFileTest(String location) {
12131186
assertParamDetails(operationParams, 0, PathParameter.class, "param3", "path");
12141187
assertParamDetails(operationParams, 1, HeaderParameter.class, "param4", "header");
12151188

1216-
12171189
final Map<String, ApiResponse> responsesMap = operation.getResponses();
12181190

12191191
assertResponse(openAPI, responsesMap, "200","application/json", "Health information from the server", "#/components/schemas/health");

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,47 @@ public void testArrayQueryParam() throws Exception {
763763
assertTrue(((ArraySchema) p).getItems() instanceof StringSchema);
764764
}
765765

766+
@Test
767+
public void testArrayItems() {
768+
String yaml =
769+
"openapi: 3.0.0\n" +
770+
"info:\n" +
771+
" title: Test\n" +
772+
" version: 1.0.0\n" +
773+
"paths:\n" +
774+
" \"/store/inventory\":\n" +
775+
" post:\n" +
776+
" requestBody:\n" +
777+
" content:\n" +
778+
" application/json:\n" +
779+
" schema:\n" +
780+
" type: array\n" +
781+
" minItems: 1\n" +
782+
" responses:\n" +
783+
" '200':\n" +
784+
" description: successful operation\n" +
785+
" content:\n" +
786+
" application/json:\n" +
787+
" schema:\n" +
788+
" items:\n" +
789+
" type: string"
790+
;
791+
792+
OpenAPIV3Parser parser = new OpenAPIV3Parser();
793+
SwaggerParseResult result = parser.readContents(yaml, null, null);
794+
assertEquals(result.getMessages(), Arrays.asList("attribute paths.'/store/inventory'(post).requestBody.content.schema.items is missing"));
795+
796+
OpenAPI openAPI = result.getOpenAPI();
797+
798+
Schema body = openAPI.getPaths().get("/store/inventory").getPost().getRequestBody().getContent().get("application/json").getSchema();
799+
assertFalse(body.getClass().equals( ArraySchema.class), "body is an ArraySchema");
800+
assertEquals(body.getType(), "array");
801+
assertEquals(body.getMinItems(), Integer.valueOf(1));
802+
803+
Schema response = openAPI.getPaths().get("/store/inventory").getPost().getResponses().get("200").getContent().get("application/json").getSchema();
804+
assertTrue(response.getClass().equals( ArraySchema.class), "response is an ArraySchema");
805+
assertEquals(body.getType(), "array");
806+
}
766807

767808
@Test(description = "it should read a top-level extension per https://github.com/openAPI-api/validator-badge/issues/59")
768809
public void testToplevelExtension() throws Exception {
@@ -2263,8 +2304,6 @@ public void readServerObject(JsonNode rootNode) throws Exception {
22632304
Assert.assertEquals(server.get(2).getExtensions().get("x-server").toString(),"server extension");
22642305
Assert.assertEquals(server.get(2).getVariables().get("basePath").getDescription(),"testing overwriting");
22652306
Assert.assertEquals(server.get(2).getVariables().get("basePath").getDefault(),"v2");
2266-
2267-
22682307
}
22692308

22702309
@Test
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
openapi: 3.0.2
2+
info:
3+
title: API Components
4+
description: Common components
5+
version: '1.0'
6+
components:
7+
schemas:
8+
ErrorCodeDescription:
9+
type: object
10+
properties:
11+
code:
12+
format: int32
13+
type: integer
14+
description: HTTP error code.
15+
description:
16+
type: string
17+
description: Brief description of the error.
18+
details:
19+
type: string
20+
description: Details about the error.
21+
responses:
22+
401:
23+
description: User is not authorized for this action
24+
content:
25+
application/json:
26+
schema:
27+
uniqueItems: false
28+
type: array
29+
items:
30+
$ref: "#/components/schemas/ErrorCodeDescription"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
openapi: "3.0.2"
2+
info:
3+
title: User and Session
4+
description: Admin API endpoints.
5+
version: "0.1"
6+
paths:
7+
/users:
8+
get:
9+
responses:
10+
"401":
11+
$ref: "./domain.yaml#/components/responses/401"
12+
components: {
13+
schemas: {}
14+
}

0 commit comments

Comments
 (0)