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

Added operation passthrough in RequestContext #394

Merged
merged 8 commits into from
Jun 7, 2021
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 @@ -256,6 +256,7 @@ public Response apply(ContainerRequestContext ctx) {
List<Parameter> parameters = operation.getParameters();

final RequestContext requestContext = createContext(ctx);
requestContext.setOperation(operation);
Map<String, File> inputStreams = new HashMap<>();

Object[] args = new Object[parameterClasses.length];
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/io/swagger/oas/inflector/models/RequestContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> headers;
MediaType mediaType;
List<MediaType> acceptableMediaTypes;
private Operation operation;
private String remoteAddr;
private final HttpServletRequest request;
private final HttpServletResponse response;
Expand All @@ -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<String, Object> getExtensions() {
if(operation != null && operation.getExtensions() != null) {
return operation.getExtensions();
}
return new HashMap<>();
}

public RequestContext headers(MultivaluedMap<String, String> headers) {
this.headers = headers;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,22 @@ public ResponseContext inlineRequiredBody(RequestContext request, com.fasterxml.
.entity("success!");
}

public ResponseContext updatePet(RequestContext request, com.fasterxml.jackson.databind.JsonNode petType) {
NewCookie cookie = new NewCookie("type", "chocolate");
public ResponseContext updatePet(RequestContext request, Pet petType) {
if(request.getOperation() != null && request.getExtensions().containsKey("x-sample-extension")) {
NewCookie cookie = new NewCookie("type", "chocolate");
String message = request.getExtensions().get("x-sample-extension").toString();
return new ResponseContext()
.status(200)
.cookie(cookie)
.entity(message);
}
return new ResponseContext()
.status(200)
.entity("oops");
}

public ResponseContext updatePetByType(RequestContext request, com.fasterxml.jackson.databind.JsonNode petType) {
return new ResponseContext()
.cookie(cookie)
.status(200)
.entity("OK!");
}
Expand Down
15 changes: 8 additions & 7 deletions src/test/java/io/swagger/oas/test/integration/RequestTestIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,20 @@ public class RequestTestIT {

@Test
public void verifyUpdatePet() throws Exception {
String path = "/pets";
String path = "/pet";
Map<String,String> body = new HashMap<>();
body.put("pet_type","Cat");
Response response = client.getResponse(path, "POST", new HashMap<String, String>(), body, new HashMap<String, String>(), null, "application/json", null, new String[0]);
body.put("id", "10");
body.put("name","Cat");
Response response = client.getResponse(path, "PUT", new HashMap<String, String>(), body, new HashMap<String, String>(), 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<String, String>(), null, new HashMap<String, String>(), null, "application/json", null, new String[0]);
assertEquals(str, "OK!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand Down
4 changes: 3 additions & 1 deletion src/test/swagger/oas3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ paths:
/pets:
post:
x-swagger-router-controller: TestController
operationId: updatePet
operationId: updatePetByType
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were mapping two operations to the same controller implementation (updatePet) which was making it really confusing.

requestBody:
content:
application/json:
Expand Down Expand Up @@ -427,6 +427,8 @@ paths:
put:
tags:
- pet
x-sample-extension: "it works!"
x-swagger-router-controller: TestController
summary: Update an existing pet
description: ''
operationId: updatePet
Expand Down