diff --git a/src/main/java/io/swagger/oas/inflector/controllers/OpenAPIOperationController.java b/src/main/java/io/swagger/oas/inflector/controllers/OpenAPIOperationController.java index 46f5348a..34384aa8 100644 --- a/src/main/java/io/swagger/oas/inflector/controllers/OpenAPIOperationController.java +++ b/src/main/java/io/swagger/oas/inflector/controllers/OpenAPIOperationController.java @@ -56,6 +56,7 @@ import javax.servlet.http.HttpServletResponse; import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.NewCookie; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.UriInfo; @@ -440,6 +441,10 @@ public Response apply(ContainerRequestContext ctx) { } } + for (NewCookie cookie : wrapper.getCookies()) { + builder.cookie(cookie); + } + // entity if (wrapper.getEntity() != null) { builder.entity(wrapper.getEntity()); diff --git a/src/main/java/io/swagger/oas/inflector/models/ResponseContext.java b/src/main/java/io/swagger/oas/inflector/models/ResponseContext.java index 252935ac..6a639acb 100644 --- a/src/main/java/io/swagger/oas/inflector/models/ResponseContext.java +++ b/src/main/java/io/swagger/oas/inflector/models/ResponseContext.java @@ -19,10 +19,14 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.NewCookie; import javax.ws.rs.core.Response.Status; +import java.util.ArrayList; +import java.util.List; public class ResponseContext { private MultivaluedMap headers = new MultivaluedHashMap(); + private List cookies = new ArrayList<>(); private MediaType contentType; private int status = Status.OK.getStatusCode(); private Object entity; @@ -32,6 +36,11 @@ public ResponseContext header(String key, String value) { return this; } + public ResponseContext cookie(NewCookie cookie) { + this.cookies.add(cookie); + return this; + } + public ResponseContext contentType(MediaType contentType) { this.contentType = contentType; return this; @@ -65,6 +74,14 @@ public void setHeaders(MultivaluedMap headers) { this.headers = headers; } + public List getCookies() { + return cookies; + } + + public void setCookies(List cookies) { + this.cookies = cookies; + } + public MediaType getContentType() { return contentType; } diff --git a/src/test/java/io/swagger/oas/sample/controllers/TestController.java b/src/test/java/io/swagger/oas/sample/controllers/TestController.java index 04edc0e4..5021ebf7 100644 --- a/src/test/java/io/swagger/oas/sample/controllers/TestController.java +++ b/src/test/java/io/swagger/oas/sample/controllers/TestController.java @@ -29,6 +29,7 @@ import io.swagger.oas.test.models.User; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.NewCookie; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import java.io.File; @@ -73,7 +74,9 @@ public ResponseContext inlineRequiredBody(RequestContext request, com.fasterxml. } public ResponseContext updatePet(RequestContext request, com.fasterxml.jackson.databind.JsonNode petType) { + NewCookie cookie = new NewCookie("type", "chocolate"); return new ResponseContext() + .cookie(cookie) .status(200) .entity("OK!"); } diff --git a/src/test/java/io/swagger/oas/test/client/ApiClient.java b/src/test/java/io/swagger/oas/test/client/ApiClient.java index 703fd506..fcc1f09e 100644 --- a/src/test/java/io/swagger/oas/test/client/ApiClient.java +++ b/src/test/java/io/swagger/oas/test/client/ApiClient.java @@ -458,6 +458,73 @@ public String invokeAPI(String path, String method, Map queryPar } } + public Response getResponse(String path, String method, Map queryParams, Object body, Map headerParams, Entity formParams, String accept, String contentType, String[] authNames) throws ApiException { + updateParamsForAuth(authNames, queryParams, headerParams); + + final ClientConfig clientConfig = new ClientConfig(); + clientConfig.register(MultiPartFeature.class); + if (debugging) { + clientConfig.register(LoggingFeature.class); + } + Client client = ClientBuilder.newClient(clientConfig); + + WebTarget target = client.target(this.basePath).path(path); + + for (String key : queryParams.keySet()) { + String value = queryParams.get(key); + if (value != null) { + target = target.queryParam(key, value); + } + } + + Invocation.Builder invocationBuilder = target.request(contentType); + + for (String key : headerParams.keySet()) { + String value = headerParams.get(key); + if (value != null) { + invocationBuilder = invocationBuilder.header(key, value); + } + } + + for (String key : defaultHeaderMap.keySet()) { + if (!headerParams.containsKey(key)) { + String value = defaultHeaderMap.get(key); + if (value != null) { + invocationBuilder = invocationBuilder.header(key, value); + } + } + } + Response response = null; + + invocationBuilder = invocationBuilder.accept(accept); + + if ("GET".equals(method)) { + response = invocationBuilder.get(); + } else if ("POST".equals(method)) { + if (formParams != null) { + response = invocationBuilder.post(formParams); + } else if (body == null) { + response = invocationBuilder.post(null); + } else { + response = invocationBuilder.post(serialize(body, contentType)); + } + } else if ("PUT".equals(method)) { + if (formParams != null) { + response = invocationBuilder.put(formParams); + } else if (body == null) { + response = invocationBuilder.put(null); + } else { + response = invocationBuilder.put(serialize(body, contentType)); + } + } else if ("DELETE".equals(method)) { + response = invocationBuilder.delete(); + } else { + throw new ApiException(500, "unknown method type " + method); + } + + return response; + } + /** * Update query and header parameters based on authentication settings. * diff --git a/src/test/java/io/swagger/oas/test/integration/RequestTestIT.java b/src/test/java/io/swagger/oas/test/integration/RequestTestIT.java index e2c0b0ae..2799fe18 100644 --- a/src/test/java/io/swagger/oas/test/integration/RequestTestIT.java +++ b/src/test/java/io/swagger/oas/test/integration/RequestTestIT.java @@ -27,9 +27,7 @@ import org.testng.annotations.Test; import javax.ws.rs.client.Entity; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedHashMap; -import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.*; import java.io.ByteArrayInputStream; import java.io.File; import java.io.InputStream; @@ -46,6 +44,19 @@ public class RequestTestIT { ApiClient client = new ApiClient(); + + @Test + public void verifyUpdatePet() throws Exception { + String path = "/pets"; + Map body = new HashMap<>(); + body.put("pet_type","Cat"); + Response response = client.getResponse(path, "POST", new HashMap(), body, new HashMap(), null, "application/json", null, new String[0]); + assertNotNull(response.getCookies()); + assertEquals(1, response.getCookies().size()); + NewCookie cookie = response.getCookies().get("type"); + assertEquals("type", cookie.getName()); + assertEquals("chocolate", cookie.getValue()); + } @Test public void verifyValidDateTimeInput() throws Exception {