Conversation
Implements sam generate openapi command to extract OpenAPI/Swagger specs from SAM templates. Features: - Generates OpenAPI from SAM templates using SAM Translator - Supports both Swagger 2.0 and OpenAPI 3.0 output (--openapi-version flag) - YAML and JSON output formats - Auto-detects single API or accepts --api-logical-id for multiple APIs - Outputs to file or stdout - Works with complex templates (tested: 35+ routes, 6 functions) Implementation: - Command: samcli/commands/generate/openapi/ - Library: samcli/lib/generate/openapi_generator.py - Converter: samcli/lib/generate/openapi_converter.py (Swagger 2.0 → OpenAPI 3.0) - Tests: 81 comprehensive unit tests + integration tests Test Coverage: 94.01% (exceeds 94% threshold) Tests Passing: 5,956 tests Addresses: aws#1473
Which issue(s) does this change fix?Fixes #1473 Why is this change necessary?SAM CLI currently generates OpenAPI/Swagger documents automatically at deploy time, but there's no way to access this generated document during the build process. This creates challenges for development teams who want to:
Currently, teams must hand-manage OpenAPI documents separately instead of leveraging SAM's automatic generation capabilities, leading to:
This feature has been requested by the community for over 6 years with strong support from multiple users. How does it address the issue?This PR implements a new Command Structure
Key Features
Architecture
Implementation PhilosophyThe command generates "deployment-faithful" OpenAPI documents that accurately reflect what SAM creates at deploy time:
What side effects does this change have?Positive Side Effects
Potential Considerations
Mandatory Checklist
Test ResultsTesting on Production TemplatesSuccessfully tested on complex production template with:
Open Questions for MaintainersWhile the implementation is complete and tested, I'd appreciate guidance on these architectural decisions before finalizing:
Happy to adjust based on team preferences. The current implementation follows a "deployment-faithful" philosophy but can be enhanced iteratively based on user feedback. Example Usage# Basic usage - output to stdout
sam generate openapi -t template.yaml
# Save to file with specific format
sam generate openapi -t template.yaml -o api.yaml --format yaml
# Generate Swagger 2.0 instead of OpenAPI 3.0
sam generate openapi -t template.yaml --openapi-version 2.0
# Specify API for templates with multiple APIs
sam generate openapi -t template.yaml --api-logical-id MyApi
# JSON output to stdout
sam generate openapi -t template.yaml --format jsonBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. |
GitHub Issue #1473 Comment - OpenAPI Compliance Discussion
Hey SAM CLI team! 👋
I've implemented
sam generate openapifor issue #1473 - it's working and tested (94% coverage, 5,956 tests passing, verified on complex production templates). But I need architectural guidance on 5 OpenAPI compliance decisions before opening the PR.What Works Today
Command successfully extracts OpenAPI from SAM templates via SAM Translator:
Tested on production template: 35 API routes, 6 Lambda functions, Cognito authorization - all generated correctly with proper CORS and security configuration.
The 5 Compliance Gaps (Need Community Direction)
1. CloudFormation Intrinsic Functions
Current Output:
OpenAPI Standard: Plain resolved values
Why It Happens: SAM Translator preserves template structure - doesn't resolve CloudFormation parameters or functions.
The Problem: CloudFormation refs make the spec less usable for swagger-codegen and other OpenAPI tooling.
Options:
--resolve-refsflag for user choice (default: keep refs)My Recommendation: Option C - Default to raw output (accurate to SAM), add flag for resolved version if needed.
2. AWS-Specific Extensions
Current Output:
OpenAPI Standard: No vendor-specific extensions
Why It Happens: These ARE the actual API Gateway deployment configurations that SAM generates. They show exactly how the API will be deployed.
The Problem: Pure OpenAPI specs shouldn't have vendor extensions.
Options:
--strip-aws-extensionsflag for choiceMy Recommendation: Option A - Keep extensions. Reasons:
3. Swagger 2.0 vs OpenAPI 3.0
Current Output: Swagger 2.0 format (what SAM Translator generates)
Why: SAM Translator hasn't migrated to OpenAPI 3.0 yet - still outputs Swagger 2.0.
Current Implementation: Added
--openapi-versionflag--openapi-version 2.0: Raw SAM Translator output (Swagger 2.0)--openapi-version 3.0: Auto-converts to OpenAPI 3.0 (moves securityDefinitions → components.securitySchemes)Default: OpenAPI 3.0 (modern standard)
Question: Is defaulting to 3.0 correct, or should we default to 2.0 (matches SAM Translator output)?
My Recommendation: Keep default as 3.0 - Most tooling prefers OpenAPI 3.0. Users can specify 2.0 if needed.
4. Missing
serversSectionCurrent Output: No servers array
OpenAPI 3.0 Requirement:
Why Missing: API Gateway assigns URL at deployment time. Pre-deployment, the actual URL doesn't exist yet.
The Problem:
https://{apiId}.execute-api.{region}.amazonaws.com/{stage}{apiId}is assigned by CloudFormation at deploy timeOptions:
https://example.com/REPLACE_AFTER_DEPLOYMENThttps://{apiId}.execute-api.{region}.amazonaws.com/{stage}with note--api-urlparameterMy Recommendation: Option C - Add template URL with CloudFormation-style variables. Shows the structure, users can substitute
{apiId}and{stage}after deployment. Example:5. Missing Request/Response Schemas
Current Output:
OpenAPI Standard: Should define detailed schemas
Why Missing: SAM templates define ROUTES but not DATA MODELS. Example SAM template:
SAM supports RequestModel/ResponseModel, but most templates don't use them.
The Real Problem: This is a SAM template limitation, not a command limitation. If schemas aren't in the template, we can't generate them.
Options:
type: object)--infer-schemasflag to attempt inference from Lambda function type hints (complex, fragile)My Recommendation: Option A - Leave empty and document. Reasons:
Summary of Recommendations
--resolve-refsflag later if neededThe Trade-Off
Current approach: Generates "deployment-faithful" OpenAPI
Alternative: "tooling-friendly" OpenAPI
Question: Which philosophy should SAM CLI adopt? The original issue says "use swagger-codegen" but doesn't specify which approach.
What I'm Asking
Branch is ready: https://github.com/dcabib/aws-sam-cli/tree/feat/generate-openapi-command
(Blocked by AWS security hook on unrelated file, but code is committed locally)
Happy to adjust based on team direction! 🚀