Commit 57f9068
authored
fix(amplify-alpha): handle empty customResponseHeaders array (#35700)
### Issue # (if applicable)
Closes #35693.
### Reason for this change
The `amplify.App` construct fails with a TypeError when `customResponseHeaders` is an empty array, preventing CDK synthesis. This is a regression introduced in v2.202.0 (PR #31771) that breaks applications passing an empty array to this property.
### Description of changes
Added defensive checks to prevent array access on empty `customResponseHeaders` arrays:
1. **Length check** in App constructor (line 319-321): Prevents calling render function with empty arrays by checking length before invocation, ensuring CloudFormation `CustomHeaders` property is properly omitted (undefined)
2. **Defensive assertion** in `renderCustomResponseHeaders` function (line 608-611): Throws clear error if function is called with empty array, catching potential CDK programming bugs
3. **JSDoc documentation** added to clarify function contract and internal nature
4. **Unit test** enhanced with clarifying comments referencing issue #35693
The fix follows CDK defensive programming patterns with a clear separation of concerns:
- **Call site validation** (line 319): Handles user input, ensures proper CloudFormation output
- **Function assertion** (line 608): Catches CDK programming errors with fail-fast behavior
- **Documentation**: Makes the contract explicit for future maintainers
### Description of how you validated changes
- **Unit tests**: Added new test case "with empty custom response headers array" that verifies empty arrays don't cause errors and that the CloudFormation `CustomHeaders` property is correctly absent. All 45 unit tests pass (100%).
- **Integration tests**: All 10 existing integration tests pass with UNCHANGED status, confirming no regression in existing functionality. The `integ.app-monorepo-custom-headers` test specifically validates custom headers behavior remains correct.
- **Manual validation**: Tested the exact reproduction case from issue #35693 - empty array no longer causes TypeError and synthesis completes successfully.
### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)
----
*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*1 parent effa46d commit 57f9068
File tree
2 files changed
+36
-2
lines changed- packages/@aws-cdk/aws-amplify-alpha
- lib
- test
2 files changed
+36
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
316 | 316 | | |
317 | 317 | | |
318 | 318 | | |
319 | | - | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
320 | 322 | | |
321 | 323 | | |
322 | 324 | | |
| |||
594 | 596 | | |
595 | 597 | | |
596 | 598 | | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
597 | 608 | | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
598 | 614 | | |
599 | 615 | | |
600 | 616 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
549 | 549 | | |
550 | 550 | | |
551 | 551 | | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
552 | 570 | | |
553 | 571 | | |
554 | 572 | | |
| |||
0 commit comments