Skip to content

Conversation

@abhishektang
Copy link

Description

Fixes #3816

This PR fixes the issue where enabling CORS on an API with paths that differ only by a trailing slash (e.g., /datasets and /datasets/) causes API Gateway to throw an error:

Duplicate method 'OPTIONS' found for resource /datasets. Only one is allowed.

Problem

When CORS is enabled, SAM adds OPTIONS methods to all paths. However, API Gateway treats /path and /path/ as the same resource. If both paths exist in the template, SAM was adding OPTIONS to both separately, causing a duplicate method error.

Solution

The fix normalizes paths before adding CORS OPTIONS methods:

  • Paths are normalized by removing trailing slashes (except for root /)
  • Normalized paths are tracked in a set to skip duplicates
  • Ensures only one OPTIONS method is added per normalized path

Changes

Modified files:

  • samtranslator/model/api/api_generator.py: Added path normalization in _add_cors() method
  • tests/model/api/test_api_generator.py: Added unit test test_add_cors_with_trailing_slash_paths

Testing

Unit Tests

  • Added new test test_add_cors_with_trailing_slash_paths that verifies:
    • Paths /datasets (POST) and /datasets/ (PUT) are correctly handled
    • Only one OPTIONS method is added (not duplicated)
    • Test passes successfully

Integration Tests

  • All 68 existing CORS integration tests pass without regression
  • Verified no breaking changes to existing functionality

Real-world Validation

Related Issues

Closes #3816


Checklist:

  • Fixed on develop branch
  • Added unit test for the fix
  • All tests pass locally
  • Tested with real-world production template
  • No regressions in existing CORS tests
  • Clear commit message
  • Code follows project conventions

Fixes aws#3816

When CORS is enabled on an API with paths that differ only by a trailing
slash (e.g., /datasets and /datasets/), API Gateway would throw an error:
'Duplicate method OPTIONS found for resource /datasets. Only one is allowed.'

This occurs because API Gateway treats /path and /path/ as the same
resource, but SAM was adding OPTIONS methods to both paths separately.

Solution:
- Added path normalization in ApiGenerator._add_cors() method
- Paths are normalized by removing trailing slashes (except for root '/')
- Track normalized paths in a set to skip duplicates
- Ensures only one OPTIONS method is added per normalized path

Testing:
- Added unit test test_add_cors_with_trailing_slash_paths to verify fix
- All existing CORS integration tests (68 tests) pass without regression
- Tested with real-world production templates with multiple HTTP methods
@abhishektang abhishektang requested a review from a team as a code owner November 17, 2025 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Duplicate method 'OPTIONS' error

1 participant