Skip to content

fix(apigateway): return the v1 response shape an SDK parses (#529) - #565

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/529-apigw-v1-wire
Aug 4, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/529-apigw-v1-wire

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Every API Gateway v1 response substrate sent was unparseable by an AWS SDK, and
nothing reported it. aws apigateway create-rest-api --query id printed an empty
line; get-rest-apis --query 'length(items)' failed on a null. Both at HTTP 200
with a well-formed body and no error.

The mechanism is #528's: the plugin marshaled its state struct onto the wire,
and botocore's rest-json parser matches a response key against the model's
locationName (falling back to the member name) case-sensitively. A
PascalCase key matches nothing, so the parse produces an empty result rather than
a failure. Feeding substrate's actual body to create_parser('rest-json') turns
{"Id":"a1","Name":"n1",…} into {} — not "some fields missing", nothing at all.

It hid because Go's json.Unmarshal matches keys case-insensitively. The
request side therefore works, every Go round-trip test passes, and a hand-written
assertion struct with the wrong tags stays green. Response-only fix.

Three corrections to #529 as filed

The items envelope was not already correct. The issue says it is. Every v1
collection member is named items but carries locationName: "item", singular,
so {"items":[…]} parses to {} and {"item":[…]} parses. Seven of the eight
list handlers sent the plural; they now send item.

GetStages is the exception and is deliberately unchanged. Its model member is
literally named item, and substrate already emitted item. A blanket rename
would have broken the only v1 list an SDK could already read. It keeps its key,
with a comment saying why and a regression test that goes red if someone
"corrects" it.

resourceMethods and methodIntegration were never populated at all. Not a
casing problem — methods and integrations were stored under separate state keys and
never joined, so both members were structurally absent from every response no
matter how they were spelled. The recursive projection is only observable once that
join exists, so this PR adds it: GetResource, GetResources and GetMethod now
report all three depths.

The fix

The eleven state types in apigateway_types.go are untouched. Their PascalCase
tags are a persisted format that MemoryStateManager.Snapshot round-trips and
recorded runs replay from — retagging them would silently change the format of
every recording. State encoding is unchanged, and a test asserts it by checking
the stored bytes still carry Id/Name/RootResourceId/AccountID/Region and
that the wire spellings do not appear there.

apigateway_wire.go adds eleven response-only element types tagged from the
botocore model, each with a …Wire() projection, plus a generic apigwItemsOut[T]
envelope. None of the wire types has an AccountID, Region or APIId member at
all — the leak is closed structurally, not by an omitempty a future field could
step around. resourceWire recurses into methodWire which recurses into
integrationWire.

MethodResponses and IntegrationResponses hold whatever the caller PUT. They pass
through unchanged: those are the caller's own keys, not substrate's to rename.

Judgement calls, made from the model rather than the previous code

  • providerARNs keeps its capitalised acronym — that is what the model spells, and
    a mechanical lowerCamel transform would get it wrong.
  • UsagePlan reports no createdDate and BasePathMapping no domainName.
    Neither is a member of its shape; substrate stores both, and a mapping's domain
    is a path parameter of the request.
  • Unset optionals are omitted rather than sent as "" or null, following each
    model member's optionality. A caller distinguishing absent from null is making a
    real observation.
  • No response invents a position token. Substrate returns every element in one
    page and honors no token; emitting one would invite a caller to page on nothing.
  • GetStage's invokeUrl is substrate's own member that no model declares. It is
    kept (a consumer reading the raw response uses it) and respelled from InvokeUrl.
    It cannot break an SDK caller, because botocore drops a key the model does not
    declare.

The updated existing assertions were wrong today and passed anyway

Ten sites in apigateway_plugin_test.go and two in apigateway_proxy_test.go
assert on PascalCase members and items envelopes. They passed because of the
case-insensitive Unmarshal above, not because the wire was right — one carried a
comment documenting the broken spelling as intended ("RestAPIState uses json tag
Id (capital I)"
). Updating them is part of the fix, not a weakening of the
tests. The GetRestApis decode could not simply be retagged: it decoded into
[]emulator.RestAPIState, which is the state type this PR is specifically keeping
off the wire, so it now decodes into a local struct with wire tags.

Verification

gofmt -l ., go build ./..., go vet ./..., golangci-lint run ./... → 0
issues, make test (race), make docs-reference docs-reference-check docs-versions,
npm --prefix docs run build, go vet -C test/e2e ./..., go test -C test/e2e ./....

apigateway_wire_test.go adds 18 tests that decode into map[string]any and assert
literal wire keys — including one envelope test per list operation (a single
test over all eight would pass while seven were wrong), the GetStages regression
gate, the three-depth nested-projection test, byte-level negative assertions on
AccountID/Region/APIId, empty-list-is-a-list-not-null, and the
state-encoding-invariance test. Value assertions throughout, not key presence: a
Wire() that drops a member still emits its key holding Go's zero value.

Mutation testing: 30 mutants, 30 CAUGHT. Including getStages "fixed" to
items, two different list operations reverted independently, per-type tag
reversions, AccountID and APIId added back to a wire type, the nested projection
skipped at each of two levels, both joins removed, eight separate single-member
Wire() drops, two omitempty drops, the two non-model members added back, an
invented position, and the state struct retagged. One survivor was found and
fixed on the way: the API-key test compared got["value"] against
created["value"], so dropping the member compared "" with "" — it now asserts
non-empty before comparing.

Live against substrate server --address :4621, every call parsing a non-empty
result where it returned empty before:

create-rest-api --query id                → ojkaahjfpk
get-rest-apis --query 'length(items)'     → 1
get-resources items[0].path               → /
get-method                                → GET  True  AWS_PROXY  arn:…
get-resource resourceMethods.GET.methodIntegration.type  → AWS_PROXY   # all three depths
get-stages item[0].[stageName,deploymentId]              → prod  mbbmfpmbli   # regression gate
authorizer / api-key / usage-plan / domain-name / base-path-mapping → all parse
AccountID|Region|APIId grep on every response            → 0

Raw body: {"item":[{"id":"ojkaahjfpk","name":"prod-api","description":"a description","rootResourceId":"pnmjfhiipp","createdDate":"…"}]}.

No other consumer of the v1 wire shape exists: tagging_plugin.go reads state
directly, and cfn_resources_v19.go already decodes lowerCamel and computes the
stage invoke URL locally rather than reading it from the response.

Scope

v1 only. API Gateway v2 is the same defect and is the next PR — aws apigatewayv2 create-api --query ApiId still returns None today. #529 covers v1, v2 and MSK, so
it stays open until v2 lands; MSK was #564.

Refs #529

Every apigateway v1 response used PascalCase members under an "items"
envelope. botocore matches a response key against the model's locationName
case-sensitively, so nothing substrate returned parsed: `create-rest-api
--query id` printed nothing and `get-rest-apis --query 'length(items)'`
failed on a null, both at HTTP 200 with no error.

Both halves were wrong. This corrects #529 as filed, which says the "items"
envelope is already correct: every v1 collection member is named "items" but
carries locationName "item", singular. Seven of the eight list handlers sent
the plural and now send "item". GetStages is the exception — its model member
is literally named "item", substrate already emitted it, and it is left
unchanged, with a regression test, because a blanket rename would break the
only v1 list an SDK could already read.

Fixed as #528 fixed CloudWatch Logs: the eleven state types keep their
PascalCase tags and are untouched, so state encoding is unchanged and
recorded runs replay. Eleven response-only element types tagged from the
model are projected from them, and none carries an AccountID, Region or
APIId member at all, so the leak is closed structurally.

The projection recurses, which needed a second fix to be observable:
Resource declares a resourceMethods map and Method a methodIntegration, but
methods and integrations were stored under separate state keys and never
joined, so both members were permanently absent from every response.
GetResource and GetMethod now report them at all three depths.

From the model rather than the previous code: providerARNs keeps its
capitalised acronym; UsagePlan reports no createdDate and BasePathMapping no
domainName, neither being a member of its shape; unset optionals are omitted
rather than sent as "" or null; no response invents a position token.
GetStage's invokeUrl is substrate's own and no model declares it — kept,
respelled from InvokeUrl, and harmless because botocore drops an undeclared
key.

Existing assertions in apigateway_plugin_test.go and apigateway_proxy_test.go
are updated to the real wire keys. They were wrong today and passed anyway,
because Go's json.Unmarshal matches case-insensitively; one carried a comment
documenting the broken spelling as intended.

Refs #529
@codecov

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.66667% with 22 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/apigateway_plugin.go 75.00% 11 Missing and 11 partials ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 4305baf into main Aug 4, 2026
16 checks passed
scttfrdmn added a commit that referenced this pull request Aug 4, 2026
…567)

