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

Commit 7b8abcf

Browse files
committed
Merge branch 'master' of https://github.com/swagger-api/swagger-parser into issue-1094
2 parents fce909d + 86181e2 commit 7b8abcf

11 files changed

Lines changed: 193 additions & 41 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,13 @@ public void processSchemas(Set<String> schemaKeys, Map<String, Schema> schemas)
221221

222222
schemaProcessor.processSchema(model);
223223

224-
//if we process a RefModel here, in the #/definitions table, we want to overwrite it with the referenced value
224+
//if we process a RefModel here, in the #/components/schemas table, we want to overwrite it with the referenced value
225225
if (model.get$ref() != null) {
226226
final String renamedRef = cache.getRenamedRef(originalRef);
227227

228228
if (renamedRef != null) {
229-
//we definitely resolved the referenced and shoved it in the definitions map
230-
// because the referenced model may be in the definitions map, we need to remove old instances
229+
//we definitely resolved the referenced and shoved it in the components map
230+
// because the referenced model may be in the components map, we need to remove old instances
231231
final Schema resolvedModel = schemas.get(renamedRef);
232232

233233
// ensure the reference isn't still in use

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/processors/PathsProcessor.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ protected void updateLocalRefs(PathItem path, String pathRef) {
161161
}
162162

163163
protected void updateLocalRefs(ApiResponse response, String pathRef) {
164+
if (response.get$ref() != null){
165+
if(isLocalRef(response.get$ref())) {
166+
response.set$ref(computeLocalRef(response.get$ref(), pathRef));
167+
}
168+
}
164169
if(response.getContent() != null) {
165170
Map<String, MediaType> content = response.getContent();
166171
for (String key: content.keySet()) {
@@ -187,6 +192,11 @@ protected void updateLocalRefs(Example example, String pathRef) {
187192
}
188193

189194
protected void updateLocalRefs(Parameter param, String pathRef) {
195+
if (param.get$ref() != null){
196+
if(isLocalRef(param.get$ref())) {
197+
param.set$ref(computeLocalRef(param.get$ref(), pathRef));
198+
}
199+
}
190200
if(param.getSchema() != null) {
191201
updateLocalRefs(param.getSchema(), pathRef);
192202
}
@@ -203,6 +213,11 @@ protected void updateLocalRefs(Parameter param, String pathRef) {
203213
}
204214

205215
protected void updateLocalRefs(RequestBody body, String pathRef) {
216+
if (body.get$ref() != null){
217+
if(isLocalRef(body.get$ref())) {
218+
body.set$ref(computeLocalRef(body.get$ref(), pathRef));
219+
}
220+
}
206221
if(body.getContent() != null) {
207222
Map<String, MediaType> content = body.getContent();
208223
for (String key: content.keySet()) {

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: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,6 @@ public void testIssue811_RefSchema_ToRefSchema() {
284284

285285
Assert.assertNotNull(openAPI);
286286
Assert.assertEquals(openAPI.getPaths().get("/").getGet().getResponses().get("200").getContent().get("application/json").getSchema().get$ref(),"#/components/schemas/schema-with-reference");
287-
288-
289287
}
290288

291289
@Test
@@ -295,7 +293,6 @@ public void testIssue811() {
295293
final OpenAPI openAPI = new OpenAPIV3Parser().readLocation("oapi-reference-test/index.yaml", null, options).getOpenAPI();
296294

297295
Assert.assertNotNull(openAPI);
298-
299296
Assert.assertEquals(openAPI.getPaths().get("/").getGet().getResponses().get("200").getContent().get("application/json").getSchema().get$ref(),"#/components/schemas/schema-with-reference");
300297

301298
}
@@ -305,7 +302,6 @@ public void testIssue719() {
305302
final OpenAPI openAPI = new OpenAPIV3Parser().readLocation("extensions-responses.yaml", null, new ParseOptions()).getOpenAPI();
306303

307304
Assert.assertNotNull(openAPI);
308-
309305
Assert.assertNotNull(openAPI.getPaths().getExtensions());
310306
Assert.assertNotNull(openAPI.getPaths().get("/something").getGet().getResponses().getExtensions());
311307

@@ -492,8 +488,6 @@ public void test30(@Injectable final List<AuthorizationValue> auths) throws Exce
492488

493489
@Test
494490
public void testResolveFully() throws Exception{
495-
496-
497491
String pathFile = FileUtils.readFileToString(new File("src/test/resources/oas3.yaml.template"));
498492
pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
499493
ParseOptions options = new ParseOptions();
@@ -507,6 +501,8 @@ public void testResolveFully() throws Exception{
507501
assertEquals(result.getOpenAPI().getComponents().getSchemas().get("OrderRef").getType(),"object");
508502
}
509503

504+
505+
510506
@Test
511507
public void testResolveEmpty(@Injectable final List<AuthorizationValue> auths) throws Exception{
512508
String pathFile = FileUtils.readFileToString(new File("src/test/resources/empty-oas.yaml"));
@@ -521,12 +517,9 @@ public void testResolveEmpty(@Injectable final List<AuthorizationValue> auths) t
521517

522518
@Test
523519
public void testResolveFullyExample() throws Exception{
524-
525-
526520
String pathFile = FileUtils.readFileToString(new File("src/test/resources/oas3.yaml.template"));
527521
pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
528522
ParseOptions options = new ParseOptions();
529-
//options.setResolve(true);
530523
options.setResolveFully(true);
531524

532525
SwaggerParseResult result = new OpenAPIV3Parser().readContents(pathFile, new ArrayList<>(), options );
@@ -567,10 +560,25 @@ public void testInlineModelResolver(@Injectable final List<AuthorizationValue> a
567560
}
568561

569562
@Test
570-
public void test30NoOptions(@Injectable final List<AuthorizationValue> auths) throws Exception{
563+
public void testRemotePathItemIssue1103(@Injectable final List<AuthorizationValue> auths) throws Exception{
564+
OpenAPI result = new OpenAPIV3Parser().read("issue-1103/remote-pathItem-swagger.yaml");
565+
Assert.assertNotNull(result);
566+
Assert.assertNotNull(result.getPaths().get("/Translation/{lang}"));
567+
Assert.assertEquals(result.getPaths().get("/Translation/{lang}").getPut().getParameters().get(0).getName(), "lang");
568+
}
569+
570+
571571

572+
@Test
573+
public void testRemoteParameterIssue1103(@Injectable final List<AuthorizationValue> auths) throws Exception{
574+
OpenAPI result = new OpenAPIV3Parser().read("issue-1103/remote-parameter-swagger.yaml");
575+
Assert.assertNotNull(result);
576+
Assert.assertEquals(result.getPaths().get("/Translation/{lang}").getPut().getParameters().get(0).getName(), "lang");
572577

578+
}
573579

580+
@Test
581+
public void test30NoOptions(@Injectable final List<AuthorizationValue> auths) throws Exception{
574582
String pathFile = FileUtils.readFileToString(new File("src/test/resources/oas3.yaml.template"));
575583
pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
576584

@@ -584,7 +592,6 @@ public void test30NoOptions(@Injectable final List<AuthorizationValue> auths) th
584592

585593
@Test
586594
public void testShellMethod(@Injectable final List<AuthorizationValue> auths){
587-
588595
String url = "http://localhost:${dynamicPort}/remote/spec";
589596
url = url.replace("${dynamicPort}", String.valueOf(this.serverPort));
590597

@@ -595,7 +602,6 @@ public void testShellMethod(@Injectable final List<AuthorizationValue> auths){
595602

596603
@Test
597604
public void testInlineModelResolverByUrl(){
598-
599605
String url = "http://localhost:${dynamicPort}/remote/json";
600606
url = url.replace("${dynamicPort}", String.valueOf(this.serverPort));
601607

@@ -619,6 +625,13 @@ public void testInlineModelResolverByUrl(){
619625
assertNotNull(userAddress.getProperties().get("street"));
620626
}
621627

628+
@Test
629+
public void testIssue1105() throws Exception {
630+
OpenAPI openAPI = new OpenAPIV3Parser().read("issue-1105/swagger-api.yaml");
631+
Assert.assertNotNull(openAPI);
632+
Assert.assertNotNull(openAPI.getComponents().getSchemas().get("ErrorCodeDescription"));
633+
}
634+
622635
@Test
623636
public void testRefAdditionalProperties() throws Exception {
624637
OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/relative/additionalProperties.yaml");
@@ -669,7 +682,6 @@ public void testComposedSchemaAdjacent(@Injectable final List<AuthorizationValue
669682
OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/composedSchemaRef.yaml", auths, options);
670683

671684
Assert.assertNotNull(openAPI);
672-
673685
Assert.assertTrue(openAPI.getComponents().getSchemas().size() == 5);
674686
Schema schema = openAPI.getPaths().get("/path").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
675687
Assert.assertTrue(schema instanceof ComposedSchema);
@@ -712,9 +724,7 @@ public void testRefPaths() throws Exception {
712724
" $ref: '#/paths/~1foo'";
713725

714726
OpenAPIV3Parser parser = new OpenAPIV3Parser();
715-
716727
OpenAPI openAPI = (parser.readContents(yaml,null,null)).getOpenAPI();
717-
718728
assertEquals(openAPI.getPaths().get("foo"),openAPI.getPaths().get("foo2"));
719729

720730

@@ -742,7 +752,6 @@ public void testModelParameters() throws Exception {
742752
" description: ok";
743753

744754
OpenAPIV3Parser parser = new OpenAPIV3Parser();
745-
746755
OpenAPI openAPI = (parser.readContents(yaml,null,null)).getOpenAPI();
747756

748757
}
@@ -890,7 +899,6 @@ public void testUniqueParameters() throws Exception {
890899
@Test
891900
public void testLoadRelativeFileTree_Json() throws Exception {
892901
final OpenAPI openAPI = doRelativeFileTest("src/test/resources/relative-file-references/json/parent.json");
893-
//Json.mapper().writerWithDefaultPrettyPrinter().writeValue(new File("resolved.json"), openAPI);
894902
}
895903

896904
@Test
@@ -1003,7 +1011,6 @@ public void testIssue75() {
10031011

10041012
assertNotNull(model);
10051013
assertTrue(model instanceof ArraySchema);
1006-
10071014
ArraySchema am = (ArraySchema) model;
10081015
assertTrue(am.getItems() instanceof ByteArraySchema);
10091016
assertEquals(am.getItems().getFormat(), "byte");
@@ -1013,7 +1020,6 @@ public void testIssue75() {
10131020
public void testIssue62() {
10141021
OpenAPIV3Parser parser = new OpenAPIV3Parser();
10151022
final OpenAPI openAPI = parser.read("https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/fixtures/v2.0/json/resources/resourceWithLinkedDefinitions.json");
1016-
10171023
assertNotNull(openAPI.getPaths().get("/pets/{petId}").getGet());
10181024
}
10191025

@@ -1032,7 +1038,6 @@ public void testParameterRequired() {
10321038
OpenAPIV3Parser parser = new OpenAPIV3Parser();
10331039
final OpenAPI openAPI = parser.read("src/test/resources/petstore.yaml");
10341040
final List<Parameter> operationParams = openAPI.getPaths().get("/pet/{petId}").getPost().getParameters();
1035-
10361041
final PathParameter pathParameter = (PathParameter) operationParams.get(0);
10371042
Assert.assertTrue(pathParameter.getRequired());
10381043
}
@@ -1041,7 +1046,6 @@ public void testParameterRequired() {
10411046
public void testIssue108() {
10421047
OpenAPIV3Parser parser = new OpenAPIV3Parser();
10431048
final OpenAPI openAPI = parser.read("src/test/resources/issue_108.yaml");
1044-
10451049
assertNotNull(openAPI);
10461050
}
10471051

@@ -1110,15 +1114,13 @@ public void testIssue292WithCSVCollectionFormat() {
11101114
@Test
11111115
public void testIssue255() {
11121116
OpenAPIV3Parser parser = new OpenAPIV3Parser();
1113-
11141117
OpenAPI openAPI = parser.read("objectExample.yaml");
11151118
assertEquals(openAPI.getComponents().getSchemas().get("SamplePayload").getExample().toString(), "[{\"op\":\"replace\",\"path\":\"/s\",\"v\":\"w\"}]");
11161119
}
11171120

11181121
@Test
11191122
public void testIssue286() {
11201123
OpenAPIV3Parser parser = new OpenAPIV3Parser();
1121-
11221124
OpenAPI openAPI = parser.read("issue_286.yaml");
11231125
Schema response = openAPI.getPaths().get("/").getGet().getResponses().get("200").getContent().get("*/*").getSchema();
11241126
assertTrue(response.get$ref() != null);
@@ -1164,15 +1166,10 @@ public void testIssue360() {
11641166

11651167
assertEquals(sbpModel.getType(), "string");
11661168
assertEquals(sbpModel.getFormat(), "uuid");
1167-
11681169
RequestBody bodyParameter = openAPI.getPaths().get("/evenMorePets").getPost().getRequestBody();
1169-
11701170
assertTrue(bodyParameter.getRequired());
1171-
11721171
Schema refModel = bodyParameter.getContent().get("application/json").getSchema();
11731172
assertTrue(refModel.get$ref() != null);
1174-
1175-
11761173
assertEquals(refModel.get$ref(), "#/components/schemas/Pet");
11771174
}
11781175

@@ -1186,10 +1183,7 @@ private OpenAPI doRelativeFileTest(String location) {
11861183
Json.prettyPrint(readResult.getMessages());
11871184
}
11881185
final OpenAPI openAPI = readResult.getOpenAPI();
1189-
1190-
11911186
final PathItem path = openAPI.getPaths().get("/health");
1192-
11931187
assertEquals(path.getClass(), PathItem.class); //we successfully converted the RefPath to a Path
11941188

11951189
final List<Parameter> parameters = path.getParameters();
@@ -1201,7 +1195,6 @@ private OpenAPI doRelativeFileTest(String location) {
12011195
assertParamDetails(operationParams, 0, PathParameter.class, "param3", "path");
12021196
assertParamDetails(operationParams, 1, HeaderParameter.class, "param4", "header");
12031197

1204-
12051198
final Map<String, ApiResponse> responsesMap = operation.getResponses();
12061199

12071200
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: 42 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
@@ -2280,6 +2319,7 @@ public void readMissingServerObject() throws Exception {
22802319
final OpenAPI openAPI = result.getOpenAPI();
22812320
Assert.assertNotNull(openAPI);
22822321

2322+
22832323
assertEquals(openAPI.getServers().get(0).getUrl(),"/");
22842324
}
22852325

0 commit comments

Comments
 (0)