Files
floci/docs
Brian Taylorandlocal 881695ea3b feat(apigatewayv2): OpenAPI import and Terraform-visible API/stage attributes (#2714)
* feat(apigatewayv2): implement ImportApi and ReimportApi

HTTP APIs had no PUT route on /v2/apis or /v2/apis/{apiId}, so ImportApi and
ReimportApi fell through JAX-RS matching to the S3 path-style handler and came
back as NoSuchBucket. Terraform's aws_apigatewayv2_api calls ReimportApi
whenever `body` is set, so any HTTP API defined by an OpenAPI document failed
to apply.

Add an OpenAPI importer for the v2 object model, reusing the swagger-parser
already used by ImportRestApi/PutRestApi on the v1 side. It materialises
routes, integrations and authorizers from x-amazon-apigateway-integration,
x-amazon-apigateway-authorizer, x-amazon-apigateway-any-method and
x-amazon-apigateway-cors, honouring document- and operation-level security.

ReimportApi replaces routes, integrations and authorizers as AWS does, but
leaves API settings an OpenAPI document cannot express (CORS when the spec
carries no x-amazon-apigateway-cors, tags, disableExecuteApiEndpoint) intact,
so a Terraform resource whose cors_configuration lives outside body does not
flap.

Both actions are wired for the restJson1 and JSON 1.1 protocols.

* fix(apigatewayv2): tag stages, not just APIs

TagResource, UntagResource and GetTags read the API id from the last segment of
the resource ARN, so a stage ARN (/apis/{apiId}/stages/{stageName}) yielded the
stage name as the API id and failed with "Invalid API id specified". Parse the
resource path by segment instead, and route the operation to the stage when the
ARN names one.

Stage had no tags field at all, so tags supplied to CreateStage were dropped and
never returned on read. Add it, populate it on create, and serialise it in both
the restJson1 and JSON 1.1 stage representations.

Terraform's aws_apigatewayv2_stage sets tags on create and then reconciles them,
so both halves were needed for a stage with tags to converge.

* feat(apigatewayv2): stage access logging, throttling and API version

Four attributes were accepted on write but dropped on read, so Terraform
re-proposed them on every plan and an aws_apigatewayv2_api/aws_apigatewayv2_stage
pair never converged:

- Api.version, also carried across from an imported document's info.version, as
  AWS does
- Stage.accessLogSettings (CloudWatch destination and format)
- Stage.defaultRouteSettings and per-route Stage.routeSettings (throttling,
  data trace, detailed metrics, logging level)

Throttling and logging fields are boxed rather than primitive: AWS omits unset
knobs instead of defaulting them, and emitting a zero would leave Terraform
seeing a changed value forever.

Stage also gains description, which CreateStage accepted and discarded.

Serialised for both the restJson1 and JSON 1.1 protocols.

* feat(apigatewayv2): honour basepath and failOnWarnings on OpenAPI import

ImportApi and ReimportApi accepted both documented query parameters and then
discarded them, so basepath=prepend or split produced routes at the wrong paths
with no error, and failOnWarnings never rolled anything back.

- basepath now resolves the base path the OpenAPI 3 way — a "basePath" server
  variable first (the first one, if several are declared), otherwise the path
  carried by server.url — and applies ignore, prepend or split. An unrecognised
  value is a BadRequestException rather than silence.
- failOnWarnings turns swagger-parser messages into a 400. Parsing and
  validation now happen before ReimportApi deletes anything, so a rejected
  reimport leaves the existing routes, integrations and authorizers untouched
  instead of gutting the API it was going to replace.
- The Api resource carries the warnings and importInfo the AWS model defines,
  the latter noting definition properties the import ignored.

* fix(apigatewayv2): validate authorizers before reimport mutates, warn on unenforceable security

Two review findings on the OpenAPI importer:

- Authorizer resolution ran while the spec was being applied, so a definition
  that parsed cleanly but declared an unsupported authorizer type — or a REQUEST
  authorizer with caching and no identitySource — was rejected only after
  ReimportApi had already deleted the previous routes, integrations and
  authorizers. Resolution now happens in the planning phase, side-effect free, so
  the rejection leaves the existing definition untouched. This mirrors
  validateImportedAuthorizers on the v1 side.
- A security requirement naming a scheme with no AWS binding — a plain bearer or
  apiKey scheme, or a misspelling — silently produced authorizationType NONE, so
  a document that declares security could yield a publicly invokable route. That
  now raises a warning naming the route and the schemes, which failOnWarnings
  escalates to a 400.

Also adds the braces AGENTS.md requires to the conditionals introduced by the
earlier commits in this branch.

* fix(apigatewayv2): examine ANY-method routes and compound security on import

Two more review findings, both in the warning collection added by the previous
commit:

- The diagnostic passes walked PathItem.readOperationsMap(), which does not
  include x-amazon-apigateway-any-method. An ANY route whose security named an
  unbindable scheme therefore produced no warning, failOnWarnings could not
  reject it, and it was still imported as NONE — the same silent-public-route
  hole the previous commit closed for ordinary operations, left open for the
  route type that matches every method. Route creation and both collectors now
  share one forEachImportedOperation enumeration so they cannot diverge again.
- Several schemes inside a single SecurityRequirement is OpenAPI's AND. A route
  carries one authorizer, so resolveSecurity enforced the first resolvable
  scheme and dropped the rest silently. That now warns, naming the dropped
  schemes and the one actually enforced.

* fix(apigatewayv2): report that imported AWS_IAM routes are not enforced

A sigv4 security scheme imports to an AWS_IAM route, which is what AWS records,
but HTTP API dispatch enforces only JWT and CUSTOM. A document declaring SigV4
therefore produced a route that looks authenticated and is not.

The route keeps AWS_IAM — downgrading it to NONE would misreport the definition
to anything reading the control plane — and the import now warns that the
emulator records the type without enforcing it. The dispatch gap itself predates
this change: CreateRoute has always accepted AWS_IAM unvalidated, and
ApiGatewayExecuteController already notes that v1 dispatch does not implement
AWS_IAM either.

* fix(apigatewayv2): validate jwtConfiguration shape before reimport mutates

jwtConfiguration was copied out of the authorizer extension unchecked, but
audience reaches the store as a List<String> and is cast as one. A scalar
audience — the form the extension is commonly written with — therefore threw a
ClassCastException inside createAuthorizer, after ReimportApi had already
deleted the previous routes, integrations and authorizers.

Normalise it during planning instead: a scalar audience becomes a single-element
list, a list is copied element-wise, and any other shape (or a non-string issuer)
is a BadRequestException raised before anything is deleted.

This was the last unvalidated field in authorizerSpecs; type, identitySource,
result-TTL caching and jwtConfiguration are now all resolved in the planning
phase, so no authorizer defect can reach the mutating half.

---------

Co-authored-by: local <local@example.com>
2026-08-30 17:16:25 -04:00
..
2026-03-15 17:43:56 -05:00