Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(openapi): enable direct list input in Examples model #5318

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 2 commits into from
Oct 7, 2024

Conversation

leandrodamascena
Copy link
Contributor

@leandrodamascena leandrodamascena commented Oct 5, 2024

Issue number: #4476

Summary

Changes

This PR enhances the examples field in OpenAPI schema to accept a more flexible types of inputs. Currently, we enforce the use of a List of Example models, but this change will allow for a simpler and more versatile approach, aligning with OpenAPI specifications.

User experience

from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from pydantic import AnyUrl, BaseModel, ConfigDict, Field
from aws_lambda_powertools.event_handler.openapi.models import (
    Example,
)

app = APIGatewayRestResolver(enable_validation=True)

app.enable_swagger(path="/swagger")

class Pagination(BaseModel):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    
-  next: AnyUrl = Field(..., examples=[Example(value="http://www.example.com/some/path?cursor=SW0gYW4gb3BhcXVlIGN1cnNvcg==")])
+  next: AnyUrl = Field(..., examples=["http://www.example.com/some/path?cursor=SW0gYW4gb3BhcXVlIGN1cnNvcg=="])
    """
    The next page uri
    """
    current: int
    """
    The current page number
    """
    total: int
    """
    The total number of pages
    """

@app.get("/hello")
def hello() -> Pagination:
    ...

def lambda_handler(event, context):
    print(app.get_openapi_json_schema())
    return app.resolve(event, context)

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.

@leandrodamascena leandrodamascena requested a review from a team October 5, 2024 23:45
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Oct 5, 2024
@github-actions github-actions bot added the feature New feature or functionality label Oct 5, 2024
Copy link

codecov bot commented Oct 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.20%. Comparing base (7de63ba) to head (cc73955).
Report is 3 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #5318   +/-   ##
========================================
  Coverage    96.20%   96.20%           
========================================
  Files          229      229           
  Lines        10753    10753           
  Branches      2002     2002           
========================================
  Hits         10345    10345           
  Misses         322      322           
  Partials        86       86           

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

Copy link

sonarqubecloud bot commented Oct 7, 2024

@leandrodamascena leandrodamascena merged commit ece20f8 into develop Oct 7, 2024
12 checks passed
@leandrodamascena leandrodamascena deleted the refactor/openapi-example branch October 7, 2024 09:16
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.

Cannot use models generated by datamodel-codegen from OpenAPI 3.0 spec with property-level example:s
2 participants