From bb3167fd962c44cf68a0f42b6cbc830aacbef406 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Fri, 4 Jun 2021 10:23:08 -0700 Subject: [PATCH 1/7] sample extension --- src/test/swagger/oas3.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/swagger/oas3.yaml b/src/test/swagger/oas3.yaml index d7336464..4a2d7b60 100644 --- a/src/test/swagger/oas3.yaml +++ b/src/test/swagger/oas3.yaml @@ -427,6 +427,7 @@ paths: put: tags: - pet + x-sample-extension: "it works!" summary: Update an existing pet description: '' operationId: updatePet From 0a8799cf1320dfe21540be72ba4986c29822f11d Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Fri, 4 Jun 2021 10:23:26 -0700 Subject: [PATCH 2/7] set operation in RequestContext --- .../oas/inflector/controllers/OpenAPIOperationController.java | 1 + 1 file changed, 1 insertion(+) 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..5c7838e6 100644 --- a/src/main/java/io/swagger/oas/inflector/controllers/OpenAPIOperationController.java +++ b/src/main/java/io/swagger/oas/inflector/controllers/OpenAPIOperationController.java @@ -255,6 +255,7 @@ public Response apply(ContainerRequestContext ctx) { List parameters = operation.getParameters(); final RequestContext requestContext = createContext(ctx); + requestContext.setOperation(operation); Map inputStreams = new HashMap<>(); Object[] args = new Object[parameterClasses.length]; From d2bc9f8ffbb077b40d580e031097df50e442e7c7 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Fri, 4 Jun 2021 10:23:51 -0700 Subject: [PATCH 3/7] added accessor for Operation --- .../oas/inflector/models/RequestContext.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/java/io/swagger/oas/inflector/models/RequestContext.java b/src/main/java/io/swagger/oas/inflector/models/RequestContext.java index 966922d7..2e6c1822 100644 --- a/src/main/java/io/swagger/oas/inflector/models/RequestContext.java +++ b/src/main/java/io/swagger/oas/inflector/models/RequestContext.java @@ -16,18 +16,23 @@ package io.swagger.oas.inflector.models; +import io.swagger.v3.oas.models.Operation; + import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class RequestContext { ContainerRequestContext context; MultivaluedMap headers; MediaType mediaType; List acceptableMediaTypes; + private Operation operation; private String remoteAddr; private final HttpServletRequest request; private final HttpServletResponse response; @@ -54,6 +59,26 @@ public RequestContext(ContainerRequestContext context, HttpServletRequest reques this.response = response; } + public RequestContext operation(Operation operation) { + this.operation = operation; + return this; + } + + public Operation getOperation() { + return this.operation; + } + + public void setOperation(Operation operation) { + this.operation = operation; + } + + public Map getExtensions() { + if(operation != null && operation.getExtensions() != null) { + return operation.getExtensions(); + } + return new HashMap<>(); + } + public RequestContext headers(MultivaluedMap headers) { this.headers = headers; return this; From 3103dc375fc05ca758bc364d58863177791a8d57 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Fri, 4 Jun 2021 10:24:16 -0700 Subject: [PATCH 4/7] read the extensions in one method --- .../io/swagger/oas/sample/controllers/TestController.java | 6 ++++++ 1 file changed, 6 insertions(+) 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..2cd3e549 100644 --- a/src/test/java/io/swagger/oas/sample/controllers/TestController.java +++ b/src/test/java/io/swagger/oas/sample/controllers/TestController.java @@ -73,6 +73,12 @@ public ResponseContext inlineRequiredBody(RequestContext request, com.fasterxml. } public ResponseContext updatePet(RequestContext request, com.fasterxml.jackson.databind.JsonNode petType) { + if(request.getOperation() != null && request.getExtensions().containsKey("x-sample-extension")) { + String message = request.getExtensions().get("x-sample-extension").toString(); + return new ResponseContext() + .status(200) + .entity(message); + } return new ResponseContext() .status(200) .entity("OK!"); From 68033a5d1c31766724cb6a4b29304d9b0aa1b3ff Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Fri, 4 Jun 2021 12:03:39 -0700 Subject: [PATCH 5/7] renamed method to avoid confusion, added extension for implementation --- src/test/swagger/oas3.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/swagger/oas3.yaml b/src/test/swagger/oas3.yaml index 4a2d7b60..63f4abe6 100644 --- a/src/test/swagger/oas3.yaml +++ b/src/test/swagger/oas3.yaml @@ -307,7 +307,7 @@ paths: /pets: post: x-swagger-router-controller: TestController - operationId: updatePet + operationId: updatePetByType requestBody: content: application/json: @@ -428,6 +428,7 @@ paths: tags: - pet x-sample-extension: "it works!" + x-swagger-router-controller: TestController summary: Update an existing pet description: '' operationId: updatePet From 5de18361e5cff92fbee1e62b44e653ebb9833ad6 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Fri, 4 Jun 2021 12:04:11 -0700 Subject: [PATCH 6/7] method names, path name changes, check for extensions --- .../oas/test/integration/RequestTestIT.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) 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 1477ebb2..46887971 100644 --- a/src/test/java/io/swagger/oas/test/integration/RequestTestIT.java +++ b/src/test/java/io/swagger/oas/test/integration/RequestTestIT.java @@ -47,19 +47,20 @@ public class RequestTestIT { @Test public void verifyUpdatePet() throws Exception { - String path = "/pets"; + String path = "/pet"; 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]); + body.put("id", "10"); + body.put("name","Cat"); + Response response = client.getResponse(path, "PUT", new HashMap(), body, new HashMap(), null, "application/json", null, new String[0]); assertNotNull(response.getCookies()); - assertEquals(1, response.getCookies().size()); + assertEquals(response.getCookies().size(), 1); NewCookie cookie = response.getCookies().get("type"); - assertEquals("type", cookie.getName()); - assertEquals("chocolate", cookie.getValue()); + assertEquals(cookie.getName(), "type"); + assertEquals(cookie.getValue(), "chocolate"); } @Test - public void verifyUpdatePet() throws Exception { + public void verifyUpdatePetWithNullBody() throws Exception { String path = "/pets"; String str = client.invokeAPI(path, "POST", new HashMap(), null, new HashMap(), null, "application/json", null, new String[0]); assertEquals(str, "OK!"); From 2e1124e1ae4c94a13e19fa9d5db6bd41e769bf02 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Fri, 4 Jun 2021 12:04:21 -0700 Subject: [PATCH 7/7] switch test to different method --- .../io/swagger/oas/test/integration/SwaggerListingIT.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/io/swagger/oas/test/integration/SwaggerListingIT.java b/src/test/java/io/swagger/oas/test/integration/SwaggerListingIT.java index 03c0599a..74410e50 100644 --- a/src/test/java/io/swagger/oas/test/integration/SwaggerListingIT.java +++ b/src/test/java/io/swagger/oas/test/integration/SwaggerListingIT.java @@ -85,11 +85,11 @@ public void verifyDebugJsonParameterNames() throws Exception { null, new String[0]); - String [] substr = response.split("\"public ResponseContext updatePet"); + String [] substr = response.split("\"public ResponseContext addPet"); assertTrue(substr.length > 1); String signature = substr[1]; - String paramName = signature.split("io.swagger.oas.sample.models.Pet", 2)[1].trim(); + String paramName = signature.split("io.swagger.oas.sample.models.Dog", 2)[1].trim(); assertTrue(paramName.startsWith("p1")); }