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

Commit 86181e2

Browse files
authored
Merge pull request swagger-api#1104 from swagger-api/issue-remote-ref
Remote PathItem Resolving - issue swagger-api#1103
2 parents 266b89d + 7ec2d19 commit 86181e2

7 files changed

Lines changed: 94 additions & 37 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/PathsProcessor.java

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

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

190195
protected void updateLocalRefs(Parameter param, String pathRef) {
196+
if (param.get$ref() != null){
197+
if(isLocalRef(param.get$ref())) {
198+
param.set$ref(computeLocalRef(param.get$ref(), pathRef));
199+
}
200+
}
191201
if(param.getSchema() != null) {
192202
updateLocalRefs(param.getSchema(), pathRef);
193203
}
@@ -204,6 +214,11 @@ protected void updateLocalRefs(Parameter param, String pathRef) {
204214
}
205215

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

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

Lines changed: 20 additions & 32 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();
@@ -498,6 +492,8 @@ public void testResolveFully() throws Exception{
498492
assertEquals(result.getOpenAPI().getComponents().getSchemas().get("OrderRef").getType(),"object");
499493
}
500494

495+
496+
501497
@Test
502498
public void testResolveEmpty(@Injectable final List<AuthorizationValue> auths) throws Exception{
503499
String pathFile = FileUtils.readFileToString(new File("src/test/resources/empty-oas.yaml"));
@@ -512,12 +508,9 @@ public void testResolveEmpty(@Injectable final List<AuthorizationValue> auths) t
512508

513509
@Test
514510
public void testResolveFullyExample() throws Exception{
515-
516-
517511
String pathFile = FileUtils.readFileToString(new File("src/test/resources/oas3.yaml.template"));
518512
pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
519513
ParseOptions options = new ParseOptions();
520-
//options.setResolve(true);
521514
options.setResolveFully(true);
522515

523516
SwaggerParseResult result = new OpenAPIV3Parser().readContents(pathFile, new ArrayList<>(), options );
@@ -558,8 +551,25 @@ public void testInlineModelResolver(@Injectable final List<AuthorizationValue> a
558551
}
559552

560553
@Test
561-
public void test30NoOptions(@Injectable final List<AuthorizationValue> auths) throws Exception{
554+
public void testRemotePathItemIssue1103(@Injectable final List<AuthorizationValue> auths) throws Exception{
555+
OpenAPI result = new OpenAPIV3Parser().read("issue-1103/remote-pathItem-swagger.yaml");
556+
Assert.assertNotNull(result);
557+
Assert.assertNotNull(result.getPaths().get("/Translation/{lang}"));
558+
Assert.assertEquals(result.getPaths().get("/Translation/{lang}").getPut().getParameters().get(0).getName(), "lang");
559+
}
560+
562561

562+
563+
@Test
564+
public void testRemoteParameterIssue1103(@Injectable final List<AuthorizationValue> auths) throws Exception{
565+
OpenAPI result = new OpenAPIV3Parser().read("issue-1103/remote-parameter-swagger.yaml");
566+
Assert.assertNotNull(result);
567+
Assert.assertEquals(result.getPaths().get("/Translation/{lang}").getPut().getParameters().get(0).getName(), "lang");
568+
569+
}
570+
571+
@Test
572+
public void test30NoOptions(@Injectable final List<AuthorizationValue> auths) throws Exception{
563573
String pathFile = FileUtils.readFileToString(new File("src/test/resources/oas3.yaml.template"));
564574
pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
565575

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

574584
@Test
575585
public void testShellMethod(@Injectable final List<AuthorizationValue> auths){
576-
577586
String url = "http://localhost:${dynamicPort}/remote/spec";
578587
url = url.replace("${dynamicPort}", String.valueOf(this.serverPort));
579588

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

585594
@Test
586595
public void testInlineModelResolverByUrl(){
587-
588596
String url = "http://localhost:${dynamicPort}/remote/json";
589597
url = url.replace("${dynamicPort}", String.valueOf(this.serverPort));
590598

@@ -665,7 +673,6 @@ public void testComposedSchemaAdjacent(@Injectable final List<AuthorizationValue
665673
OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/composedSchemaRef.yaml", auths, options);
666674

667675
Assert.assertNotNull(openAPI);
668-
669676
Assert.assertTrue(openAPI.getComponents().getSchemas().size() == 5);
670677
Schema schema = openAPI.getPaths().get("/path").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
671678
Assert.assertTrue(schema instanceof ComposedSchema);
@@ -708,9 +715,7 @@ public void testRefPaths() throws Exception {
708715
" $ref: '#/paths/~1foo'";
709716

710717
OpenAPIV3Parser parser = new OpenAPIV3Parser();
711-
712718
OpenAPI openAPI = (parser.readContents(yaml,null,null)).getOpenAPI();
713-
714719
assertEquals(openAPI.getPaths().get("foo"),openAPI.getPaths().get("foo2"));
715720

716721

@@ -738,7 +743,6 @@ public void testModelParameters() throws Exception {
738743
" description: ok";
739744

740745
OpenAPIV3Parser parser = new OpenAPIV3Parser();
741-
742746
OpenAPI openAPI = (parser.readContents(yaml,null,null)).getOpenAPI();
743747

744748
}
@@ -886,7 +890,6 @@ public void testUniqueParameters() throws Exception {
886890
@Test
887891
public void testLoadRelativeFileTree_Json() throws Exception {
888892
final OpenAPI openAPI = doRelativeFileTest("src/test/resources/relative-file-references/json/parent.json");
889-
//Json.mapper().writerWithDefaultPrettyPrinter().writeValue(new File("resolved.json"), openAPI);
890893
}
891894

892895
@Test
@@ -999,7 +1002,6 @@ public void testIssue75() {
9991002

10001003
assertNotNull(model);
10011004
assertTrue(model instanceof ArraySchema);
1002-
10031005
ArraySchema am = (ArraySchema) model;
10041006
assertTrue(am.getItems() instanceof ByteArraySchema);
10051007
assertEquals(am.getItems().getFormat(), "byte");
@@ -1009,7 +1011,6 @@ public void testIssue75() {
10091011
public void testIssue62() {
10101012
OpenAPIV3Parser parser = new OpenAPIV3Parser();
10111013
final OpenAPI openAPI = parser.read("https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/fixtures/v2.0/json/resources/resourceWithLinkedDefinitions.json");
1012-
10131014
assertNotNull(openAPI.getPaths().get("/pets/{petId}").getGet());
10141015
}
10151016

@@ -1028,7 +1029,6 @@ public void testParameterRequired() {
10281029
OpenAPIV3Parser parser = new OpenAPIV3Parser();
10291030
final OpenAPI openAPI = parser.read("src/test/resources/petstore.yaml");
10301031
final List<Parameter> operationParams = openAPI.getPaths().get("/pet/{petId}").getPost().getParameters();
1031-
10321032
final PathParameter pathParameter = (PathParameter) operationParams.get(0);
10331033
Assert.assertTrue(pathParameter.getRequired());
10341034
}
@@ -1037,7 +1037,6 @@ public void testParameterRequired() {
10371037
public void testIssue108() {
10381038
OpenAPIV3Parser parser = new OpenAPIV3Parser();
10391039
final OpenAPI openAPI = parser.read("src/test/resources/issue_108.yaml");
1040-
10411040
assertNotNull(openAPI);
10421041
}
10431042

@@ -1106,15 +1105,13 @@ public void testIssue292WithCSVCollectionFormat() {
11061105
@Test
11071106
public void testIssue255() {
11081107
OpenAPIV3Parser parser = new OpenAPIV3Parser();
1109-
11101108
OpenAPI openAPI = parser.read("objectExample.yaml");
11111109
assertEquals(openAPI.getComponents().getSchemas().get("SamplePayload").getExample().toString(), "[{\"op\":\"replace\",\"path\":\"/s\",\"v\":\"w\"}]");
11121110
}
11131111

11141112
@Test
11151113
public void testIssue286() {
11161114
OpenAPIV3Parser parser = new OpenAPIV3Parser();
1117-
11181115
OpenAPI openAPI = parser.read("issue_286.yaml");
11191116
Schema response = openAPI.getPaths().get("/").getGet().getResponses().get("200").getContent().get("*/*").getSchema();
11201117
assertTrue(response.get$ref() != null);
@@ -1160,15 +1157,10 @@ public void testIssue360() {
11601157

11611158
assertEquals(sbpModel.getType(), "string");
11621159
assertEquals(sbpModel.getFormat(), "uuid");
1163-
11641160
RequestBody bodyParameter = openAPI.getPaths().get("/evenMorePets").getPost().getRequestBody();
1165-
11661161
assertTrue(bodyParameter.getRequired());
1167-
11681162
Schema refModel = bodyParameter.getContent().get("application/json").getSchema();
11691163
assertTrue(refModel.get$ref() != null);
1170-
1171-
11721164
assertEquals(refModel.get$ref(), "#/components/schemas/Pet");
11731165
}
11741166

@@ -1182,10 +1174,7 @@ private OpenAPI doRelativeFileTest(String location) {
11821174
Json.prettyPrint(readResult.getMessages());
11831175
}
11841176
final OpenAPI openAPI = readResult.getOpenAPI();
1185-
1186-
11871177
final PathItem path = openAPI.getPaths().get("/health");
1188-
11891178
assertEquals(path.getClass(), PathItem.class); //we successfully converted the RefPath to a Path
11901179

11911180
final List<Parameter> parameters = path.getParameters();
@@ -1197,7 +1186,6 @@ private OpenAPI doRelativeFileTest(String location) {
11971186
assertParamDetails(operationParams, 0, PathParameter.class, "param3", "path");
11981187
assertParamDetails(operationParams, 1, HeaderParameter.class, "param4", "header");
11991188

1200-
12011189
final Map<String, ApiResponse> responsesMap = operation.getResponses();
12021190

12031191
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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,8 +2304,6 @@ public void readServerObject(JsonNode rootNode) throws Exception {
23042304
Assert.assertEquals(server.get(2).getExtensions().get("x-server").toString(),"server extension");
23052305
Assert.assertEquals(server.get(2).getVariables().get("basePath").getDescription(),"testing overwriting");
23062306
Assert.assertEquals(server.get(2).getVariables().get("basePath").getDefault(),"v2");
2307-
2308-
23092307
}
23102308

23112309
@Test
@@ -2321,6 +2319,7 @@ public void readMissingServerObject() throws Exception {
23212319
final OpenAPI openAPI = result.getOpenAPI();
23222320
Assert.assertNotNull(openAPI);
23232321

2322+
23242323
assertEquals(openAPI.getServers().get(0).getUrl(),"/");
23252324
}
23262325

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 1.0.0
4+
title: API Reuse Common Components
5+
description: Definitions of API components to be referred across different API files
6+
components:
7+
pathitems:
8+
Translation:
9+
put:
10+
tags:
11+
- Translation
12+
summary: Set value of this parameter
13+
description: Provide the lookup table for localized strings
14+
parameters:
15+
- $ref: '#/components/parameters/param_lang'
16+
parameters:
17+
param_lang:
18+
name: lang
19+
in: path
20+
description: language - 2-letter abbreviation
21+
required: true
22+
schema:
23+
type: string
24+
enum:
25+
- en
26+
- se
27+
- dk
28+
- fi
29+
- false
30+
- de
31+
- es
32+
- et
33+
- fr
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
openapi: 3.0.0
2+
info:
3+
version: "1"
4+
title: Admin Web App Services
5+
description: 'Services'
6+
paths:
7+
'/Translation/{lang}':
8+
put:
9+
tags:
10+
- Translation_Settings
11+
summary: Set value of this parameter
12+
description: Provide the lookup table for localized strings
13+
parameters:
14+
- $ref: 'domain.yaml#/components/parameters/param_lang'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 2.0.0
4+
title: Admin Web App Services
5+
description: 'Services API'
6+
paths:
7+
'/Translation/{lang}':
8+
$ref: './domain.yaml#/components/pathitems/Translation'

0 commit comments

Comments
 (0)