Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.swagger.models.parameters.Parameter;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.RefProperty;
import io.swagger.models.refs.RefFormat;
import io.swagger.parser.ResolverCache;
import io.swagger.parser.SwaggerResolver;

Expand Down Expand Up @@ -143,12 +144,12 @@ protected void updateLocalRefs(Parameter param, String pathRef) {

protected void updateLocalRefs(Model model, String pathRef) {
if(model instanceof RefModel) {
RefModel refModel = (RefModel) model;
if(isLocalRef(refModel.get$ref())) {
refModel.set$ref(computeLocalRef(refModel.get$ref(), pathRef));
}/*else if(isLocalRef(refModel.getOriginalRef())) {
refModel.set$ref(computeLocalRef(refModel.getOriginalRef(), pathRef));
}*/
RefModel ref = (RefModel) model;
if(ref.getRefFormat() == RefFormat.INTERNAL) {
ref.set$ref(computeLocalRef(ref.get$ref(), pathRef));
} else if (ref.getRefFormat() == RefFormat.RELATIVE) {
ref.set$ref(computeRelativeRef(ref.get$ref(), pathRef));
}
}
else if(model instanceof ModelImpl) {
// process properties
Expand Down Expand Up @@ -176,22 +177,23 @@ else if(model instanceof ArrayModel) {
protected void updateLocalRefs(Property property, String pathRef) {
if(property instanceof RefProperty) {
RefProperty ref = (RefProperty) property;
if(isLocalRef(ref.get$ref())) {
if(ref.getRefFormat() == RefFormat.INTERNAL) {
ref.set$ref(computeLocalRef(ref.get$ref(), pathRef));
}/*else if(isLocalRef(ref.getOriginalRef())) {
ref.set$ref(computeLocalRef(ref.getOriginalRef(), pathRef));
}*/
}
}

protected boolean isLocalRef(String ref) {
if(ref.startsWith("#")) {
return true;
} else if (ref.getRefFormat() == RefFormat.RELATIVE) {
ref.set$ref(computeRelativeRef(ref.get$ref(), pathRef));
}
}
return false;
}

protected String computeLocalRef(String ref, String prefix) {
return prefix + ref;
}

protected String computeRelativeRef(String ref, String prefix) {
int index = prefix.lastIndexOf('/');
if (index > 1) {
return prefix.substring(0, index + 1) + ref;
}
return ref;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,29 @@ public void testIssue316() {
assertEquals(ref.get$ref(), "#/definitions/Foobar");
}

@Test
public void testIssue1223() {
SwaggerDeserializationResult result = new SwaggerParser().readWithInfo("./src/test/resources/nested-file-references/issue-1223.yaml", null, true);
assertNotNull(result.getSwagger());

Swagger swagger = result.getSwagger();
assertNotNull(swagger.getPath("/events"));
Path path = swagger.getPath("/events");
assertNotNull(path.getGet());
Operation get = path.getGet();
assertEquals(get.getOperationId(), "getEvents");
assertEquals(swagger.getDefinitions().size(),3);
assertEquals(swagger.getDefinitions().get("Foobar").getProperties().size(), 1);
assertEquals(swagger.getDefinitions().get("StatusResponse").getProperties().size(), 1);
assertEquals(swagger.getDefinitions().get("Paging2").getProperties().size(), 2);
Model model = swagger.getDefinitions().get("Paging2");

Property property = model.getProperties().get("foobar");
assertTrue(property instanceof RefProperty);
RefProperty ref = (RefProperty) property;
assertEquals(ref.get$ref(), "#/definitions/Foobar");
}

@Test
public void testIssue323() {
SwaggerDeserializationResult result = new SwaggerParser().readWithInfo("./src/test/resources/nested-file-references/issue-323.yaml", null, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
get:
description: A list of events
operationId: getEvents
responses:
200:
description: OK
schema:
type: object
properties:
paging:
# type: string
$ref: 'paging2.yaml#/Paging2'
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Paging2:
total_items:
type: integer
foobar:
$ref: '../common2/bar.yaml#/Foobar'
$ref: './common2/bar.yaml#/Foobar'
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
swagger: '2.0'
info:
title: Test API
version: '1'
host: example.com
basePath: /api/v1
schemes:
- https
consumes:
- application/json; charset=utf-8
produces:
- application/json; charset=utf-8

paths:
/events:
$ref: './common/eventsWithPagingInSubdir.yaml'

definitions:
StatusResponse:
properties:
http_code:
type: integer