Skip to content

feat(event_handler): use custom serializer during openapi serialization #3900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 8, 2024

Conversation

rubenfonseca
Copy link
Contributor

Issue number: #3892

Summary

Changes

Please provide a summary of what's being changed

This PR modifies the OpenAPI validation middleware to use the any custom serialized provided by the user on the event handler, when the normal serialization mechanisms fail.

This allows you to use your own custom serializer when returning custom objects
from the handlers.

User experience

Please share what the user experience looks like before and after this change

def test_openapi_serialize_other(gw_event):
    # GIVEN a custom serializer
    def serializer(_):
        return "hello world"

    # GIVEN APIGatewayRestResolver is initialized with enable_validation=True and the custom serializer
    app = APIGatewayRestResolver(enable_validation=True, serializer=serializer)

    # GIVEN a custom class
    class CustomClass(object):
        __slots__ = []

    # GIVEN a handler that returns an instance of that class
    @app.get("/my/path")
    def handler():
        return CustomClass()

    # WHEN we invoke the handler
    response = app(gw_event, {})

    # THEN we the custom serializer should be used
    assert response["body"] == "hello world"

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.

@rubenfonseca rubenfonseca requested a review from a team March 7, 2024 13:53
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Mar 7, 2024
@rubenfonseca rubenfonseca linked an issue Mar 7, 2024 that may be closed by this pull request
@github-actions github-actions bot added the feature New feature or functionality label Mar 7, 2024
sthulb
sthulb previously approved these changes Mar 7, 2024
@rubenfonseca rubenfonseca added the bug Something isn't working label Mar 7, 2024
@github-actions github-actions bot removed the bug Something isn't working label Mar 7, 2024
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.

Hello @rubenfonseca! I just left some small suggestions to improve code maintenance and I would like to hear your opinion on the question I asked.

Thanks, Ruben! This is a great improvement.

Copy link

sonarqubecloud bot commented Mar 8, 2024

Quality Gate Passed Quality Gate passed

Issues
1 New issue
0 Accepted issues

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

See analysis details on SonarCloud

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.

APPROVED!!

@leandrodamascena leandrodamascena merged commit 8765206 into develop Mar 8, 2024
@leandrodamascena leandrodamascena deleted the rf/3892 branch March 8, 2024 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event_handlers feature New feature or functionality size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Mongo ObjectIds not serializing out of the box
3 participants