Skip to content
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

Fixing small issues #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 19 additions & 1 deletion src/main/java/io/specto/swagger/JsonNodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

import io.swagger.models.ArrayModel;
import io.swagger.models.Model;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.MapProperty;
Expand All @@ -28,7 +30,20 @@ public JsonNode generate(final Property baseProperty) {
if (baseProperty.getType() != null && baseProperty.getType().equals("ref")) {
RefProperty refProperty = (RefProperty) baseProperty;
Model model = definitions.get(refProperty.getSimpleRef());
return generateJsonObject(model.getProperties());
if(model.getExample() != null) {
ObjectNode node = (ObjectNode) model.getExample();
return node.without("");
} else {
if(model.getClass().getName() == "io.swagger.models.ArrayModel") {
ArrayModel arrayModel = (ArrayModel) model;
Property property = arrayModel.getItems();
JsonNode innerNode = generate(property);
ArrayNode outerNode = new ArrayNode(factory);
outerNode.add(innerNode);
return outerNode;
}
return generateJsonObject(model.getProperties());
}
}

if (baseProperty.getType() != null && baseProperty.getType().equals("object") && !(baseProperty instanceof MapProperty)) {
Expand All @@ -53,6 +68,9 @@ private JsonNode generateJsonArray(ArrayProperty arrayProperty) {
private JsonNode generateJsonObject(Map<String, Property> properties) {

ObjectNode root = factory.objectNode();
if(properties == null) {
return root;
}

for (Map.Entry<String, Property> propertyEntry : properties.entrySet()) {
root.set(propertyEntry.getKey(), generate(propertyEntry.getValue()));
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/io/specto/swagger/SwaggerConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import io.swagger.models.properties.RefProperty;
import io.swagger.parser.SwaggerParser;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;

import java.util.List;
import java.util.Map;
Expand All @@ -29,15 +32,21 @@

import static java.util.stream.Collectors.toList;

import java.util.Arrays;

public class SwaggerConverter {

private static final String EMPTY_REQUEST_BODY = "";
private final ObjectMapper mapper = new ObjectMapper();
private final PropertyValueGenerator propertyValueGenerator = new PropertyValueGenerator();

public PayloadView convert(String swaggerFile) {

Logger root = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
Level level = root.getLevel();
root.setLevel(Level.WARN);
Swagger swagger = new SwaggerParser().read(swaggerFile);

root.setLevel(level);
final List<RequestResponsePairView> payloadList = swagger.getPaths().entrySet()
.stream()
.flatMap(e -> convertOperations(swagger, e))
Expand All @@ -59,7 +68,6 @@ private RequestResponsePairView convertOperation(final String urlPath, final Swa
.withMethod(operationEntry.getKey().name())
.withDestination(swagger.getHost())
.withQuery(generateQueryParameters(operationEntry.getValue()))
.withBody(generateRequestBody(operationEntry.getValue(), swagger.getDefinitions()))
.build();

final Map<String, Response> responses = operationEntry.getValue().getResponses();
Expand All @@ -77,6 +85,8 @@ private RequestResponsePairView convertOperation(final String urlPath, final Swa
responseBuilder.withBody(tryAndConvertToJsonBody(swagger.getDefinitions(), response.getValue().getSchema()));
}

responseBuilder.addHeader("Content-Type", Arrays.asList("application/json"));

return new RequestResponsePairView(requestDetails, responseBuilder.build());
}

Expand All @@ -94,7 +104,7 @@ private String generateQueryParameters(final Operation operation) {
.filter(s -> !s.isEmpty())
.collect(Collectors.joining("&"));

return query.isEmpty() ? "" : "?" + query;
return query.isEmpty() ? "" : query;
}

private String generateRequestBody(final Operation operation, final Map<String, Model> definitions) {
Expand Down
16 changes: 2 additions & 14 deletions src/main/java/io/specto/swagger/hoverfly/RequestDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ public class RequestDetails {
private final String destination;
private final String scheme;
private final String query;
private final String body;
private final Map<String, List<String>> headers;

private RequestDetails(String requestType, String path, String method, String destination, String scheme, String query, String body, Map<String, List<String>> headers) {
private RequestDetails(String requestType, String path, String method, String destination, String scheme, String query, Map<String, List<String>> headers) {
this.requestType = requestType;
this.path = path;
this.method = method;
this.destination = destination;
this.scheme = scheme;
this.query = query;
this.body = body;
this.headers = headers;
}

Expand Down Expand Up @@ -49,10 +47,6 @@ public String getQuery() {
return query;
}

public String getBody() {
return body;
}

public Map<String, List<String>> getHeaders() {
return headers;
}
Expand All @@ -76,7 +70,6 @@ public static class Builder {
private String path = "";
private String method = "";
private String destination;
private String body = "";
private String query = "";

public Builder withPath(final String path) {
Expand All @@ -98,13 +91,8 @@ public Builder withDestination(final String destination) {
return this;
}

public Builder withBody(final String body) {
this.body = body;
return this;
}

public RequestDetails build() {
return new RequestDetails(requestType, path, method, destination, SCHEME, query, body, Collections.emptyMap());
return new RequestDetails(requestType, path, method, destination, SCHEME, query, Collections.emptyMap());
}

public Builder withQuery(final String query) {
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/io/specto/swagger/hoverfly/ResponseDetails.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.specto.swagger.hoverfly;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -41,14 +42,25 @@ public static class Builder {

private String body = "";
private int status = 200;
Map<String, List<String>> headers = new LinkedHashMap<String, List<String>>();

public Builder withBody(final String body) {
this.body = body;
return this;
}

public Builder withHeaders(final Map<String, List<String>> headers) {
this.headers = headers;
return this;
}

public Builder addHeader(final String key, List<String> values) {
this.headers.put(key, values);
return this;
}

public ResponseDetails build() {
return new ResponseDetails(status, body, false, Collections.emptyMap());
return new ResponseDetails(status, body, false, headers);
}

public Builder withStatus(final int status) {
Expand Down
Loading