Skip to content

Conversation

amin-farjadi
Copy link
Contributor

@amin-farjadi amin-farjadi commented Mar 25, 2025

Issue number: #6245

Summary

Adds custom http code for when response validation fails per route.

Changes

  • Added custom_response_validation_http_code argument for Route
  • Added said argument to route method in Router and APIGatewayResolver
  • Added _validate_route_response_validation_error_http_code method to APIGatewayResolver
  • Added has_route_custom_response_validation argument to the _serialize_response method in OpenAPIValidationMiddleware
  • Added response_validation_error_response_definition to open api types

User experience

After the merge of #6189, users could add custom response validation error http code at the app level

from aws_lambda_powertools.event_handler import APIGatewayRestResolver

app = APIGatewayRestResolver(enable_validation=True, response_validation_error_http_code=500)

Now, users can customise the response validation http code at route level as well as at app level

from aws_lambda_powertools.event_handler import APIGatewayRestResolver

app = APIGatewayRestResolver(enable_validation=True, response_validation_error_http_code=500)

@app.get(
    "/response_validation_error_with_422",
    custom_response_validation_http_code=422,
)
def handler() -> list:
    return {"body": "hi"}

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@amin-farjadi amin-farjadi requested a review from a team as a code owner March 25, 2025 08:55
@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation event_handlers tests labels Mar 25, 2025
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Mar 25, 2025
@leandrodamascena leandrodamascena linked an issue Mar 25, 2025 that may be closed by this pull request
2 tasks
@github-actions github-actions bot added feature New feature or functionality and removed documentation Improvements or additions to documentation labels Mar 25, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Mar 25, 2025
@amin-farjadi
Copy link
Contributor Author

n.b. tests/functional/metrics/datadog/test_metrics_datadog.py::test_datadog_disable_write_to_log_with_env_variable fails in local machine which I believe is expected

@leandrodamascena
Copy link
Contributor

n.b. tests/functional/metrics/datadog/test_metrics_datadog.py::test_datadog_disable_write_to_log_with_env_variable fails in local machine which I believe is expected

Try to run make dev and it will install a dev dependency that fix this.

Copy link

codecov bot commented Mar 25, 2025

Codecov Report

Attention: Patch coverage is 97.56098% with 1 line in your changes missing coverage. Please review.

Project coverage is 96.33%. Comparing base (54648da) to head (9e04a63).
Report is 2 commits behind head on develop.

Files with missing lines Patch % Lines
aws_lambda_powertools/event_handler/api_gateway.py 97.05% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #6341   +/-   ##
========================================
  Coverage    96.33%   96.33%           
========================================
  Files          243      243           
  Lines        11763    11797   +34     
  Branches       871      878    +7     
========================================
+ Hits         11332    11365   +33     
  Misses         337      337           
- Partials        94       95    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@leandrodamascena
Copy link
Contributor

Looking at this now.

Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

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

Hi @amin-farjadi! Super thanks for this another PR. It took me a few more days than I expected to review this PR because I had some internal priorities.

Overall this PR is pretty good, but there is 1 critical item that needs to be fixed before I can do a final review and merge.

Please let me know if you need any help.

@amin-farjadi
Copy link
Contributor Author

@leandrodamascena I think it's all done. appreciate a review.

p.s. found a lil bug whilst testing the openapi schema — #6435

Copy link

sonarqubecloud bot commented Apr 10, 2025

Quality Gate Passed Quality Gate passed

Issues
0 New issues
5 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
4.2% Duplication on New Code

See analysis details on SonarQube Cloud

Copy link
Contributor

@anafalcao anafalcao left a comment

Choose a reason for hiding this comment

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

approved!

Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

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

Hey @amin-farjadi! Thanks a lot for another super nice PR and addressing all the comments!

APPROVED!!

@leandrodamascena leandrodamascena merged commit 89eb55b into aws-powertools:develop Apr 10, 2025
12 checks passed
leandrodamascena added a commit that referenced this pull request Apr 24, 2025
…enAPI utility (#6341)

* feat(api-gateway-resolver): Add option for custom response validation error status code.

* feat(docs): Added doc for custom response validation error responses.

* feat(unit-test): Add tests for custom response validation error.

* fix: Formatting.

* fix(unit-test): fix failed CI.

* feat(unit-test): add tests for incorrect types and invalid configs

* refactor: rename response_validation_error_http_status to response_validation_error_http_code

* refactor(tests): move unit tests into openapi_validation functional test file

* feat: add route-specific custom response validation and tests

* fix: except Route implementation

* fix: put custom_response_validation_http_code before middleware

* feat: route's custom response validation must take precedence over app's.

* feat: added more tests.

* refactor: improved error messagee and tests' descriptions.

* feat: updated docs.

* move veritifcation method of route custom http code to BaseRouter.

* fix: add validate function for route http code to APIGatewayResolver not Router

* feat: add custom_response_validation_http_code to the routes of Bedrock

* fix: make mypy happy

* fix: address comments

* fix(openapi): add response for response validation error and definition for it

* minor changes

* minor changes

---------

Co-authored-by: Amin Farjadi <[email protected]>
Co-authored-by: Leandro Damascena <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation event_handlers feature New feature or functionality size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Custom response validation status code on routes
3 participants