fix(apigateway): key stage method settings by the patch path as sent (#7977)

* fix(apigateway): key stage method settings by the patch path as sent

UpdateStage decoded the JSON Pointer escapes in a method-settings patch
path before using it as the methodSettings key, so
/~1webhook~1status/POST/logging/loglevel was stored as
webhook/status/POST. API Gateway keeps the path as written and GetStage
reports ~1webhook~1status/POST, which is the key the Terraform provider
looks up for aws_api_gateway_method_settings; on Floci the lookup found
nothing and the read failed with "empty result".

The method-settings key is now the patch path between the leading slash
and the setting name, unchanged, so the escaped and plain spellings each
round-trip under the key the client sent.

* fix(apigateway): find escaped method-settings keys at invoke time

Method settings are now stored under the patch path as sent, so a
caching patch to /~1widget/GET/caching/enabled lands under ~1widget/GET.
The invoke-time lookup only tried the decoded resource path (widget/GET)
and fell through to */*, so caching enabled that way never applied.

methodSettingFor now tries the JSON Pointer escaped resource path first,
then the plain path, then the */* wildcard.

* style(apigateway): brace single-line ifs in method-settings lookup
This commit is contained in:
Tyler Webb authored and GitHub committed 2026-11-11 08:48:24 -04:00
1 parent 8eaf5bf59c
commit 54bc8fc563
3 files changed
+38 -27

No files matched your search

@@ -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<String, MethodSetting> 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,
@@ -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());
@@ -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