docs: clarify that BinaryMediaTypes also affects request payloads - #3966
docs: clarify that BinaryMediaTypes also affects request payloads#3966roger-zhangg wants to merge 1 commit into
Conversation
Reported in aws#3794. The BinaryMediaTypes description says "List of MIME types that your API could return", which reads as response-only. In practice the list applies in both directions: when an incoming request's Content-Type matches an entry, API Gateway base64-encodes the request body and sets isBase64Encoded to true on the event handed to the function. That wording led a customer to file aws#3794 as a suspected SAM bug after adding `BinaryMediaTypes: ["application/json"]` and finding their request bodies base64-encoded. SAM only passes the property through to the RestApi resource and the OpenAPI document, so the behaviour is API Gateway's -- the defect is the documentation. Updates sam-docs.json and regenerates sam.schema.json and schema.json. The text appears twice in the generated schemas because BinaryMediaTypes is declared on both AWS::Serverless::Api Properties and Globals, which share the sam-resource-api docs stem.
| "ApiKeySourceType": "The source of the API key for metering requests according to a usage plan. Valid values are `HEADER` and `AUTHORIZER`. \n*Type*: String \n*Required*: No \n*CloudFormation compatibility*: This property is passed directly to the [`ApiKeySourceType`](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-restapi.html#cfn-apigateway-restapi-apikeysourcetype) property of an `AWS::ApiGateway::RestApi` resource.", | ||
| "Auth": "Configure authorization to control access to your API Gateway API. \nFor more information about configuring access using AWS SAM see [Control API access with your AWS SAM template](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-controlling-access-to-apis.html). For an example showing how to override a global authorizer, see [Override a global authorizer for your Amazon API Gateway REST API](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-apifunctionauth.html#sam-property-function-apifunctionauth--examples--override). \n*Type*: [ApiAuth](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-api-apiauth.html) \n*Required*: No \n*CloudFormation compatibility*: This property is unique to AWS SAM and doesn't have an CloudFormation equivalent.", | ||
| "BinaryMediaTypes": "List of MIME types that your API could return. Use this to enable binary support for APIs. \n*Type*: List \n*Required*: No \n*CloudFormation compatibility*: This property is similar to the [`BinaryMediaTypes`](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-restapi.html#cfn-apigateway-restapi-binarymediatypes) property of an `AWS::ApiGateway::RestApi` resource. The list of BinaryMediaTypes is added to both the CloudFormation resource and the OpenAPI document.", | ||
| "BinaryMediaTypes": "List of MIME types that your API could return, and that it accepts in request payloads. Use this to enable binary support for APIs. This list applies to both directions: when an incoming request's `Content-Type` matches an entry, API Gateway base64-encodes the request body and sets `isBase64Encoded` to `true` on the event passed to your function. \n*Type*: List \n*Required*: No \n*CloudFormation compatibility*: This property is similar to the [`BinaryMediaTypes`](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-restapi.html#cfn-apigateway-restapi-binarymediatypes) property of an `AWS::ApiGateway::RestApi` resource. The list of BinaryMediaTypes is added to both the CloudFormation resource and the OpenAPI document.", |
There was a problem hiding this comment.
[GENERAL] The added sentence states the request-side behavior unconditionally, but base64-encoding of the request body and the isBase64Encoded flag are specific to Lambda proxy integration (AWS_PROXY). AWS::Serverless::Api also supports non-proxy Lambda integrations, HTTP/HTTP_PROXY, MOCK, and arbitrary integrations supplied through DefinitionBody — in those cases there is no Lambda event and no isBase64Encoded field, and conversion depends on the integration's contentHandling setting.
Two concrete misreadings the current wording invites:
- "MIME types ... that it accepts in request payloads" can read as an allow-list, implying request content types absent from the list are rejected. They aren't — they're just not treated as binary.
- "the event passed to your function" implies every API in this list is Lambda-backed with a proxy integration.
Since the point of this change is to prevent a wrong inference (the one behind #3794), it's worth scoping the claim rather than trading one ambiguity for another. Suggested wording:
List of MIME types that your API treats as binary, for both request and response payloads. Use this to enable binary support for APIs. When an incoming request's Content-Type matches an entry, API Gateway treats the body as binary; for Lambda proxy integrations this means the body is base64-encoded and isBase64Encoded is set to true on the event passed to your function. Request content types that are not in the list are still accepted, but are not treated as binary.
Note this string only needs editing here — the two occurrences in schema_source/sam.schema.json and samtranslator/schema/schema.json are regenerated from this entry by make schema.
Issue #, if available
Relates to #3794
Description of changes
The
BinaryMediaTypesdescription currently reads:"could return" reads as response-only. In practice the list applies in both directions: when an incoming request's
Content-Typematches an entry, API Gateway base64-encodes the request body and setsisBase64Encodedtotrueon the event handed to the function.That wording is what led to #3794 — a customer added
BinaryMediaTypes: ["application/json"], found their request bodies base64-encoded, and reasonably filed it as a suspected SAM bug. SAM only passes the property through to theAWS::ApiGateway::RestApiresource and tox-amazon-apigateway-binary-media-typesin the OpenAPI document (samtranslator/swagger/swagger.py:add_binary_media_types), so there is no request/response asymmetry SAM could introduce. The behaviour is API Gateway's; the defect is the documentation.New text:
Docs-only — no transform behaviour changes.
Description of how you validated changes
sam-docs.jsonand regenerated the schemas withmake schema; confirmed themake lintstaleness check (diff -uagainst a fresh generation) passes for bothschema_source/sam.schema.jsonandsamtranslator/schema/schema.json.BinaryMediaTypesis declared on bothAWS::Serverless::ApiProperties(aws_serverless_api.py:280) andGlobals(:393), which share thesam-resource-apidocs stem. Verified both picked up the new wording.tests/schema+tests/translatorpass (2928 tests). No test asserted the old string.sam-docs.json, 2 lines in each generated schema.Checklist
Note: this fixes the schema/IDE tooltip text that lives in this repo. If the published developer guide page is authored separately rather than built from
sam-docs.json, a parallel docs-team change will be needed for the website to match — happy to be pointed at the right place if so.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.