Skip to content
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

feat(event_handler): add ability to expose a Swagger UI #3254

Merged
merged 15 commits into from
Nov 6, 2023

Conversation

rubenfonseca
Copy link
Contributor

@rubenfonseca rubenfonseca commented Oct 25, 2023

Issue number: #3253

Summary

Changes

Please provide a summary of what's being changed

This PR adds the ability to expose a Swagger UI from the AWS Lambda. It works with all supported REST gateways.

In addition:

  • it adds support for omitting routes from the OpenAPI spec (include_in_schema)
  • adds a lot of coverage
  • fixes some bugs encountered with improving coverage

User experience

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

from aws_lambda_powertools import Logger
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.utilities.typing import LambdaContext

logger = Logger()
app = APIGatewayRestResolver(enable_validation=True)
app.enable_swagger(path="/swagger")


@app.get("/hello_get")
def get_openapi():
    return "hello_get"

@app.post("/hello_post")
def post_openapi():
    return "hello_post"

# You can continue to use other utilities just as before
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST, log_event=True)
def lambda_handler(event: dict, context: LambdaContext) -> dict:
    return app.resolve(event, context)

image

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.

@boring-cyborg boring-cyborg bot added commons dependencies Pull requests that update a dependency file event_handlers tests labels Oct 25, 2023
@pull-request-size pull-request-size bot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Oct 25, 2023
@rubenfonseca rubenfonseca linked an issue Oct 25, 2023 that may be closed by this pull request
2 tasks
@rubenfonseca rubenfonseca self-assigned this Oct 25, 2023
@pull-request-size pull-request-size bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Oct 25, 2023
@codecov-commenter
Copy link

codecov-commenter commented Oct 25, 2023

Codecov Report

Attention: 8 lines in your changes are missing coverage. Please review.

Comparison is base (cec5249) 95.14% compared to head (1e03073) 95.24%.
Report is 1 commits behind head on develop.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #3254      +/-   ##
===========================================
+ Coverage    95.14%   95.24%   +0.09%     
===========================================
  Files          205      207       +2     
  Lines         9517     9581      +64     
  Branches      1742      798     -944     
===========================================
+ Hits          9055     9125      +70     
+ Misses         342      338       -4     
+ Partials       120      118       -2     
Files Coverage Δ
...ls/event_handler/middlewares/openapi_validation.py 87.20% <100.00%> (+3.72%) ⬆️
...mbda_powertools/event_handler/openapi/constants.py 100.00% <100.00%> (ø)
...owertools/event_handler/openapi/swagger_ui/html.py 100.00% <100.00%> (ø)
aws_lambda_powertools/event_handler/vpc_lattice.py 100.00% <100.00%> (ø)
aws_lambda_powertools/utilities/parameters/ssm.py 97.60% <100.00%> (ø)
...da_powertools/event_handler/lambda_function_url.py 84.61% <60.00%> (-15.39%) ⬇️
..._lambda_powertools/event_handler/openapi/models.py 86.93% <77.77%> (ø)
aws_lambda_powertools/event_handler/api_gateway.py 97.65% <92.00%> (+1.19%) ⬆️

... and 1 file with indirect coverage changes

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

@pull-request-size pull-request-size bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Oct 25, 2023
@rubenfonseca rubenfonseca marked this pull request as ready for review October 25, 2023 15:54
@rubenfonseca rubenfonseca requested a review from a team as a code owner October 25, 2023 15:54
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.

I left some comments that we need to change to improve the developer experience and fix small issues.

This is incredible and will work perfectly even locally after these changes.

image

image

aws_lambda_powertools/event_handler/api_gateway.py Outdated Show resolved Hide resolved
aws_lambda_powertools/event_handler/api_gateway.py Outdated Show resolved Hide resolved
aws_lambda_powertools/event_handler/api_gateway.py Outdated Show resolved Hide resolved
aws_lambda_powertools/event_handler/api_gateway.py Outdated Show resolved Hide resolved
aws_lambda_powertools/event_handler/api_gateway.py Outdated Show resolved Hide resolved
aws_lambda_powertools/event_handler/api_gateway.py Outdated Show resolved Hide resolved
aws_lambda_powertools/event_handler/api_gateway.py Outdated Show resolved Hide resolved
@rubenfonseca
Copy link
Contributor Author

@leandrodamascena addressed your comments, answered the pending questions and re-requested a review

@heitorlessa
Copy link
Contributor

Quick one for now: Add the constraint to the linked issue that this is not yet for customers using micro-functions. This is only useful for customers with a single Lambda with many routes.

@guywilsonjr
Copy link

I'm currently managing a combination of a hodge podge of solutions to achieve this functionality and this feature would be greatly appreciated by my team :)

@leandrodamascena respectfully, are there any remaining issues here?

@leandrodamascena
Copy link
Contributor

I'm currently managing a combination of a hodge podge of solutions to achieve this functionality and this feature would be greatly appreciated by my team :)

@leandrodamascena respectfully, are there any remaining issues here?

Hi @guywilsonjr! It's amazing to know that this feature will make your life easier when working with OpenAPI Spec + Swagger. There are no critical issues here and we hope to merge this PR early next week and release it soon.

Copy link

sonarqubecloud bot commented Nov 6, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 4 Code Smells

No Coverage information No Coverage information
2.4% 2.4% Duplication

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.

Just a final comment:

Running this new code with enable_validation=False, means we don´t bring Pydantic.

image

Running this new code with enable_validation=True, which means we bring in Pydantic+ assets but have no impact because of the assets, only because Pydantic, what is expected.

image

@leandrodamascena
Copy link
Contributor

APPROVED @rubenfonseca! 🚀

@leandrodamascena leandrodamascena merged commit 8a09adc into develop Nov 6, 2023
14 checks passed
@leandrodamascena leandrodamascena deleted the rf/openapi-swagger branch November 6, 2023 17:23
@heitorlessa heitorlessa added this to the OpenAPI in Event Handler milestone Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
commons dependencies Pull requests that update a dependency file event_handlers size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Add ability to expose a Swagger UI on the event handler
5 participants