Skip to content

Fix issue with relative file references #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 25, 2016
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
1 change: 1 addition & 0 deletions modules/swagger-parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<groups>single</groups>
<workingDirectory>.</workingDirectory>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,16 @@ public Property property(ObjectNode node, String location, ParseResult result) {
}
}
}

// work-around for https://github.com/swagger-api/swagger-core/issues/1977
if(node.get("$ref") != null && node.get("$ref").isTextual()) {
// check if it's a relative ref
String refString = node.get("$ref").textValue();
if(refString.indexOf("/") == -1 && refString.indexOf(".") > 0) {
refString = "./" + refString;
node.put("$ref", refString);
}
}
return Json.mapper().convertValue(node, Property.class);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.swagger.parser;

import io.swagger.models.Swagger;
import io.swagger.parser.util.SwaggerDeserializationResult;
import org.testng.annotations.Test;

import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

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

Swagger swagger = result.getSwagger();

assertTrue(swagger.getDefinitions().size() == 3);
// resolved from `$ref: './book.yaml'`
assertNotNull(swagger.getDefinitions().get("Inventory"));
// resolved from `$ref: 'book.yaml'`
assertNotNull(swagger.getDefinitions().get("Orders"));

// copied from `./book.yaml`
assertNotNull(swagger.getDefinitions().get("book"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
description: A book entity
properties:
name:
type: string
title:
type: string
pages:
type: integer
format: int32
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
definitions:
Category:
properties:
name:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
swagger: '2.0'
definitions:
Animal:
required:
- id
properties:
id:
type: string
categories:
$ref: './components/entities.yaml#/definitions/Category'
Inventory:
properties:
book:
$ref: './book.yaml'
Orders:
properties:
book:
$ref: 'book.yaml'