Every apigatewayv2 response marshaled a PascalCase state struct onto the wire
under an "Items" envelope. botocore matches a response key against the model's
locationName case-sensitively, so nothing substrate returned parsed: `aws
apigatewayv2 create-api --query ApiId` printed None and `get-apis --query
'length(Items)'` failed on a null. Substrate's own AccountID and Region went out
on every response too.

Same remedy as v1 (#565) and MSK: the state types keep their PascalCase tags and
are untouched, so state encoding is unchanged and recorded runs replay. Nine
response-only element types tagged from the botocore model are projected from
them, and none declares an AccountID or Region member at all, so the leak cannot
return through a field added later. The five list envelopes emit "items".

Three members are now absent, from the model rather than the previous code: a
route, integration, stage and API mapping declare no apiId (the API is a path
parameter, not a member), a domain name reports the regional hostname where v2
puts it — domainNameConfigurations[].apiGatewayDomainName — rather than under
v1's regionalDomainName spelling, and no response carries a nextToken.

The CloudFormation v2 deploy handlers read these responses to set a resource's
physical ID and now read the wire spellings; they worked before only because Go's
json.Unmarshal is case-insensitive, which is also why ~10 existing assertions
were wrong today and passed anyway. Those CloudFormation tests asserted only a
resource count, so they now assert the physical ID itself.

Verified through botocore's own parser against a live server: 28 checks that
returned None or an empty parse now return values, and 0 leaked fields.
@scttfrdmn
scttfrdmn deleted the fix/529-apigw-v1-wire branch August 9, 2026 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant