mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(apigateway): preserve and update method response parameters (#5030)
* fix(apigateway): preserve and update method response parameters * fix(apigateway): serialize method response patches and decode header paths * fix(apigateway): return created for method response updates
This commit is contained in:
1 parent
1d44b5fc7c
commit
b988a061b4
4 files changed
+196
-2
No files matched your search
+51
@@ -0,0 +1,51 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.services.apigateway.ApiGatewayClient;
|
||||
import software.amazon.awssdk.services.apigateway.model.PatchOperation;
|
||||
import software.amazon.awssdk.services.apigateway.model.Resource;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("API Gateway method responses")
|
||||
class ApiGatewayMethodResponseCompatibilityTest {
|
||||
|
||||
@Test
|
||||
void responseParametersRoundTripThroughTheSdk() {
|
||||
try (ApiGatewayClient apiGateway = TestFixtures.apiGatewayClient()) {
|
||||
String apiId = apiGateway.createRestApi(request -> request
|
||||
.name(TestFixtures.uniqueName("method-response"))).id();
|
||||
try {
|
||||
String rootId = apiGateway.getResources(request -> request.restApiId(apiId))
|
||||
.items().stream().filter(resource -> "/".equals(resource.path()))
|
||||
.findFirst().map(Resource::id).orElseThrow();
|
||||
String resourceId = apiGateway.createResource(request -> request
|
||||
.restApiId(apiId).parentId(rootId).pathPart("items")).id();
|
||||
apiGateway.putMethod(request -> request.restApiId(apiId)
|
||||
.resourceId(resourceId).httpMethod("GET").authorizationType("NONE"));
|
||||
|
||||
Map<String, Boolean> initial = Map.of("method.response.header.X-First", false);
|
||||
assertThat(apiGateway.putMethodResponse(request -> request.restApiId(apiId)
|
||||
.resourceId(resourceId).httpMethod("GET").statusCode("200")
|
||||
.responseParameters(initial)).responseParameters()).isEqualTo(initial);
|
||||
|
||||
Map<String, Boolean> updated = Map.of(
|
||||
"method.response.header.X-First", false,
|
||||
"method.response.header.X-Second", true);
|
||||
assertThat(apiGateway.updateMethodResponse(request -> request.restApiId(apiId)
|
||||
.resourceId(resourceId).httpMethod("GET").statusCode("200")
|
||||
.patchOperations(PatchOperation.builder().op("add")
|
||||
.path("/responseParameters/method.response.header.X-Second")
|
||||
.value("true").build())).responseParameters()).isEqualTo(updated);
|
||||
assertThat(apiGateway.getMethodResponse(request -> request.restApiId(apiId)
|
||||
.resourceId(resourceId).httpMethod("GET").statusCode("200"))
|
||||
.responseParameters()).isEqualTo(updated);
|
||||
} finally {
|
||||
apiGateway.deleteRestApi(request -> request.restApiId(apiId));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+20
@@ -173,6 +173,22 @@ public class ApiGatewayController {
|
||||
}
|
||||
}
|
||||
|
||||
@PATCH
|
||||
@Path("/restapis/{apiId}/resources/{resourceId}/methods/{httpMethod}/responses/{statusCode}")
|
||||
public Response updateMethodResponse(@Context HttpHeaders headers,
|
||||
@PathParam("apiId") String apiId,
|
||||
@PathParam("resourceId") String resourceId,
|
||||
@PathParam("httpMethod") String httpMethod,
|
||||
@PathParam("statusCode") String statusCode,
|
||||
String body) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<Map<String, String>> patchOperations = parsePatchOperations(body);
|
||||
MethodResponse response = service.updateMethodResponse(
|
||||
region, apiId, resourceId, httpMethod, statusCode, patchOperations);
|
||||
return Response.status(201).entity(toMethodResponseNode(response).toString())
|
||||
.type(MediaType.APPLICATION_JSON).build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/restapis/{apiId}/resources/{resourceId}/methods/{httpMethod}/responses/{statusCode}")
|
||||
public Response deleteMethodResponse(@Context HttpHeaders headers,
|
||||
@@ -2235,6 +2251,10 @@ public class ApiGatewayController {
|
||||
private ObjectNode toMethodResponseNode(MethodResponse r) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("statusCode", r.statusCode());
|
||||
if (r.responseParameters() != null && !r.responseParameters().isEmpty()) {
|
||||
ObjectNode parameters = node.putObject("responseParameters");
|
||||
r.responseParameters().forEach(parameters::put);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
+36
-2
@@ -541,7 +541,7 @@ public class ApiGatewayService implements ResourceProvider {
|
||||
resourceStore.put(resourceKey(region, apiId, resourceId), resource);
|
||||
}
|
||||
|
||||
public MethodResponse putMethodResponse(String region, String apiId, String resourceId,
|
||||
public synchronized MethodResponse putMethodResponse(String region, String apiId, String resourceId,
|
||||
String httpMethod, String statusCode,
|
||||
Map<String, Object> request) {
|
||||
MethodConfig method = getMethod(region, apiId, resourceId, httpMethod);
|
||||
@@ -564,7 +564,41 @@ public class ApiGatewayService implements ResourceProvider {
|
||||
return mr;
|
||||
}
|
||||
|
||||
public void deleteMethodResponse(String region, String apiId, String resourceId,
|
||||
public synchronized MethodResponse updateMethodResponse(String region, String apiId, String resourceId,
|
||||
String httpMethod, String statusCode,
|
||||
List<Map<String, String>> patchOperations) {
|
||||
MethodResponse response = getMethodResponse(region, apiId, resourceId, httpMethod, statusCode);
|
||||
Map<String, Boolean> parameters = new HashMap<>(
|
||||
response.responseParameters() != null ? response.responseParameters() : Map.of());
|
||||
if (patchOperations != null) {
|
||||
for (Map<String, String> patch : patchOperations) {
|
||||
String path = patch.get("path");
|
||||
if (path == null || !path.startsWith("/responseParameters/method.response.header.")
|
||||
|| path.length() == "/responseParameters/method.response.header.".length()) {
|
||||
throw new AwsException("BadRequestException", "Invalid patch operation", 400);
|
||||
}
|
||||
String name = unescapeJsonPointer(path.substring("/responseParameters/".length()));
|
||||
String op = patch.get("op");
|
||||
if ("remove".equals(op)) {
|
||||
parameters.remove(name);
|
||||
} else if ("add".equals(op) || "replace".equals(op)) {
|
||||
String value = patch.get("value");
|
||||
if (!"true".equals(value) && !"false".equals(value)) {
|
||||
throw new AwsException("BadRequestException", "Invalid patch operation", 400);
|
||||
}
|
||||
parameters.put(name, Boolean.parseBoolean(value));
|
||||
} else {
|
||||
throw new AwsException("BadRequestException", "Invalid patch operation", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
MethodResponse updated = new MethodResponse(statusCode, parameters);
|
||||
getMethod(region, apiId, resourceId, httpMethod).getMethodResponses().put(statusCode, updated);
|
||||
resourceStore.put(resourceKey(region, apiId, resourceId), getResource(region, apiId, resourceId));
|
||||
return updated;
|
||||
}
|
||||
|
||||
public synchronized void deleteMethodResponse(String region, String apiId, String resourceId,
|
||||
String httpMethod, String statusCode) {
|
||||
MethodConfig method = getMethod(region, apiId, resourceId, httpMethod);
|
||||
if (method.getMethodResponses().remove(statusCode) == null) {
|
||||
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasKey;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
@QuarkusTest
|
||||
class ApiGatewayMethodResponseParametersIntegrationTest {
|
||||
|
||||
@Test
|
||||
void putGetAndPatchResponseParameters() {
|
||||
String apiId = given().contentType("application/json")
|
||||
.body("{\"name\":\"method-response-parameters\"}")
|
||||
.when().post("/restapis").then().statusCode(201).extract().path("id");
|
||||
String resourceId = given().when().get("/restapis/" + apiId + "/resources")
|
||||
.then().statusCode(200).extract().path("item[0].id");
|
||||
given().contentType("application/json").body("{\"authorizationType\":\"NONE\"}")
|
||||
.when().put("/restapis/" + apiId + "/resources/" + resourceId + "/methods/GET")
|
||||
.then().statusCode(201);
|
||||
|
||||
String path = "/restapis/" + apiId + "/resources/" + resourceId + "/methods/GET/responses/200";
|
||||
given().contentType("application/json")
|
||||
.body("{\"responseParameters\":{\"method.response.header.X-First\":false}}")
|
||||
.when().put(path).then().statusCode(201)
|
||||
.body("responseParameters.'method.response.header.X-First'", equalTo(false));
|
||||
given().when().get(path).then().statusCode(200)
|
||||
.body("responseParameters.'method.response.header.X-First'", equalTo(false));
|
||||
|
||||
given().contentType("application/json")
|
||||
.body("{\"patchOperations\":[{\"op\":\"add\","
|
||||
+ "\"path\":\"/responseParameters/method.response.header.X-Second\","
|
||||
+ "\"value\":\"true\"}]}")
|
||||
.when().patch(path).then().statusCode(201)
|
||||
.body("responseParameters.'method.response.header.X-First'", equalTo(false))
|
||||
.body("responseParameters.'method.response.header.X-Second'", equalTo(true));
|
||||
given().contentType("application/json")
|
||||
.body("{\"patchOperations\":[{\"op\":\"replace\","
|
||||
+ "\"path\":\"/responseParameters/method.response.header.X-First\","
|
||||
+ "\"value\":\"true\"},{\"op\":\"remove\","
|
||||
+ "\"path\":\"/responseParameters/method.response.header.X-Second\"}]}")
|
||||
.when().patch(path).then().statusCode(201)
|
||||
.body("responseParameters.'method.response.header.X-First'", equalTo(true))
|
||||
.body("responseParameters", not(hasKey("method.response.header.X-Second")));
|
||||
given().when().get(path).then().statusCode(200)
|
||||
.body("responseParameters.'method.response.header.X-First'", equalTo(true));
|
||||
|
||||
given().contentType("application/json")
|
||||
.body("{\"patchOperations\":[{\"op\":\"add\","
|
||||
+ "\"path\":\"/responseParameters/method.response.header.X~0Tag\","
|
||||
+ "\"value\":\"true\"}]}")
|
||||
.when().patch(path).then().statusCode(201)
|
||||
.body("responseParameters.'method.response.header.X~Tag'", equalTo(true))
|
||||
.body("responseParameters", not(hasKey("method.response.header.X~0Tag")));
|
||||
given().when().get(path).then().statusCode(200)
|
||||
.body("responseParameters.'method.response.header.X~Tag'", equalTo(true));
|
||||
|
||||
IntStream.range(0, 16).parallel().forEach(index -> given().contentType("application/json")
|
||||
.body("{\"patchOperations\":[{\"op\":\"add\","
|
||||
+ "\"path\":\"/responseParameters/method.response.header.X-Concurrent-"
|
||||
+ index + "\",\"value\":\"true\"}]}")
|
||||
.when().patch(path).then().statusCode(201));
|
||||
for (int index = 0; index < 16; index++) {
|
||||
given().when().get(path).then().statusCode(200)
|
||||
.body("responseParameters.'method.response.header.X-Concurrent-" + index + "'",
|
||||
equalTo(true));
|
||||
}
|
||||
|
||||
given().contentType("application/json")
|
||||
.body("{\"patchOperations\":[{\"op\":\"replace\","
|
||||
+ "\"path\":\"/responseParameters/method.response.header.X-First\","
|
||||
+ "\"value\":\"false\"},{\"op\":\"add\","
|
||||
+ "\"path\":\"/responseParameters/method.response.header.X-Bad\","
|
||||
+ "\"value\":\"maybe\"}]}")
|
||||
.when().patch(path).then().statusCode(400);
|
||||
given().when().get(path).then().statusCode(200)
|
||||
.body("responseParameters.'method.response.header.X-First'", equalTo(true));
|
||||
given().contentType("application/json")
|
||||
.body("{\"patchOperations\":[{\"op\":\"add\","
|
||||
+ "\"path\":\"/responseParameters/method.response.header.X-First\","
|
||||
+ "\"value\":\"true\"}]}")
|
||||
.when().patch(path.replace("/200", "/404")).then().statusCode(404);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user