Skip to content

fix(apigatewayv2): return the v2 response shape an SDK parses (#529) - #567

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

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

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #529.

The last of the three plugins. Every apigatewayv2 response marshaled a PascalCase
state struct straight onto the wire under an Items envelope, and botocore matches a
response key against the model's locationName case-sensitively — so nothing
substrate returned parsed at all. Not a member missing here and there:

# before (main @ 4305baf), raw wire body from botocore's own debug log
b'{"ApiId":"llijecdmcl","Name":"pre3","ProtocolType":"HTTP","ApiEndpoint":"…",
   "CreatedDate":"…","AccountID":"123456789012","Region":"us-east-1"}'

$ aws apigatewayv2 create-api --name pre --protocol-type HTTP --query ApiId
None
$ aws apigatewayv2 get-apis --query 'length(Items)'
# fails on a null

# after
b'{"apiId":"eoffpmoiol","name":"post3","protocolType":"HTTP","apiEndpoint":"…",
   "createdDate":"…"}'

Note the two AccountID/Region keys in the before-body: substrate's own bookkeeping
went out on every v2 response.

This was invisible over the wire until PR A (15867b6) made v2 reachable by an SDK at
all — create-api used to 404 with unknown operation for POST /v2/apis, which is why
#529's "same check applied to API Gateway v2" criterion was unverifiable when it was
filed.

Remedy — the same one as #528, v1 (#565) and MSK

  • The seven state types keep their PascalCase tags and are untouched. They are a
    persisted format: MemoryStateManager snapshots them and recorded runs replay from
    those bytes. State encoding is unchanged, and TestAPIGatewayV2Wire_StateEncodingUnchanged
    asserts the stored bytes still carry ApiId/Name/ProtocolType/AccountID/Region
    and that no wire spelling appears in state — so that is a fact, not a claim, and it is
    the test that fails if someone later "fixes" a casing bug by retagging a state struct.
  • Nine response-only element types in a new apigatewayv2_wire.go, each tagged from the
    model, each with a Wire() projection.
  • No wire type declares an AccountID or Region member at all. The leak is fixed
    structurally rather than by an omitempty a future field could sidestep.
  • The five list envelopes now emit items — lowercase, and note this differs from
    v1's singular item
    . The two spellings are not a typo in one of them; each was
    transcribed from its own model.

v2APIMappingState was declared inline inside createAPIMapping and is now a
package-level type, because a wire projection needed to name it. Its tags are unchanged.

Why v2 was easier than v1, and why it was still done by hand

All 1,545 members of v2's response shapes are the plain lowerCamel of their member
names, against v1's ~230 irregular renames. The only irregular spellings anywhere in
the v2 model — ResourceArn -> "resource-arn" — are on the three tag request shapes,
which substrate does not route.

Each tag was still transcribed from the model rather than lowercased programmatically. A
mechanical transform is exactly what silently gets an exception wrong, and "there are no
exceptions" is a claim that has to be checked before it can be relied on — which is the
same mistake #529 as filed made about v1's envelope and about MSK.

Three members are now absent — from the model, not from the old code

  • A route, integration, stage and API mapping report no apiId. The API is a path
    parameter of the request, not a member of any of those four shapes. botocore discarded
    the key regardless, so nothing observable is lost.
  • A domain name reports no regionalDomainName. That spelling is v1's; v2 has no
    top-level equivalent and nests the regional hostname as
    domainNameConfigurations[].apiGatewayDomainName. Substrate computes the hostname
    either way — projecting it into that list is what makes it visible to an SDK instead
    of being dropped as an undeclared key.
  • No response carries a nextToken. Substrate returns every element in one page and
    honours no token; emitting an empty one would invite a caller to page on nothing.

Unset optionals are omitted rather than sent as "" or null, following each member's
optionality in the model, because "absent" versus "null" is a real observable a caller
can branch on.

Existing assertions that were wrong today and passed anyway

This is the part of the diff that must not read as weakening tests.

Go's json.Unmarshal matches an object key against a struct tag case-insensitively.
Measured, not assumed — a field tagged json:"ApiId" and one tagged json:"apiId" both
decode {"apiId":"abc123"} to abc123. That single fact is why this defect survived the
entire life of the plugin: the request side worked, every Go round-trip test passed, and
an assertion struct with the wrong tags stayed green.

So two groups of consumers were wrong and silent:

  • The four CloudFormation v2 deploy handlers (cfn_resources_v19.go) decode ApiId,
    RouteId, IntegrationId and AuthorizerId out of these response bodies to set a
    resource's PhysicalID and ARN. Retagged to the wire spellings.
  • ~10 assertions across apigatewayv2_plugin_test.go and apigateway_proxy_test.go
    read result["ApiId"], ["IntegrationId"], ["Items"] and decoded response bodies
    into emulator.V2*State — the state type this change deliberately keeps off the wire.
    Rewritten to read literal wire keys.

