Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

feat(cloudformation, apigw-v1) Partially support OpenAPI specs when creating REST API in CloudFormation (#928) (#965)

maximooseminedd5825d6a841
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d0f34716..6d3a9dee 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,13 @@ All notable changes to MiniStack will be documented here.
Format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
Versioning follows [Semantic Versioning](https://semver.org/).
+## [Unreleased]
+
+### Added
+- **CloudFormation / API Gateway — `AWS::ApiGateway::RestApi` partially supports the OpenAPI `Body` property** — This implementation is very incomplete. See #928.
+
+---
+
## [1.3.66] — 2026-06-22
### Added
diff --git a/ministack/services/apigateway_v1.py b/ministack/services/apigateway_v1.py
index a451ccfd..83448965 100644
--- a/ministack/services/apigateway_v1.py
+++ b/ministack/services/apigateway_v1.py
@@ -1247,6 +1247,66 @@ def _delete_rest_api(api_id):
return 202, {}, b""
+# ---- OpenAPI parsing ----
+
+_OPENAPI_HTTP_METHODS = {
+ "get", "post", "put", "delete", "patch", "head", "options",
+}
+
+
+def _import_rest_api(spec, base_data=None):
+ data = dict(base_data or {})
+ info = spec.get("info") or {}
+ if info.get("title") and not data.get("name"):
+ data["name"] = str(info["title"])
+ if info.get("version") and not data.get("version"):
+ data["version"] = str(info["version"])
+
+ _status, _headers, body = _create_rest_api(data)
+ api_id = json.loads(body)["id"]
+ for path, path_item in (spec.get("paths") or {}).items():
+ _import_path_item(api_id, path, path_item)
+ return api_id
+
+
+def _import_path_item(api_id, path, path_item):
+ resource_id = next(
+ rid for rid, res in _resources.get(api_id, {}).items()
+ if res.get("path") == "/"
+ )
+ if path != "/":
+ for segment in path.strip("/").split("/"):
+ child = next(
+ (rid for rid, res in _resources.get(api_id, {}).items()
+ if res.get("parentId") == resource_id
+ and res.get("pathPart") == segment),
+ None,
+ )
+ if child is None:
+ _status, _headers, body = _create_resource(
+ api_id, resource_id, {"pathPart": segment}
+ )
+ child = json.loads(body)["id"]
+ resource_id = child
+
+ for method, operation in path_item.items():
+ if method.lower() in _OPENAPI_HTTP_METHODS:
+ _import_operation(api_id, resource_id, method.upper(), operation)
+
+
+def _import_operation(api_id, resource_id, http_method, operation):
+ _put_method(api_id, resource_id, http_method, {"authorizationType": "NONE"})
+
+ integration = (operation or {}).get("x-amazon-apigateway-integration")
+ if integration:
+ int_type = integration.get("type")
+ _put_integration(api_id, resource_id, http_method, {
+ "type": int_type.upper() if int_type else None,
+ "httpMethod": integration.get("httpMethod"),
+ "uri": integration.get("uri"),
+ })
+
+
# ---- Control plane: Resources ----
def _get_resources(api_id, query_params):
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.py
index fe6f5b0d..facc9836 100644
--- a/ministack/services/cloudformation/provisioners.py
+++ b/ministack/services/cloudformation/provisioners.py
@@ -1677,9 +1677,7 @@ def _custom_resource_delete(physical_id, props, stack_name=None, logical_id=None
# --- API Gateway REST API ---
def _apigw_rest_api_create(logical_id, props, stack_name):
- name = props.get("Name") or _physical_name(stack_name, logical_id, max_len=64)
data = {
- "name": name,
"description": props.get("Description", ""),
"endpointConfiguration": props.get("EndpointConfiguration", {"types": ["REGIONAL"]}),
"binaryMediaTypes": props.get("BinaryMediaTypes", []),
@@ -1687,9 +1685,18 @@ def _apigw_rest_api_create(logical_id, props, stack_name):
"policy": props.get("Policy"),
"tags": {t["Key"]: t["Value"] for t in props.get("Tags", [])},
}
- status, headers, body = _apigw_v1._create_rest_api(data)
- api = json.loads(body) if isinstance(body, bytes) else json.loads(body)
- api_id = api.get("id", "")
+ if props.get("Name"):
+ data["name"] = props["Name"]
+
+ body_spec = props.get("Body")
+ if isinstance(body_spec, dict):
+ api_id = _apigw_v1._import_rest_api(body_spec, data)
+ else:
+ data.setdefault("name", _physical_name(stack_name, logical_id, max_len=64))
+ status, headers, body = _apigw_v1._create_rest_api(data)
+ api = json.loads(body) if isinstance(body, bytes) else json.loads(body)
+ api_id = api.get("id", "")
+
# Find root resource id
root_id = ""
for rid, res in _apigw_v1._resources.get(api_id, {}).items():
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index 7ea57a2f..2082fe89 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -3442,6 +3442,88 @@ def test_cfn_apigateway_account_provisions(cfn, apigw_v1):
_wait_stack(cfn, stack_name)
+# ---------------------------------------------------------------------------
+# ApiGatewayV1 Integration with OpenAPI spec parsing
+# ---------------------------------------------------------------------------
+
+def test_cfn_restapi_openapi_body_petstore(cfn, apigw_v1):
+ stack = "cfn-restapi-body"
+ op = {
+ "x-amazon-apigateway-integration": {
+ "httpMethod": "POST",
+ "type": "aws_proxy",
+ "uri": {
+ "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/"
+ "2015-03-31/functions/${PetStoreFunction.Arn}/invocations"
+ },
+ },
+ "responses": {},
+ }
+ body = {
+ "swagger": "2.0",
+ "info": {"version": "1.0", "title": {"Ref": "AWS::StackName"}},
+ "paths": {
+ "/pets": {"get": dict(op), "post": dict(op)},
+ "/pets/featured": {"get": dict(op)},
+ "/pets/{petId}": {"get": dict(op), "delete": dict(op)},
+ },
+ }
+ template = json.dumps({
+ "AWSTemplateFormatVersion": "2010-09-09",
+ "Resources": {
+ "PetStoreFunction": {
+ "Type": "AWS::Lambda::Function",
+ "Properties": {
+ "FunctionName": f"{stack}-fn",
+ "Runtime": "python3.12",
+ "Handler": "index.handler",
+ "Role": "arn:aws:iam::000000000000:role/r",
+ "Code": {"ZipFile": "def handler(e, c):\n return {}\n"},
+ },
+ },
+ "ServerlessRestApi": {
+ "Type": "AWS::ApiGateway::RestApi",
+ "Properties": {"Body": body},
+ },
+ },
+ "Outputs": {"ApiId": {"Value": {"Ref": "ServerlessRestApi"}}},
+ })
+
+ cfn.create_stack(StackName=stack, TemplateBody=template)
+ s = _wait_stack(cfn, stack)
+ assert s["StackStatus"] == "CREATE_COMPLETE"
+ api_id = {o["OutputKey"]: o["OutputValue"] for o in s["Outputs"]}["ApiId"]
+
+ api = apigw_v1.get_rest_api(restApiId=api_id)
+ assert api["name"] == stack
+ assert api["version"] == "1.0"
+
+ rmap = {}
+ for r in apigw_v1.get_resources(restApiId=api_id, limit=500)["items"]:
+ rmap[r["path"]] = {
+ m: apigw_v1.get_integration(restApiId=api_id, resourceId=r["id"],
+ httpMethod=m)
+ for m in (r.get("resourceMethods") or {})
+ }
+
+ assert set(rmap) == {"/", "/pets", "/pets/featured", "/pets/{petId}"}
+ assert set(rmap["/pets"]) == {"GET", "POST"}
+ assert set(rmap["/pets/featured"]) == {"GET"}
+ assert set(rmap["/pets/{petId}"]) == {"GET", "DELETE"}
+
+ integ = rmap["/pets"]["GET"]
+ assert integ["type"] == "AWS_PROXY"
+ assert integ["httpMethod"] == "POST"
+ assert integ["uri"].startswith("arn:aws:apigateway:")
+ assert "${" not in integ["uri"]
+ assert f":function:{stack}-fn/invocations" in integ["uri"]
+
+ cfn.delete_stack(StackName=stack)
+ _wait_stack(cfn, stack)
+ ids = [a["id"] for a in apigw_v1.get_rest_apis(limit=500)["items"]]
+ assert api_id not in ids
+
+
# ============================================================================
# Nested Stacks (AWS::CloudFormation::Stack)
# ============================================================================