Skip to content

APIGW: fix binaryMediaTypes when importing/updating REST APIs #12586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions localstack-core/localstack/services/apigateway/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,15 @@ def export(
self._add_paths(spec, resources, with_extension)
self._add_models(spec, models["items"], "#/definitions")

return getattr(spec, self.export_formats.get(export_format))()
response = getattr(spec, self.export_formats.get(export_format))()
if (
with_extension
and isinstance(response, dict)
and (binary_media_types := rest_api.get("binaryMediaTypes")) is not None
):
response[OpenAPIExt.BINARY_MEDIA_TYPES] = binary_media_types

return response


class _OpenApiOAS30Exporter(_BaseOpenApiExporter):
Expand Down Expand Up @@ -298,8 +306,16 @@ def export(
self._add_models(spec, models["items"], "#/components/schemas")

response = getattr(spec, self.export_formats.get(export_format))()
if isinstance(response, dict) and "components" not in response:
response["components"] = {}
if isinstance(response, dict):
if "components" not in response:
response["components"] = {}

if (
with_extension
and (binary_media_types := rest_api.get("binaryMediaTypes")) is not None
):
response[OpenAPIExt.BINARY_MEDIA_TYPES] = binary_media_types

return response


Expand Down
13 changes: 11 additions & 2 deletions localstack-core/localstack/services/apigateway/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,10 @@ def import_api_from_openapi_spec(
region_name = context.region

# TODO:
# 1. validate the "mode" property of the spec document, "merge" or "overwrite"
# 1. validate the "mode" property of the spec document, "merge" or "overwrite", and properly apply it
# for now, it only considers it for the binaryMediaTypes
# 2. validate the document type, "swagger" or "openapi"
mode = request.get("mode", "merge")

rest_api.version = (
str(version) if (version := resolved_schema.get("info", {}).get("version")) else None
Expand Down Expand Up @@ -948,7 +950,14 @@ def create_method_resource(child, method, method_schema):
get_or_create_path(base_path + path, base_path=base_path)

# binary types
rest_api.binaryMediaTypes = resolved_schema.get(OpenAPIExt.BINARY_MEDIA_TYPES, [])
if mode == "merge":
existing_binary_media_types = rest_api.binaryMediaTypes or []
else:
existing_binary_media_types = []

rest_api.binaryMediaTypes = existing_binary_media_types + resolved_schema.get(
OpenAPIExt.BINARY_MEDIA_TYPES, []
)

policy = resolved_schema.get(OpenAPIExt.POLICY)
if policy:
Expand Down
4 changes: 4 additions & 0 deletions tests/aws/files/pets.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"schemes": [
"https"
],
"x-amazon-apigateway-binary-media-types": [
"image/png",
"image/jpg"
],
"paths": {
"/pets": {
"get": {
Expand Down
1 change: 0 additions & 1 deletion tests/aws/services/apigateway/test_apigateway_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
THIS_FOLDER = os.path.dirname(os.path.realpath(__file__))
TEST_SWAGGER_FILE_JSON = os.path.join(THIS_FOLDER, "../../files/swagger.json")
TEST_SWAGGER_FILE_YAML = os.path.join(THIS_FOLDER, "../../files/swagger.yaml")
TEST_IMPORT_REST_API_FILE = os.path.join(THIS_FOLDER, "../../files/pets.json")
TEST_IMPORT_MOCK_INTEGRATION = os.path.join(THIS_FOLDER, "../../files/openapi-mock.json")
TEST_IMPORT_REST_API_ASYNC_LAMBDA = os.path.join(THIS_FOLDER, "../../files/api_definition.yaml")

Expand Down
16 changes: 14 additions & 2 deletions tests/aws/services/apigateway/test_apigateway_extended.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ def _create(**kwargs):
[TEST_IMPORT_PETSTORE_SWAGGER, TEST_IMPORT_PETS],
ids=["TEST_IMPORT_PETSTORE_SWAGGER", "TEST_IMPORT_PETS"],
)
@markers.snapshot.skip_snapshot_verify(paths=["$..body.host"])
@markers.snapshot.skip_snapshot_verify(
paths=[
"$..body.host",
# TODO: not returned by LS
"$..endpointConfiguration.ipAddressType",
]
)
def test_export_swagger_openapi(aws_client, snapshot, import_apigw, import_file, region_name):
snapshot.add_transformer(
[
Expand Down Expand Up @@ -82,7 +88,13 @@ def test_export_swagger_openapi(aws_client, snapshot, import_apigw, import_file,
[TEST_IMPORT_PETSTORE_SWAGGER, TEST_IMPORT_PETS],
ids=["TEST_IMPORT_PETSTORE_SWAGGER", "TEST_IMPORT_PETS"],
)
@markers.snapshot.skip_snapshot_verify(paths=["$..body.servers..url"])
@markers.snapshot.skip_snapshot_verify(
paths=[
"$..body.servers..url",
# TODO: not returned by LS
"$..endpointConfiguration.ipAddressType",
]
)
def test_export_oas30_openapi(aws_client, snapshot, import_apigw, region_name, import_file):
snapshot.add_transformer(
[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"tests/aws/services/apigateway/test_apigateway_extended.py::test_export_swagger_openapi[TEST_IMPORT_PETSTORE_SWAGGER]": {
"recorded-date": "15-04-2024, 21:43:25",
"recorded-date": "06-05-2025, 18:20:26",
"recorded-content": {
"import-api": {
"apiKeySource": "HEADER",
"createdDate": "datetime",
"description": "Your first API with Amazon API Gateway. This is a sample API that integrates via HTTP with our demo Pet Store endpoints",
"disableExecuteApiEndpoint": false,
"endpointConfiguration": {
"ipAddressType": "ipv4",
"types": [
"EDGE"
]
Expand Down Expand Up @@ -638,13 +639,18 @@
}
},
"tests/aws/services/apigateway/test_apigateway_extended.py::test_export_swagger_openapi[TEST_IMPORT_PETS]": {
"recorded-date": "15-04-2024, 21:43:56",
"recorded-date": "06-05-2025, 18:21:08",
"recorded-content": {
"import-api": {
"apiKeySource": "HEADER",
"binaryMediaTypes": [
"image/png",
"image/jpg"
],
"createdDate": "datetime",
"disableExecuteApiEndpoint": false,
"endpointConfiguration": {
"ipAddressType": "ipv4",
"types": [
"EDGE"
]
Expand Down Expand Up @@ -727,15 +733,15 @@
}
},
"x-amazon-apigateway-integration": {
"type": "http",
"httpMethod": "GET",
"uri": "http://petstore-demo-endpoint.execute-api.com/petstore/pets",
"responses": {
"default": {
"statusCode": "200"
}
},
"passthroughBehavior": "when_no_match",
"type": "http"
"passthroughBehavior": "when_no_match"
}
}
},
Expand All @@ -755,6 +761,7 @@
}
},
"x-amazon-apigateway-integration": {
"type": "http",
"httpMethod": "GET",
"uri": "http://petstore-demo-endpoint.execute-api.com/petstore/pets/{id}",
"responses": {
Expand All @@ -765,12 +772,15 @@
"requestParameters": {
"integration.request.path.id": "method.request.path.petId"
},
"passthroughBehavior": "when_no_match",
"type": "http"
"passthroughBehavior": "when_no_match"
}
}
}
}
},
"x-amazon-apigateway-binary-media-types": [
"image/png",
"image/jpg"
]
},
"contentDisposition": "attachment; filename=\"swagger_1.0.0.json\"",
"contentType": "application/octet-stream",
Expand All @@ -782,14 +792,15 @@
}
},
"tests/aws/services/apigateway/test_apigateway_extended.py::test_export_oas30_openapi[TEST_IMPORT_PETSTORE_SWAGGER]": {
"recorded-date": "15-04-2024, 21:45:03",
"recorded-date": "06-05-2025, 18:34:11",
"recorded-content": {
"import-api": {
"apiKeySource": "HEADER",
"createdDate": "datetime",
"description": "Your first API with Amazon API Gateway. This is a sample API that integrates via HTTP with our demo Pet Store endpoints",
"disableExecuteApiEndpoint": false,
"endpointConfiguration": {
"ipAddressType": "ipv4",
"types": [
"EDGE"
]
Expand Down Expand Up @@ -1140,6 +1151,7 @@
}
},
"x-amazon-apigateway-integration": {
"type": "http",
"httpMethod": "GET",
"uri": "http://petstore.execute-api.<region>.amazonaws.com/petstore/pets",
"responses": {
Expand All @@ -1154,8 +1166,7 @@
"integration.request.querystring.page": "method.request.querystring.page",
"integration.request.querystring.type": "method.request.querystring.type"
},
"passthroughBehavior": "when_no_match",
"type": "http"
"passthroughBehavior": "when_no_match"
}
},
"post": {
Expand Down Expand Up @@ -1190,6 +1201,7 @@
}
},
"x-amazon-apigateway-integration": {
"type": "http",
"httpMethod": "POST",
"uri": "http://petstore.execute-api.<region>.amazonaws.com/petstore/pets",
"responses": {
Expand All @@ -1200,8 +1212,7 @@
}
}
},
"passthroughBehavior": "when_no_match",
"type": "http"
"passthroughBehavior": "when_no_match"
}
},
"options": {
Expand Down Expand Up @@ -1235,6 +1246,7 @@
}
},
"x-amazon-apigateway-integration": {
"type": "mock",
"responses": {
"default": {
"statusCode": "200",
Expand All @@ -1248,8 +1260,7 @@
"requestTemplates": {
"application/json": "{\"statusCode\": 200}"
},
"passthroughBehavior": "when_no_match",
"type": "mock"
"passthroughBehavior": "when_no_match"
}
}
},
Expand Down Expand Up @@ -1286,6 +1297,7 @@
}
},
"x-amazon-apigateway-integration": {
"type": "http",
"httpMethod": "GET",
"uri": "http://petstore.execute-api.<region>.amazonaws.com/petstore/pets/{petId}",
"responses": {
Expand All @@ -1299,8 +1311,7 @@
"requestParameters": {
"integration.request.path.petId": "method.request.path.petId"
},
"passthroughBehavior": "when_no_match",
"type": "http"
"passthroughBehavior": "when_no_match"
}
},
"options": {
Expand Down Expand Up @@ -1344,6 +1355,7 @@
}
},
"x-amazon-apigateway-integration": {
"type": "mock",
"responses": {
"default": {
"statusCode": "200",
Expand All @@ -1357,8 +1369,7 @@
"requestTemplates": {
"application/json": "{\"statusCode\": 200}"
},
"passthroughBehavior": "when_no_match",
"type": "mock"
"passthroughBehavior": "when_no_match"
}
}
},
Expand All @@ -1378,6 +1389,7 @@
}
},
"x-amazon-apigateway-integration": {
"type": "mock",
"responses": {
"default": {
"statusCode": "200",
Expand All @@ -1392,8 +1404,7 @@
"requestTemplates": {
"application/json": "{\"statusCode\": 200}"
},
"passthroughBehavior": "when_no_match",
"type": "mock"
"passthroughBehavior": "when_no_match"
}
}
}
Expand Down Expand Up @@ -1468,13 +1479,18 @@
}
},
"tests/aws/services/apigateway/test_apigateway_extended.py::test_export_oas30_openapi[TEST_IMPORT_PETS]": {
"recorded-date": "15-04-2024, 21:45:07",
"recorded-date": "06-05-2025, 18:34:49",
"recorded-content": {
"import-api": {
"apiKeySource": "HEADER",
"binaryMediaTypes": [
"image/png",
"image/jpg"
],
"createdDate": "datetime",
"disableExecuteApiEndpoint": false,
"endpointConfiguration": {
"ipAddressType": "ipv4",
"types": [
"EDGE"
]
Expand Down Expand Up @@ -1620,7 +1636,11 @@
}
}
},
"components": {}
"components": {},
"x-amazon-apigateway-binary-media-types": [
"image/png",
"image/jpg"
]
},
"contentDisposition": "attachment; filename=\"oas30_1.0.0.json\"",
"contentType": "application/octet-stream",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
"last_validated_date": "2024-10-10T18:54:41+00:00"
},
"tests/aws/services/apigateway/test_apigateway_extended.py::test_export_oas30_openapi[TEST_IMPORT_PETSTORE_SWAGGER]": {
"last_validated_date": "2024-04-15T21:45:02+00:00"
"last_validated_date": "2025-05-06T18:34:11+00:00"
},
"tests/aws/services/apigateway/test_apigateway_extended.py::test_export_oas30_openapi[TEST_IMPORT_PETS]": {
"last_validated_date": "2024-04-15T21:45:04+00:00"
"last_validated_date": "2025-05-06T18:34:17+00:00"
},
"tests/aws/services/apigateway/test_apigateway_extended.py::test_export_swagger_openapi[TEST_IMPORT_PETSTORE_SWAGGER]": {
"last_validated_date": "2024-04-15T21:43:24+00:00"
"last_validated_date": "2025-05-06T18:20:25+00:00"
},
"tests/aws/services/apigateway/test_apigateway_extended.py::test_export_swagger_openapi[TEST_IMPORT_PETS]": {
"last_validated_date": "2024-04-15T21:43:30+00:00"
"last_validated_date": "2025-05-06T18:20:36+00:00"
}
}
Loading
Loading