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());