Skip to content

Bug: Body openapi_examples should be included in openapi schema #7860

@facu-01

Description

@facu-01

Expected Behaviour

The openapi_examples from Body, should be included in the openapi schema, under examples.

@app.post("/todos")
def create_todo(todo: Annotated[Todo, Body(
    openapi_examples={
    "example1": {
        "summary": "A simple todo example",
        "description": "A simple todo example with title and completed status",
        "value": {
            "title": "Buy groceries",
            "completed": False
        }
    },
    "example2": {
        "summary": "A tagged todo example",
        "description": "A todo example with a tag",
        "value": {
            "title": "Read a book",
            "completed": True,
            "tag": "leisure"
        }}
    })]
    ) -> str:  
    
    return "test" 

openapi schema:

        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Todo"
              },
              "examples": {
                "example1": {
                  "summary": "A simple todo example",
                  "description": "A simple todo example with title and completed status",
                  "value": {
                    "title": "Buy groceries",
                    "completed": false
                  }
                },
                "example2": {
                  "summary": "A tagged todo example",
                  "description": "A todo example with a tag",
                  "value": {
                    "title": "Read a book",
                    "completed": true,
                    "tag": "leisure"
                  }
                }
              }
            }
          },
          "required": true
        },

Current Behaviour

The openapi_examples gets ignored,

Code snippet

from typing import Optional, Annotated
from pydantic import BaseModel
from aws_lambda_powertools.event_handler.openapi.params import Body
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.utilities.typing import LambdaContext

app = APIGatewayRestResolver(enable_validation=True)  


class Todo(BaseModel):  
    title: str
    completed: bool
    tag: Optional[str] = None


@app.post("/todos")
def create_todo(todo: Annotated[Todo, Body(
    openapi_examples={
    "example1": {
        "summary": "A simple todo example",
        "description": "A simple todo example with title and completed status",
        "value": {
            "title": "Buy groceries",
            "completed": False
        }
    },
    "example2": {
        "summary": "A tagged todo example",
        "description": "A todo example with a tag",
        "value": {
            "title": "Read a book",
            "completed": True,
            "tag": "leisure"
        }}
    })]
    ) -> str:  
    
    return "test"  


def lambda_handler(event: dict, context: LambdaContext) -> dict:
    return app.resolve(event, context)



if __name__ == "__main__":
    # generate openapi.json

    openapi_schema = app.get_openapi_json_schema()

    with open("openapi.json", "w") as f:
        f.write(openapi_schema)

Possible Solution

Modify the Body class and Route class to handle the openapi_examples.

Steps to Reproduce

Run python.

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.13

Packaging format used

PyPi

Debugging logs

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

Status

Coming soon

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions