Code Monkey home page Code Monkey logo

Comments (5)

rubenfonseca avatar rubenfonseca commented on August 9, 2024 1

Pasting the comment here:

Hi @MCR2019 this makes sense, FastAPI supports it this way, so we could work on adding support in Powertools too. I'm converting this to a Feature Request ticket!

from powertools-lambda-python.

rubenfonseca avatar rubenfonseca commented on August 9, 2024

@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())

from powertools-lambda-python.

MCR2019 avatar MCR2019 commented on August 9, 2024

Thanks for the work around @rubenfonseca :)

from powertools-lambda-python.

github-actions avatar github-actions commented on August 9, 2024

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

from powertools-lambda-python.

github-actions avatar github-actions commented on August 9, 2024

This is now released under 2.32.0 version!

from powertools-lambda-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.