diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java index 2d8fb28498..906188b018 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java @@ -735,7 +735,7 @@ public class ApiGatewayService { // 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 = prefix.substring(0, lastSlash).replace("~1", "/"); + String resourcePath = unescapeJsonPointer(prefix.substring(0, lastSlash)); if (resourcePath.startsWith("/")) resourcePath = resourcePath.substring(1); String httpMethod = prefix.substring(lastSlash + 1); String methodKey = resourcePath + "/" + httpMethod; @@ -747,6 +747,15 @@ public class ApiGatewayService { } } + /** + * Reverses RFC 6901 JSON Pointer escaping: {@code ~1} is a literal {@code /} and {@code ~0} a + * literal {@code ~}. The order matters: {@code ~1} must be decoded first so that {@code ~01} + * (an escaped literal "~1") is not turned into a slash. + */ + static String unescapeJsonPointer(String segment) { + return segment.replace("~1", "/").replace("~0", "~"); + } + private void applyMethodSettingValue(MethodSetting setting, String settingKey, String value) { if (value == null) return; switch (settingKey) { diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayIntegrationReadBackTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayIntegrationReadBackTest.java index 909005fb1d..d619f51a1d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayIntegrationReadBackTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayIntegrationReadBackTest.java @@ -136,6 +136,32 @@ class ApiGatewayIntegrationReadBackTest { .body("methodSettings.'widget/POST'.throttlingBurstLimit", equalTo(77)); } + @Test + void methodSettingsUnescapeTheJsonPointerTildeEscape() { + String deploymentId = given().contentType(ContentType.JSON).body("{}") + .when().post("/restapis/" + apiId + "/deployments") + .then().statusCode(201).extract().path("id"); + given().contentType(ContentType.JSON) + .body("{\"stageName\":\"tilde\",\"deploymentId\":\"" + deploymentId + "\"}") + .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\"}," + + "{\"op\":\"replace\",\"path\":\"/~1v~01/POST/throttling/rateLimit\",\"value\":\"3.5\"}]}") + .when().patch("/restapis/" + apiId + "/stages/tilde") + .then().statusCode(200); + + 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)); + } + @Test void anIntegrationWithNoTypeReportsRatherThanCrashing() { given().contentType(ContentType.JSON).body("{\"authorizationType\":\"NONE\"}")