diff --git a/src/main/java/io/specto/swagger/JsonNodeGenerator.java b/src/main/java/io/specto/swagger/JsonNodeGenerator.java index 7737d2f..cc87c7b 100644 --- a/src/main/java/io/specto/swagger/JsonNodeGenerator.java +++ b/src/main/java/io/specto/swagger/JsonNodeGenerator.java @@ -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; @@ -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)) { @@ -53,6 +68,9 @@ private JsonNode generateJsonArray(ArrayProperty arrayProperty) { private JsonNode generateJsonObject(Map properties) { ObjectNode root = factory.objectNode(); + if(properties == null) { + return root; + } for (Map.Entry propertyEntry : properties.entrySet()) { root.set(propertyEntry.getKey(), generate(propertyEntry.getValue())); diff --git a/src/main/java/io/specto/swagger/SwaggerConverter.java b/src/main/java/io/specto/swagger/SwaggerConverter.java index 6d16bee..9ae57b6 100644 --- a/src/main/java/io/specto/swagger/SwaggerConverter.java +++ b/src/main/java/io/specto/swagger/SwaggerConverter.java @@ -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; @@ -29,6 +32,8 @@ import static java.util.stream.Collectors.toList; +import java.util.Arrays; + public class SwaggerConverter { private static final String EMPTY_REQUEST_BODY = ""; @@ -36,8 +41,12 @@ public class SwaggerConverter { 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 payloadList = swagger.getPaths().entrySet() .stream() .flatMap(e -> convertOperations(swagger, e)) @@ -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 responses = operationEntry.getValue().getResponses(); @@ -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()); } @@ -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 definitions) { diff --git a/src/main/java/io/specto/swagger/hoverfly/RequestDetails.java b/src/main/java/io/specto/swagger/hoverfly/RequestDetails.java index 0104bf9..c1dfe35 100644 --- a/src/main/java/io/specto/swagger/hoverfly/RequestDetails.java +++ b/src/main/java/io/specto/swagger/hoverfly/RequestDetails.java @@ -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> headers; - private RequestDetails(String requestType, String path, String method, String destination, String scheme, String query, String body, Map> headers) { + private RequestDetails(String requestType, String path, String method, String destination, String scheme, String query, Map> 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; } @@ -49,10 +47,6 @@ public String getQuery() { return query; } - public String getBody() { - return body; - } - public Map> getHeaders() { return headers; } @@ -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) { @@ -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) { diff --git a/src/main/java/io/specto/swagger/hoverfly/ResponseDetails.java b/src/main/java/io/specto/swagger/hoverfly/ResponseDetails.java index 8b20368..7177b82 100644 --- a/src/main/java/io/specto/swagger/hoverfly/ResponseDetails.java +++ b/src/main/java/io/specto/swagger/hoverfly/ResponseDetails.java @@ -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; @@ -41,14 +42,25 @@ public static class Builder { private String body = ""; private int status = 200; + Map> headers = new LinkedHashMap>(); public Builder withBody(final String body) { this.body = body; return this; } + public Builder withHeaders(final Map> headers) { + this.headers = headers; + return this; + } + + public Builder addHeader(final String key, List 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) { diff --git a/src/test/resources/petstore-output.json b/src/test/resources/petstore-output.json index 83365f9..a6038b6 100644 --- a/src/test/resources/petstore-output.json +++ b/src/test/resources/petstore-output.json @@ -8,14 +8,13 @@ "destination" : "petstore.swagger.io", "scheme" : "http", "query" : "", - "body" : "{\"id\":\"0\",\"category\":{\"id\":\"0\",\"name\":\"string\"},\"name\":\"doggie\",\"photoUrls\":[\"string\"],\"tags\":[{\"id\":\"0\",\"name\":\"string\"}],\"status\":\"available\"}", "headers" : { } }, "response" : { "status" : 400, "body" : "", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -25,14 +24,13 @@ "destination" : "petstore.swagger.io", "scheme" : "http", "query" : "", - "body" : "{\"id\":\"0\",\"category\":{\"id\":\"0\",\"name\":\"string\"},\"name\":\"doggie\",\"photoUrls\":[\"string\"],\"tags\":[{\"id\":\"0\",\"name\":\"string\"}],\"status\":\"available\"}", "headers" : { } }, "response" : { "status" : 405, "body" : "", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -41,15 +39,14 @@ "method" : "GET", "destination" : "petstore.swagger.io", "scheme" : "http", - "query" : "?status=available", - "body" : "", + "query" : "status=available", "headers" : { } }, "response" : { "status" : 200, "body" : "[{\"id\":\"0\",\"category\":{\"id\":\"0\",\"name\":\"string\"},\"name\":\"doggie\",\"photoUrls\":[\"string\"],\"tags\":[{\"id\":\"0\",\"name\":\"string\"}],\"status\":\"available\"}]", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -58,15 +55,14 @@ "method" : "GET", "destination" : "petstore.swagger.io", "scheme" : "http", - "query" : "?tags=string", - "body" : "", + "query" : "tags=string", "headers" : { } }, "response" : { "status" : 200, "body" : "[{\"id\":\"0\",\"category\":{\"id\":\"0\",\"name\":\"string\"},\"name\":\"doggie\",\"photoUrls\":[\"string\"],\"tags\":[{\"id\":\"0\",\"name\":\"string\"}],\"status\":\"available\"}]", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -76,14 +72,13 @@ "destination" : "petstore.swagger.io", "scheme" : "http", "query" : "", - "body" : "", "headers" : { } }, "response" : { "status" : 405, "body" : "", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -93,14 +88,13 @@ "destination" : "petstore.swagger.io", "scheme" : "http", "query" : "", - "body" : "", "headers" : { } }, "response" : { "status" : 400, "body" : "", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -110,14 +104,13 @@ "destination" : "petstore.swagger.io", "scheme" : "http", "query" : "", - "body" : "", "headers" : { } }, "response" : { "status" : 200, "body" : "{\"id\":\"0\",\"category\":{\"id\":\"0\",\"name\":\"string\"},\"name\":\"doggie\",\"photoUrls\":[\"string\"],\"tags\":[{\"id\":\"0\",\"name\":\"string\"}],\"status\":\"available\"}", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -127,14 +120,13 @@ "destination" : "petstore.swagger.io", "scheme" : "http", "query" : "", - "body" : "", "headers" : { } }, "response" : { "status" : 200, "body" : "{\"code\":\"0\",\"type\":\"string\",\"message\":\"string\"}", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -144,14 +136,13 @@ "destination" : "petstore.swagger.io", "scheme" : "http", "query" : "", - "body" : "", "headers" : { } }, "response" : { "status" : 200, "body" : "\"{}\"", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -161,14 +152,13 @@ "destination" : "petstore.swagger.io", "scheme" : "http", "query" : "", - "body" : "{\"id\":\"0\",\"petId\":\"0\",\"quantity\":\"0\",\"shipDate\":\"\",\"status\":\"placed\",\"complete\":\"\"}", "headers" : { } }, "response" : { "status" : 200, "body" : "{\"id\":\"0\",\"petId\":\"0\",\"quantity\":\"0\",\"shipDate\":\"\",\"status\":\"placed\",\"complete\":\"\"}", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -178,14 +168,13 @@ "destination" : "petstore.swagger.io", "scheme" : "http", "query" : "", - "body" : "", "headers" : { } }, "response" : { "status" : 400, "body" : "", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -195,14 +184,13 @@ "destination" : "petstore.swagger.io", "scheme" : "http", "query" : "", - "body" : "", "headers" : { } }, "response" : { "status" : 200, "body" : "{\"id\":\"0\",\"petId\":\"0\",\"quantity\":\"0\",\"shipDate\":\"\",\"status\":\"placed\",\"complete\":\"\"}", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -212,14 +200,13 @@ "destination" : "petstore.swagger.io", "scheme" : "http", "query" : "", - "body" : "{\"id\":\"0\",\"username\":\"string\",\"firstName\":\"string\",\"lastName\":\"string\",\"email\":\"string\",\"password\":\"string\",\"phone\":\"string\",\"userStatus\":\"0\"}", "headers" : { } }, "response" : { "status" : 200, "body" : "", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -229,14 +216,13 @@ "destination" : "petstore.swagger.io", "scheme" : "http", "query" : "", - "body" : "", "headers" : { } }, "response" : { "status" : 200, "body" : "", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -246,14 +232,13 @@ "destination" : "petstore.swagger.io", "scheme" : "http", "query" : "", - "body" : "", "headers" : { } }, "response" : { "status" : 200, "body" : "", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -263,14 +248,13 @@ "destination" : "petstore.swagger.io", "scheme" : "http", "query" : "", - "body" : "", "headers" : { } }, "response" : { "status" : 200, "body" : "\"string\"", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -280,14 +264,13 @@ "destination" : "petstore.swagger.io", "scheme" : "http", "query" : "", - "body" : "", "headers" : { } }, "response" : { "status" : 200, "body" : "", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -297,14 +280,13 @@ "destination" : "petstore.swagger.io", "scheme" : "http", "query" : "", - "body" : "{\"id\":\"0\",\"username\":\"string\",\"firstName\":\"string\",\"lastName\":\"string\",\"email\":\"string\",\"password\":\"string\",\"phone\":\"string\",\"userStatus\":\"0\"}", "headers" : { } }, "response" : { "status" : 400, "body" : "", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -314,14 +296,13 @@ "destination" : "petstore.swagger.io", "scheme" : "http", "query" : "", - "body" : "", "headers" : { } }, "response" : { "status" : 400, "body" : "", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } }, { "request" : { @@ -331,14 +312,13 @@ "destination" : "petstore.swagger.io", "scheme" : "http", "query" : "", - "body" : "", "headers" : { } }, "response" : { "status" : 200, "body" : "{\"id\":\"0\",\"username\":\"string\",\"firstName\":\"string\",\"lastName\":\"string\",\"email\":\"string\",\"password\":\"string\",\"phone\":\"string\",\"userStatus\":\"0\"}", "encodedBody" : false, - "headers" : { } + "headers": { "Content-Type" : [ "application/json" ]} } } ] }, diff --git a/src/test/resources/simple-swagger-output.json b/src/test/resources/simple-swagger-output.json index f9d7dd5..a4fb3a5 100644 --- a/src/test/resources/simple-swagger-output.json +++ b/src/test/resources/simple-swagger-output.json @@ -9,13 +9,12 @@ "destination": "specto.io", "scheme": "http", "query": "", - "body": "", "headers": {} }, "response": { "status": 200, "encodedBody": false, - "headers": {} + "headers": { "Content-Type" : [ "application/json" ]} } } ]