diff --git a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayExecuteController.java b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayExecuteController.java index aa4f25435f..f593619934 100644 --- a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayExecuteController.java +++ b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayExecuteController.java @@ -766,13 +766,27 @@ public class ApiGatewayExecuteController { /** * The method's own settings, falling back to the stage-wide wildcard entry. Settings are keyed - * by the resource path without its leading slash ({@code pets/GET}), matching what AWS reports. + * by the patch path as the client sent it, so the method is found under either the JSON Pointer + * escaped resource path AWS reports ({@code ~1pets/GET}) or the plain one ({@code pets/GET}). */ private static MethodSetting methodSettingFor(Stage stage, String resourcePath, String httpMethod) { - String normalized = resourcePath != null && resourcePath.startsWith("/") - ? resourcePath.substring(1) : resourcePath; - MethodSetting exact = stage.getMethodSettings().get(normalized + "/" + httpMethod); - return exact != null ? exact : stage.getMethodSettings().get("*/*"); + Map settings = stage.getMethodSettings(); + if (resourcePath != null) { + MethodSetting escaped = settings.get(escapeJsonPointer(resourcePath) + "/" + httpMethod); + if (escaped != null) { + return escaped; + } + String plain = resourcePath.startsWith("/") ? resourcePath.substring(1) : resourcePath; + MethodSetting exact = settings.get(plain + "/" + httpMethod); + if (exact != null) { + return exact; + } + } + return settings.get("*/*"); + } + + private static String escapeJsonPointer(String segment) { + return segment.replace("~", "~0").replace("/", "~1"); } private String cacheKeyParameterValue(String param, HttpHeaders headers, UriInfo uriInfo, diff --git a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java index 2bb95675ce..1ff171e0a9 100644 --- a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java +++ b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java @@ -1160,16 +1160,10 @@ public class ApiGatewayService implements ResourceProvider, Resettable { String suffix = "/" + settingKey; if (!path.endsWith(suffix)) continue; - String prefix = path.substring(1, path.length() - suffix.length()); - int lastSlash = prefix.lastIndexOf('/'); - if (lastSlash < 0) return; - // AWS escapes the resource path's slashes as ~1 in the patch path ("/~1pets/GET/...") - // but reports the setting keyed by the plain path ("pets/GET"), so normalise both the - // escaped and unescaped spellings onto that one key. - String resourcePath = unescapeJsonPointer(prefix.substring(0, lastSlash)); - if (resourcePath.startsWith("/")) resourcePath = resourcePath.substring(1); - String httpMethod = prefix.substring(lastSlash + 1); - String methodKey = resourcePath + "/" + httpMethod; + String methodKey = path.substring(1, path.length() - suffix.length()); + if (methodKey.lastIndexOf('/') < 0) { + return; + } MethodSetting setting = stage.getMethodSettings() .computeIfAbsent(methodKey, k -> new MethodSetting()); diff --git a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayIntegrationReadBackTest.java b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayIntegrationReadBackTest.java index 935bab4ccb..6f4f600440 100644 --- a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayIntegrationReadBackTest.java +++ b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayIntegrationReadBackTest.java @@ -8,6 +8,8 @@ import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasKey; +import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; /** @@ -114,7 +116,7 @@ class ApiGatewayIntegrationReadBackTest { } @Test - void methodSettingsAcceptTheEscapedResourcePathAwsSends() { + void methodSettingsKeepTheEscapedResourcePathAsTheKey() { String deploymentId = given().contentType(ContentType.JSON).body("{}") .when().post("/restapis/" + apiId + "/deployments") .then().statusCode(201).extract().path("id"); @@ -123,21 +125,25 @@ class ApiGatewayIntegrationReadBackTest { .when().post("/restapis/" + apiId + "/stages") .then().statusCode(201); - // AWS escapes the resource path's slashes as ~1. That spelling previously produced the - // unusable key "~1widget/POST" instead of "widget/POST". given().contentType(ContentType.JSON) - .body("{\"patchOperations\":[{\"op\":\"replace\"," - + "\"path\":\"/~1widget/POST/throttling/burstLimit\",\"value\":\"77\"}]}") + .body("{\"patchOperations\":[" + + "{\"op\":\"replace\",\"path\":\"/~1widget/POST/throttling/burstLimit\",\"value\":\"77\"}," + + "{\"op\":\"replace\",\"path\":\"/~1webhook~1status/POST/logging/loglevel\",\"value\":\"INFO\"}," + + "{\"op\":\"replace\",\"path\":\"/~1webhook~1status/POST/logging/dataTrace\",\"value\":\"true\"}]}") .when().patch("/restapis/" + apiId + "/stages/test") .then().statusCode(200); given().when().get("/restapis/" + apiId + "/stages/test") .then().statusCode(200) - .body("methodSettings.'widget/POST'.throttlingBurstLimit", equalTo(77)); + .body("methodSettings.'~1widget/POST'.throttlingBurstLimit", equalTo(77)) + .body("methodSettings.'~1webhook~1status/POST'.loggingLevel", equalTo("INFO")) + .body("methodSettings.'~1webhook~1status/POST'.dataTraceEnabled", equalTo(true)) + .body("methodSettings", not(hasKey("widget/POST"))) + .body("methodSettings", not(hasKey("webhook/status/POST"))); } @Test - void methodSettingsUnescapeTheJsonPointerTildeEscape() { + void methodSettingsKeepTheTildeEscapeInTheKey() { String deploymentId = given().contentType(ContentType.JSON).body("{}") .when().post("/restapis/" + apiId + "/deployments") .then().statusCode(201).extract().path("id"); @@ -146,9 +152,6 @@ class ApiGatewayIntegrationReadBackTest { .when().post("/restapis/" + apiId + "/stages") .then().statusCode(201); - // RFC 6901 escapes a literal "~" as ~0 and "/" as ~1. "/~1pets~0promo/GET/..." therefore - // addresses the resource "/pets~promo"; only ~1 used to be decoded, leaving a stray "~0". - // "~01" is an escaped literal "~1", not a slash, so decoding order matters too. given().contentType(ContentType.JSON) .body("{\"patchOperations\":[" + "{\"op\":\"replace\",\"path\":\"/~1pets~0promo/GET/throttling/burstLimit\",\"value\":\"12\"}," @@ -158,8 +161,8 @@ class ApiGatewayIntegrationReadBackTest { given().when().get("/restapis/" + apiId + "/stages/tilde") .then().statusCode(200) - .body("methodSettings.'pets~promo/GET'.throttlingBurstLimit", equalTo(12)) - .body("methodSettings.'v~1/POST'.throttlingRateLimit", equalTo(3.5f)); + .body("methodSettings.'~1pets~0promo/GET'.throttlingBurstLimit", equalTo(12)) + .body("methodSettings.'~1v~01/POST'.throttlingRateLimit", equalTo(3.5f)); } @Test