Skip to content

Commit

Permalink
Merge pull request #36 from swagger-api/develop
Browse files Browse the repository at this point in the history
prepare for release
  • Loading branch information
fehguy committed Apr 5, 2015
2 parents 9bbfe7f + 6e1d5ec commit 09747b5
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 99 deletions.
4 changes: 2 additions & 2 deletions modules/swagger-compat-spec-parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
<parent>
<groupId>io.swagger</groupId>
<artifactId>swagger-parser-project</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>io.swagger</groupId>
<artifactId>swagger-compat-spec-parser</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
<packaging>jar</packaging>
<name>swagger-compat-spec-parser</name>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,8 @@ else if(items.getRef() != null) {
else {
if(obj.getRef() != null)
output = new RefProperty(obj.getRef());
else if(type != null)
else if(type != null && !type.equals("void"))
output = new RefProperty(type);
else
output = new RefProperty("void");
}
}

Expand Down
4 changes: 2 additions & 2 deletions modules/swagger-parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
<parent>
<groupId>io.swagger</groupId>
<artifactId>swagger-parser-project</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>io.swagger</groupId>
<artifactId>swagger-parser</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
<packaging>jar</packaging>
<name>swagger-parser</name>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class SwaggerResolver {
Logger LOGGER = LoggerFactory.getLogger(SwaggerResolver.class);
protected Swagger swagger;
protected Map<String, ResolutionContext> resolutionMap = new HashMap<String, ResolutionContext>();
protected Map<String, List<ResolutionContext>> resolutionMap = new HashMap<String, List<ResolutionContext>>();

protected ResolverOptions opts;
public SwaggerResolver(){}
Expand Down Expand Up @@ -57,70 +57,71 @@ public void applyResolutions(List<AuthorizationValue> auths) {
objectList = new ArrayList<Object>();
hostToObjectMap.put(host, objectList);
}
ResolutionContext ctx = resolutionMap.get(path);

Object mapping = ctx.object;
Object target = ctx.parent;
try {
String contents = null;
if(host.startsWith("http"))
contents = new RemoteUrl().urlToString(host, auths);
else
contents = Json.mapper().writeValueAsString(swagger);
JsonNode location = null;
String locationName = null;
if(contents != null) {
location = Json.mapper().readTree(contents);
String[] objectPath = definitionPath.split("/");
for(String objectPathPart : objectPath) {
LOGGER.debug("getting part " + objectPathPart);
if(objectPathPart.length() > 0 && location != null) {
location = location.get(objectPathPart);
locationName = objectPathPart;
}
}
}
if(location != null) {
// convert the node to the proper type
if(mapping instanceof Property) {
Model model = Json.mapper().convertValue(location, Model.class);
if(mapping instanceof RefProperty) {
RefProperty ref = (RefProperty) mapping;
ref.set$ref(locationName);
swagger.addDefinition(locationName, model);
List<ResolutionContext> contexts = resolutionMap.get(path);
for(ResolutionContext ctx : contexts) {
Object mapping = ctx.object;
Object target = ctx.parent;
try {
String contents = null;
if(host.startsWith("http"))
contents = new RemoteUrl().urlToString(host, auths);
else
contents = Json.mapper().writeValueAsString(swagger);
JsonNode location = null;
String locationName = null;
if(contents != null) {
location = Json.mapper().readTree(contents);
String[] objectPath = definitionPath.split("/");
for(String objectPathPart : objectPath) {
LOGGER.debug("getting part " + objectPathPart);
if(objectPathPart.length() > 0 && location != null) {
location = location.get(objectPathPart);
locationName = objectPathPart;
}
}
}
else if(target instanceof Parameter) {
if(mapping instanceof RefModel) {
if(location != null) {
// convert the node to the proper type
if(mapping instanceof Property) {
Model model = Json.mapper().convertValue(location, Model.class);
RefModel ref = (RefModel) mapping;
ref.set$ref(locationName);
swagger.addDefinition(locationName, model);
if(mapping instanceof RefProperty) {
RefProperty ref = (RefProperty) mapping;
ref.set$ref(locationName);
swagger.addDefinition(locationName, model);
}
}
}
else if(target instanceof Operation) {

// get the operation position
Operation operation = (Operation) target;
int position = 0;
for(Parameter param : operation.getParameters()) {

if(param instanceof RefParameter) {
RefParameter ref = (RefParameter) param;
if(ref.getSimpleRef().equals(locationName)) {
// found a match!
Parameter remoteParam = Json.mapper().convertValue(location, Parameter.class);
operation.getParameters().set(position, remoteParam);
else if(target instanceof Parameter) {
if(mapping instanceof RefModel) {
Model model = Json.mapper().convertValue(location, Model.class);
RefModel ref = (RefModel) mapping;
ref.set$ref(locationName);
swagger.addDefinition(locationName, model);
}
}
else if(target instanceof Operation) {

// get the operation position
Operation operation = (Operation) target;
int position = 0;
for(Parameter param : operation.getParameters()) {

if(param instanceof RefParameter) {
RefParameter ref = (RefParameter) param;
if(ref.getSimpleRef().equals(locationName)) {
// found a match!
Parameter remoteParam = Json.mapper().convertValue(location, Parameter.class);
operation.getParameters().set(position, remoteParam);
}
}
position += 1;
}
position += 1;
}
}
}
}
catch(Exception e) {
// failed to get it
e.printStackTrace();
catch(Exception e) {
// failed to get it
e.printStackTrace();
}
}
}
}
Expand All @@ -140,16 +141,29 @@ public void detectOperationRefs() {
BodyParameter bp = (BodyParameter) parameter;
if(bp.getSchema() != null && bp.getSchema() instanceof RefModel) {
RefModel ref = (RefModel)bp.getSchema();
if(ref.get$ref().startsWith("http")) {
LOGGER.debug("added reference to " + ref.get$ref());
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, bp, "ref"));
String key = ref.get$ref();
if(key.startsWith("http")) {
LOGGER.debug("added reference to " + key);
List<ResolutionContext> m = resolutionMap.get(key);
if(m == null) {
m = new ArrayList<ResolutionContext>();
}
m.add(new ResolutionContext(ref, bp, "ref"));
resolutionMap.put(key, m);
}
}
}
else if(parameter instanceof RefParameter) {
RefParameter ref = (RefParameter) parameter;
LOGGER.debug("added reference to " + ref.get$ref());
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, operation, "inline"));
String key = ref.get$ref();
LOGGER.debug("added reference to " + ref);

List<ResolutionContext> m = resolutionMap.get(key);
if(m == null) {
m = new ArrayList<ResolutionContext>();
}
m.add(new ResolutionContext(ref, operation, "inline"));
resolutionMap.put(key, m);
}
}
}
Expand All @@ -160,8 +174,15 @@ else if(parameter instanceof RefParameter) {
Property schema = response.getSchema();
if(schema instanceof RefProperty) {
RefProperty ref = (RefProperty) schema;
if(ref.get$ref() != null && ref.get$ref().startsWith("http")) {
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, response, "ref"));
String key = ref.get$ref();

if(key != null && key.startsWith("http")) {
List<ResolutionContext> m = resolutionMap.get(key);
if(m == null) {
m = new ArrayList<ResolutionContext>();
}
m.add(new ResolutionContext(ref, response, "ref"));
resolutionMap.put(key, m);
}
}
}
Expand All @@ -179,18 +200,30 @@ public void detectModelRefs() {
Model model = models.get(modelName);
if(model instanceof RefModel) {
RefModel ref = (RefModel) model;
if(ref.get$ref() != null && ref.get$ref().startsWith("http")) {
LOGGER.debug("added reference to " + ref.get$ref());
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, swagger.getDefinitions(), "ref"));
String key = ref.get$ref();
if(key != null && key.startsWith("http")) {
LOGGER.debug("added reference to " + key);
List<ResolutionContext> m = resolutionMap.get(key);
if(m == null) {
m = new ArrayList<ResolutionContext>();
}
m.add(new ResolutionContext(ref, swagger.getDefinitions(), "ref"));
resolutionMap.put(key, m);
}
}
else if(model instanceof ArrayModel) {
ArrayModel arrayModel = (ArrayModel) model;
if(arrayModel.getItems() != null && arrayModel.getItems() instanceof RefProperty) {
RefProperty ref = (RefProperty)arrayModel.getItems();
if(ref.get$ref() != null && ref.get$ref().startsWith("http")) {
LOGGER.debug("added reference to " + ref.get$ref());
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, swagger.getDefinitions(), "ref"));
String key = ref.get$ref();
if(key != null && key.startsWith("http")) {
LOGGER.debug("added reference to " + key);
List<ResolutionContext> m = resolutionMap.get(key);
if(m == null) {
m = new ArrayList<ResolutionContext>();
}
m.add(new ResolutionContext(ref, swagger.getDefinitions(), "ref"));
resolutionMap.put(key, m);
}
}
}
Expand All @@ -202,28 +235,46 @@ else if(model instanceof ModelImpl) {
Property property = properties.get(propertyName);
if(property instanceof RefProperty) {
RefProperty ref = (RefProperty)property;
if(ref.get$ref() != null && ref.get$ref().startsWith("http")) {
LOGGER.debug("added reference to " + ref.get$ref());
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, impl, "ref"));
String key = ref.get$ref();
if(key != null && key.startsWith("http")) {
LOGGER.debug("added reference to " + key);
List<ResolutionContext> m = resolutionMap.get(key);
if(m == null) {
m = new ArrayList<ResolutionContext>();
}
m.add(new ResolutionContext(ref, impl, "ref"));
resolutionMap.put(key, m);
}
}
else if(property instanceof ArrayProperty) {
ArrayProperty arrayProperty = (ArrayProperty) property;
if(arrayProperty.getItems() != null && arrayProperty.getItems() instanceof RefProperty) {
RefProperty ref = (RefProperty)arrayProperty.getItems();
if(ref.get$ref() != null && ref.get$ref().startsWith("http")) {
LOGGER.debug("added reference to " + ref.get$ref());
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, arrayProperty, "ref"));
String key = ref.get$ref();
if(key != null && key.startsWith("http")) {
LOGGER.debug("added reference to " + key);
List<ResolutionContext> m = resolutionMap.get(key);
if(m == null) {
m = new ArrayList<ResolutionContext>();
}
m.add(new ResolutionContext(ref, arrayProperty, "ref"));
resolutionMap.put(key, m);
}
}
}
else if(property instanceof MapProperty) {
MapProperty mp = (MapProperty) property;
if(mp.getAdditionalProperties() != null && mp.getAdditionalProperties() instanceof RefProperty) {
RefProperty ref = (RefProperty)mp.getAdditionalProperties();
if(ref.get$ref() != null && ref.get$ref().startsWith("http")) {
LOGGER.debug("added reference to " + ref.get$ref());
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, mp, "ref"));
String key = ref.get$ref();
if(key != null && key.startsWith("http")) {
LOGGER.debug("added reference to " + key);
List<ResolutionContext> m = resolutionMap.get(key);
if(m == null) {
m = new ArrayList<ResolutionContext>();
}
m.add(new ResolutionContext(ref, mp, "ref"));
resolutionMap.put(key, m);
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions modules/swagger-parser/src/test/resources/issue_16.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
swagger: "2.0"
info:
version: 0.0.0
title: Simple API
paths:
/:
get:
responses:
"200":
description: OK
schema:
$ref: "#/definitions/InternshipResultModel"
definitions:
InternshipResultModel:
properties:
Review:
$ref: "#/definitions/ReviewModel"
ReviewModel:
properties:
Rating:
type: integer
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ class SwaggerReaderTest extends FlatSpec with Matchers {
it should "read the issue 16 resource" in {
val parser = new SwaggerParser()
val swagger = parser.read("./src/test/resources/issue_16.yaml")
Json.prettyPrint(swagger)
// Json.prettyPrint(swagger)
}
}
Loading

0 comments on commit 09747b5

Please sign in to comment.