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

Feature request: Enhance OpenAPIResponse with other fields from OpenAPI specification #4870

Open
1 of 2 tasks
tlinhart opened this issue Aug 2, 2024 · 4 comments
Open
1 of 2 tasks
Labels
event_handlers feature-request feature request help wanted Could use a second pair of eyes/hands openapi-schema

Comments

@tlinhart
Copy link
Contributor

tlinhart commented Aug 2, 2024

Use case

I use an API Gateway event handler with validation enabled and I'd like to document various responses my endpoint can return. I can add responses parameter to my endpoint definition, however it's not ideal:

As OpenAPIResponse is a TypedDict, I can actually provide a dict with additional fields, but in that case it shows type errors in the IDE (VS Code in my case) which I have to suppress with # type: ignore. Here's an example:

@app.get(
    "/foo",
    responses={
        200: {
            "description": "Successful Response",
            "content": {
                "application/json": {
                    "schema": {"$ref": "#/components/schemas/FooModel"},
                    "examples": {
                        "example1": {...}
                    }  # type: ignore
                }
            }
        }
    }
)
def foo() -> FooModel:
    ...

Also, note that when I want to add examples to the content value, I have to provide schema and rely on the fact that the response model would be added to the OpenAPI schema automatically due to the endpoint function return value annotation. I can't use model because in that case the examples wouldn't be present in the generated OpenAPI schema (see here).

The issue was previously discussed on Discord – https://discord.com/channels/1006478942305263677/1006527338621710376/1266007500336005120.

Solution/User Experience

The proposed user experience can be seen from the following example:

@app.get(
    "/foo",
    responses={
        200: {
            "description": "Successful Response",
            "headers": {...},
            "content": {
                "application/json": {
                    "model": FooModel,
                    "examples": {
                        "example1": {...}
                    }
                }
            }
        }
    }
)
def foo() -> FooModel:
    ...

To summarize:

  • I could use any field supported by the OpenAPI specification for the response object.
  • I could use a model field with my response's Pydantic model and it would generate the correct OpenAPI schema while retaining other fields supported by the OpenAPI specification for media type object.

Alternative solutions

No response

Acknowledgment

@tlinhart tlinhart added feature-request feature request triage Pending triage from maintainers labels Aug 2, 2024
Copy link

boring-cyborg bot commented Aug 2, 2024

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@leandrodamascena
Copy link
Contributor

Hey @tlinhart! Thanks a lot for opening this issue! I'm adding this to our backlog and expect to work on this early next month.

@leandrodamascena leandrodamascena self-assigned this Aug 5, 2024
@leandrodamascena leandrodamascena added event_handlers openapi-schema and removed triage Pending triage from maintainers labels Aug 5, 2024
@anafalcao anafalcao moved this from Triage to Backlog in Powertools for AWS Lambda (Python) Jan 28, 2025
@anafalcao
Copy link
Contributor

Hey @tlinhart ! We've added this issue on our backlog, and we'll try to work on this in the next quarter.
If you or anyone would like to submit a PR for this, please do :)

@anafalcao anafalcao added the help wanted Could use a second pair of eyes/hands label Jan 28, 2025
@tlinhart
Copy link
Contributor Author

Hi @anafalcao, thanks for the good news! I'm afraid I won't have a spare capacity personally, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event_handlers feature-request feature request help wanted Could use a second pair of eyes/hands openapi-schema
Projects
Status: Backlog
Development

No branches or pull requests

3 participants