-
Notifications
You must be signed in to change notification settings - Fork 401
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: ability to define additional response models in OpenAPI #3542
Comments
Pasting the comment here:
|
@MCR2019 I'm still trying to figure out what the best UX is for this feature, but in the meantime, in case you are blocked by this, here's a workaround: from typing import Union
from pydantic import BaseModel, Field
from aws_lambda_powertools.event_handler import APIGatewayHttpResolver, Response
from aws_lambda_powertools.event_handler.openapi.types import COMPONENT_REF_PREFIX
app = APIGatewayHttpResolver(enable_validation=True)
class User(BaseModel):
id_: int = Field(..., alias="id")
name: str
class Order(BaseModel):
order_id: int
@app.get(
"/",
responses={
200: {
"description": "Hello 200",
"content": {"application/json": {"schema": {"$ref": COMPONENT_REF_PREFIX + "User"}}},
},
404: {
"description": "Hello 404",
"content": {"application/json": {"schema": {"$ref": COMPONENT_REF_PREFIX + "Order"}}},
},
},
)
def put(alternative_return: bool) -> Response[Union[User, Order]]:
if alternative_return:
return Response(404, body=Order(order_id=1))
else:
return Response(200, body=User(id=3, name="John"))
print(app.get_openapi_json_schema()) |
Thanks for the work around @rubenfonseca :) |
|
This is now released under 2.32.0 version! |
Discussed in #3540
Originally posted by MCR2019 December 19, 2023
Really excited about the quite new powertools openAPI spec generation so thank to all those who've worked on it and those working on it now :)
I'm trying to get it working for an endpoint and following the documentation on how to specify responses here:
Customizing API Operations docs
Specifically I'd like to add custom reponses which the documentation shows as:
I'd really like to be able to specify the content/schema of the response, maybe as a Pydantic Model which would then generate the response schema in the openAPI spec. (in the same way that the function response type is used).
At the moment I'm only able to get it to work with a basic string which doesn't represent the actual return type/structure.
Is there any more info on what the intention is for how this should be used with a fuller example?
I'm aware that this functionality is quite new so I'm not sure if this is something already on the to do list?
Any info would be great, and thanks again!
The text was updated successfully, but these errors were encountered: