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 @@ -47,6 +47,7 @@
import io.swagger.models.resourcelisting.OAuth2Authorization;
import io.swagger.models.resourcelisting.ResourceListing;
import io.swagger.parser.util.RemoteUrl;
import io.swagger.parser.util.SwaggerDeserializationResult;
import io.swagger.report.MessageBuilder;
import io.swagger.transform.migrate.ApiDeclarationMigrator;
import io.swagger.transform.migrate.ResourceListingMigrator;
Expand All @@ -69,6 +70,18 @@
public class SwaggerCompatConverter implements SwaggerParserExtension {
static Logger LOGGER = LoggerFactory.getLogger(SwaggerCompatConverter.class);

@Override
public SwaggerDeserializationResult readWithInfo(JsonNode node) {
// not implemented
return null;
}

@Override
public SwaggerDeserializationResult readWithInfo(String location, List<AuthorizationValue> auths) {
// not implemented
return null;
}

@Override
public Swagger read(JsonNode node) throws IOException {
// TODO
Expand Down Expand Up @@ -230,11 +243,9 @@ public Parameter convertParameter(io.swagger.models.apideclaration.Parameter par
String format = param.getFormat() == null ? null : param.getFormat().toString();

if (null == type) {
new Throwable("").printStackTrace();
LOGGER.warn("Empty type in Param: " + param);
}
if (null == format) {
new Throwable("").printStackTrace();
LOGGER.warn("Empty format in Param: " + param);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@ public void readResourceListing() throws Exception {
List<ApiListingReference> apis = resourceListing.getApis();
for (ApiListingReference ref : apis) {
String path = ref.getPath();

if (path.startsWith("http")) {
// absolute listing, do some magic

} else {
// it is possible that the resource listing URL has a trailing slash and the path has a leading one
path = (resourceListingUri + path).replace("//", "/");
}

System.out.println("found path " + path);
}
}
Expand Down
15 changes: 15 additions & 0 deletions modules/swagger-parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>test-single</id>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<groups>single</groups>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import io.swagger.models.auth.AuthorizationValue;
import io.swagger.parser.util.ClasspathHelper;
import io.swagger.parser.util.RemoteUrl;
import io.swagger.parser.util.SwaggerDeserializationResult;
import io.swagger.parser.util.SwaggerDeserializer;
import io.swagger.util.Json;
import io.swagger.util.Yaml;
import org.apache.commons.io.FileUtils;
Expand All @@ -25,6 +27,43 @@ public class Swagger20Parser implements SwaggerParserExtension {

private static final Logger LOGGER = LoggerFactory.getLogger(Swagger20Parser.class);

@Override
public SwaggerDeserializationResult readWithInfo(JsonNode node) {
SwaggerDeserializer ser = new SwaggerDeserializer();
return ser.deserialize(node);
}

@Override
public SwaggerDeserializationResult readWithInfo(String location, List<AuthorizationValue> auths) {
String data;

try {
if (location.toLowerCase().startsWith("http")) {
data = RemoteUrl.urlToString(location, auths);
} else {
final Path path = Paths.get(location);
if (Files.exists(path)) {
data = FileUtils.readFileToString(path.toFile(), "UTF-8");
} else {
data = ClasspathHelper.loadFileFromClasspath(location);
}
}
ObjectMapper mapper;
if (data.trim().startsWith("{")) {
mapper = Json.mapper();
} else {
mapper = Yaml.mapper();
}
JsonNode rootNode = mapper.readTree(data);
return readWithInfo(rootNode);
}
catch (Exception e) {
SwaggerDeserializationResult output = new SwaggerDeserializationResult();
output.message("unable to read location `" + location + "`");
return output;
}
}

@Override
public Swagger read(String location, List<AuthorizationValue> auths) throws IOException {
System.out.println("reading from " + location);
Expand All @@ -45,7 +84,6 @@ public Swagger read(String location, List<AuthorizationValue> auths) throws IOEx

return convertToSwagger(data);
} catch (Exception e) {
System.out.println(e.getMessage());
if (System.getProperty("debugParser") != null) {
e.printStackTrace();
}
Expand All @@ -71,7 +109,9 @@ private Swagger convertToSwagger(String data) throws IOException {
if (swaggerNode == null) {
return null;
} else {
Swagger convertValue = mapper.convertValue(rootNode, Swagger.class);
SwaggerDeserializationResult result = new SwaggerDeserializer().deserialize(rootNode);

Swagger convertValue = result.getSwagger();
if (System.getProperty("debugParser") != null) {
LOGGER.info("\n\nSwagger Tree convertValue : \n"
+ ReflectionToStringBuilder.toString(convertValue, ToStringStyle.MULTI_LINE_STYLE) + "\n\n");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package io.swagger.parser;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.models.Swagger;
import io.swagger.models.auth.AuthorizationValue;
import io.swagger.parser.util.SwaggerDeserializationResult;
import io.swagger.util.Json;
import io.swagger.util.Yaml;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -11,6 +15,33 @@
import java.util.ServiceLoader;

public class SwaggerParser {

public SwaggerDeserializationResult readWithInfo(String location, List<AuthorizationValue> auths, boolean resolve) {
if (location == null) {
return null;
}

List<SwaggerParserExtension> parserExtensions = getExtensions();
SwaggerDeserializationResult output;

if(auths == null) {
auths = new ArrayList<AuthorizationValue>();
}

output = new Swagger20Parser().readWithInfo(location, auths);
if (output != null) {
output.setSwagger(new SwaggerResolver(output.getSwagger(), auths, location).resolve());
return output;
}
for (SwaggerParserExtension extension : parserExtensions) {
output = extension.readWithInfo(location, auths);
if (output != null) {
return output;
}
}
return null;
}

public Swagger read(String location) {
return read(location, null, true);
}
Expand Down Expand Up @@ -47,6 +78,31 @@ public Swagger read(String location, List<AuthorizationValue> auths, boolean res
return null;
}

public SwaggerDeserializationResult readWithInfo(String swaggerAsString) {
if(swaggerAsString == null) {
return new SwaggerDeserializationResult().message("empty or null swagger supplied");
}
ObjectMapper mapper = null;
if(swaggerAsString.trim().startsWith("{")) {
mapper = Json.mapper();
}
else {
mapper = Yaml.mapper();
}
try {
JsonNode node = mapper.readTree(swaggerAsString);

SwaggerDeserializationResult result = new Swagger20Parser().readWithInfo(node);
if (result != null) {
result.setSwagger(new SwaggerResolver(result.getSwagger(), new ArrayList<AuthorizationValue>(), null).resolve());
}
return new Swagger20Parser().readWithInfo(node);
}
catch (Exception e) {
return new SwaggerDeserializationResult().message("malformed or unreadable swagger supplied");
}
}

public Swagger parse(String swaggerAsString) {
return parse(swaggerAsString, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.models.Swagger;
import io.swagger.models.auth.AuthorizationValue;
import io.swagger.parser.util.SwaggerDeserializationResult;

import java.io.IOException;
import java.util.List;

public interface SwaggerParserExtension {
SwaggerDeserializationResult readWithInfo(JsonNode node);
SwaggerDeserializationResult readWithInfo(String location, List<AuthorizationValue> auths);
Swagger read(String location, List<AuthorizationValue> auths) throws IOException;

Swagger read(JsonNode node) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.swagger.parser.util;

import io.swagger.models.Swagger;

import java.util.ArrayList;
import java.util.List;

public class SwaggerDeserializationResult {
private Swagger swagger;
private List<String> messages;

public Swagger getSwagger() {
return swagger;
}

public List<String> getMessages() {
return messages;
}

public void setMessages(List<String> messages) {
this.messages = messages;
}

public void setSwagger(Swagger swagger) {
this.swagger = swagger;
}

public SwaggerDeserializationResult message(String message) {
if(messages == null) {
messages = new ArrayList<String>();
}
messages.add(message);
return this;
}
}
Loading