Skip to content

Conversation

@pahud
Copy link
Contributor

@pahud pahud commented Oct 8, 2025

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 amplify-alpha: custom_response_headers cannot be empty #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 amplify-alpha: custom_response_headers cannot be empty #35693 - empty array no longer causes TypeError and synthesis completes successfully.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

- Add null check for empty custom response headers array in App constructor
- Update renderCustomResponseHeaders function to handle empty array case
- Add test case for empty custom response headers array
- Prevent rendering of CustomHeaders when array is empty
- Improve null/undefined handling for custom response headers configuration
@github-actions github-actions bot added bug This issue is a bug. p0 labels Oct 8, 2025
@aws-cdk-automation aws-cdk-automation requested a review from a team October 8, 2025 15:21
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Oct 8, 2025
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This review is outdated)

@pahud pahud changed the title fix(amplify-alpha): handle empty customResponseHeaders array choreamplify-alpha): handle empty customResponseHeaders array Oct 8, 2025
@pahud pahud changed the title choreamplify-alpha): handle empty customResponseHeaders array chore(amplify-alpha): handle empty customResponseHeaders array Oct 8, 2025
@aws-cdk-automation aws-cdk-automation dismissed their stale review October 8, 2025 15:25

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@pahud pahud changed the title chore(amplify-alpha): handle empty customResponseHeaders array fix(amplify-alpha): handle empty customResponseHeaders array Oct 8, 2025
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter fails with the following errors:

❌ Fixes must contain a change to an integration test file and the resulting snapshot.

If you believe this pull request should receive an exemption, please comment and provide a justification. A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed, add Clarification Request to a comment.

✅ A exemption request has been requested. Please wait for a maintainer's review.

@pahud
Copy link
Contributor Author

pahud commented Oct 8, 2025

Exemption Request

this PR is essentially improving the check and having a unit test covered.

@aws-cdk-automation aws-cdk-automation added the pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. label Oct 8, 2025
pahud added 2 commits October 8, 2025 11:46
- Enhance `renderCustomResponseHeaders` function to handle empty headers array more robustly
- Add defensive error throwing for internal edge case scenarios
- Update test case to validate empty custom headers array handling
- Improve error messaging and internal documentation for header rendering method
- Ensure consistent behavior when no custom headers are provided
Resolves aws#35693 by preventing potential runtime errors and improving error handling in Amplify App configuration.
- Replace generic Error with custom ValidationError in renderCustomResponseHeaders
- Add explicit error scope to improve error tracing and debugging
- Maintain existing validation logic for empty custom headers array
- Enhance error message specificity for internal CDK validation
@aemada-aws aemada-aws added pr-linter/exempt-integ-test The PR linter will not require integ test changes and removed pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. labels Oct 9, 2025
@mergify
Copy link
Contributor

mergify bot commented Oct 9, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot added the queued label Oct 9, 2025
@mergify
Copy link
Contributor

mergify bot commented Oct 9, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 57f9068 into aws:main Oct 9, 2025
19 checks passed
@mergify mergify bot removed the queued label Oct 9, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Oct 9, 2025

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 9, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bug This issue is a bug. contribution/core This is a PR that came from AWS. p0 pr-linter/exempt-integ-test The PR linter will not require integ test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

amplify-alpha: custom_response_headers cannot be empty

3 participants