Skip to content

Commit 918c0cb

Browse files
authored
Merge pull request #881 from anicolas/issue-783-fix
Fix issue #783: update internal refs in external files
2 parents b4a9ca3 + 3486c13 commit 918c0cb

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ private void processRefSchema(Schema subRef, String externalFile) {
674674
RefFormat format = computeRefFormat(subRef.get$ref());
675675

676676
if (!isAnExternalRefFormat(format)) {
677-
processRefToExternalSchema(externalFile + subRef.get$ref(), RefFormat.RELATIVE);
677+
subRef.set$ref(processRefToExternalSchema(externalFile + subRef.get$ref(), RefFormat.RELATIVE));
678678
return;
679679
}
680680
String $ref = subRef.get$ref();
@@ -723,4 +723,4 @@ else if("".equals(uri.getPath()) && !fragment.startsWith("/")) {
723723
}
724724

725725

726-
}
726+
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,21 @@ public void testSettingsAddParametersToEachOperationDisabled() {
11021102
assertEquals(qp.getName(), "page");
11031103
}
11041104

1105+
@Test(description = "update internal references of external files")
1106+
public void testUpdateInternalReferencesOfExternalFiles() {
1107+
ParseOptions options = new ParseOptions();
1108+
options.setResolve(true);
1109+
1110+
OpenAPI openAPI = new OpenAPIV3Parser().read("internal-references-in-external-files/main.yaml", null, options);
1111+
1112+
ComposedSchema commonSchema = (ComposedSchema) openAPI.getComponents().getSchemas().get("common");
1113+
1114+
assertEquals(commonSchema.getAllOf().get(0).get$ref(), "#/components/schemas/core");
1115+
assertEquals(((Schema) commonSchema.getAllOf().get(1).getProperties().get("direct")).get$ref(), "#/components/schemas/core");
1116+
assertEquals(((ArraySchema) commonSchema.getAllOf().get(1).getProperties().get("referenced")).getItems().get$ref(), "#/components/schemas/core");
1117+
Schema coreSchema = openAPI.getComponents().getSchemas().get("core");
1118+
assertEquals(((Schema) coreSchema.getProperties().get("inner")).get$ref(), "#/components/schemas/innerCore");
1119+
}
11051120

11061121
public String replacePort(String url){
11071122
String pathFile = url.replace("${dynamicPort}", String.valueOf(this.serverPort));
@@ -1112,4 +1127,4 @@ private static int getDynamicPort() {
11121127
return new Random().ints(50000, 60000).findFirst().getAsInt();
11131128
}
11141129

1115-
}
1130+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
common:
2+
allOf:
3+
- $ref: "#/core"
4+
- type: object
5+
properties:
6+
attribute:
7+
type: string
8+
direct:
9+
$ref: "#/core"
10+
referenced:
11+
type: array
12+
items:
13+
$ref: "#/core"
14+
15+
core:
16+
type: object
17+
properties:
18+
coreAttribute:
19+
type: string
20+
inner:
21+
$ref: "#/innerCore"
22+
23+
innerCore:
24+
type: object
25+
properties:
26+
innerCoreAttribute:
27+
type: string
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 0.0.1
4+
title: API
5+
paths:
6+
/:
7+
post:
8+
summary: Create
9+
requestBody:
10+
content:
11+
application/json:
12+
schema:
13+
$ref: '#/components/schemas/create'
14+
responses:
15+
'200':
16+
description: Successful response
17+
components:
18+
schemas:
19+
create:
20+
type: object
21+
properties:
22+
attribute:
23+
type: string
24+
direct:
25+
$ref: "./external.yaml#/common"
26+
referenced:
27+
type: array
28+
items:
29+
$ref: "./external.yaml#/common"

0 commit comments

Comments
 (0)