And the CloudFormation v2 tests asserted only assert.Len(t, result.Resources, N). A
physical ID silently falling back to a logical ID or a route key would not have failed
one, so a resource count could stay green while every Ref in the stack pointed at the
wrong thing. They now assert the ID itself.

Tests

apigatewayv2_wire_test.go, 13 tests, all decoding into map[string]any and asserting
literal wire keys — per #528's lesson that a struct marshaled and unmarshaled by its
own definition agrees with itself whatever its tags say.

  • One per element type, asserting values and not merely key presence: a Wire() that
    forgets a member still emits its key holding Go's zero value. Reuses requireKeys/
    requireValues from msk_wire_test.go.
  • One per list operation — five of them, not one shared helper. A single test over all
    five would pass while four were wrong.
  • Byte-level bytes.Contains negative assertions on AccountID, Region, accountId
    and region, so a nested occurrence cannot hide from a top-level map check.
  • _EmptyListsAreLists: a table over four collections asserting an empty collection is
    [] and not null, and that no nextToken appears.
  • _StateEncodingUnchanged, described above.
  • The domain-name test asserts the hostname is reachable at
    domainNameConfigurations[0].apiGatewayDomainName and that no top-level
    regionalDomainName survives.

Mutation testing

38 mutants, 38 CAUGHT, 0 SURVIVED, 0 BROKEN, 0 ANCHOR-MISS, against a green baseline,
each anchor count-asserted before applying and each mutant gated through gofmt -w +
go vet and run with -race.

Covered: a tag reverted to PascalCase on each of the nine element types; both envelope
reversions (Items, and v1's item — the cross-version confusion is a real hazard);
three list operations reverted independently; AccountID/Region added back to two wire
types; three single-resource returns left unprojected; nine Wire() projections each
dropping one member; two omitempty drops; the three non-model members added back; an
always-emitted nextToken; a state struct retagged instead of the wire type; and the
CloudFormation decode tags.

One mutant is worth recording because it was initially scored SURVIVED and is in fact
EQUIVALENT: reverting a CloudFormation decode tag to json:"ApiId" leaves the tests
green because it changes nothing — Go's case-insensitive decode, proven above. No test
can distinguish the two spellings. It was replaced with a mutant that does not case-fold
onto the wire key (json:"restApiId"), which is CAUGHT, and that is what proves the new
physical-ID assertions have teeth. The comment in the harness records the measurement so
the next reader does not mistake it for a coverage gap.

Verified end to end through botocore's real parser

Against a live substrate server, and against main @ 4305baf on a second port for the
before-comparison quoted at the top. 28 checks that returned None or an empty parse
now return values; 0 leaked fields.
Every v2 resource type is covered — API, route,
integration, stage, authorizer (including jwtConfiguration and identitySource),
deployment, domain name (both nested configuration members) and API mapping.

The AWS CLI presents these responses with PascalCase output keys (--query ApiId,
Items) because botocore maps locationName back to the member name. That is expected:
the wire is apiId, the CLI's own view of it is ApiId, and the whole defect was that
the mapping between them never happened.

One thing deliberately left out

aws apigatewayv2 get-api-mappings fails with unknown operation for GET /v2/domainnames/{name}/apimappings — only POST on that path is routed. That is a
missing operation, not a wrong response shape, so it is filed as #566 rather than
folded into a wire-format change. The shape work it needs is already done here
(v2APIMappingOut, v2APIMappingWire and the apigwV2ItemsOut[T] envelope all exist and
are tested), so it becomes a routing and state-indexing change. create-api-mapping
itself parses correctly today.

Verification

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

docs/services.md gains a Response shape paragraph for the v2 section: every member
lowerCamel, collections under items, no nextToken, and the three absent members with
the reason for each. The operation table is unchanged — no plugin is added or removed.

Compatibility

Any consumer asserting a PascalCase member, an AccountID/Region field, or an Items
envelope on an API Gateway v2 response will go red. That is the fix — no AWS SDK could
parse the old shape — but a consumer who pinned to the broken shape needs to be told
plainly. State encoding is unchanged, so recorded runs replay.

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.
@codecov

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 866a057 into main Aug 4, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the fix/529-apigw-v2-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.

API Gateway: state structs are marshalled straight to the wire, so responses are PascalCase and carry substrate's internal AccountID/Region

1 participant