litestar
                                
                                 litestar copied to clipboard
                                
                                    litestar copied to clipboard
                            
                            
                            
                        Bug: Examples not appearing in OpenAPI request
Description
This could be user error. Trying to add examples to the request body. The body has no problem with generating the schema and "example value" based on the structure but I can't seem to get the "examples" working.
Much prefer having the examples decoupled from the model lib, so if I could get this working it would be awesome!
Thanks!
URL to code causing the issue
No response
MCVE
"""Minimal Litestar application."""
from __future__ import annotations
from asyncio import sleep
from dataclasses import dataclass
from typing import Annotated
from litestar import Litestar, post
__all__ = ("async_hello_world")
from litestar.openapi.spec import Example
from litestar.params import Body
@dataclass
class User:
    """User dataclass."""
    name: str
    age: int
@post("/async")
async def async_hello_world(
        data: Annotated[User, Body(title="test", examples=[Example(value={
            "test": "test"})])]
) -> User:  # noqa:
    # UP006
    """Route Handler that outputs hello world."""
    await sleep(0.1)
    return data
app = Litestar(route_handlers=[async_hello_world])
Steps to reproduce
- Build app
- Navigate to /schemas/swagger
Screenshots
No response
Logs
No response
Litestar Version
2.5.5
Platform
- [X] Linux
- [ ] Mac
- [ ] Windows
- [ ] Other (Please specify in the description above)
[!NOTE]
While we are open for sponsoring on GitHub Sponsors and OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.Check out all issues funded or available for funding on our Polar.sh dashboard
- If you would like to see an issue prioritized, make a pledge towards it!
- We receive the pledge once the issue is completed & verified
- This, along with engagement in the community, helps us know which features are a priority to our users.
I think your MCVE got cut off :) and is missing some imports
That's fair! Here is a app.py I created, I'll also edit the original post.
"""Minimal Litestar application."""
from __future__ import annotations
from asyncio import sleep
from dataclasses import dataclass
from typing import Annotated
from litestar import Litestar, post
__all__ = ("async_hello_world")
from litestar.openapi.spec import Example
from litestar.params import Body
@dataclass
class User:
    """User dataclass."""
    name: str
    age: int
@post("/async")
async def async_hello_world(
        data: Annotated[User, Body(title="test", examples=[Example(value={
            "test": "test"})])]
) -> User:  # noqa:
    # UP006
    """Route Handler that outputs hello world."""
    await sleep(0.1)
    return data
app = Litestar(route_handlers=[async_hello_world])
@robswc Can you describe what output you're expecting and what you're actually getting?
@robswc Can you describe what output you're expecting and what you're actually getting?
Will do!
The image is what I'm getting.
What I'm trying to get is {"test": "test"} to show as an example in swagger.  With pydantic/FastAPI I'm able to set json_schema_extra and examples to get those to pull through.
The Body seems to take an examples argument but I'm not sure if I'm using that correctly.
Also, for a bit more context, it looks like arguments passed to Body show on swagger.
Body(title="test", description="this comes through", ...
The reason for this is twofold.
- We aren't checking if examples are provided for any of the supported complex types i.e. dataclasses, structs or typeddicts. We are also not creating any examples if they have not been provided, but the user has asked to generate them.
- The examples need to be stored as part of the content-typefor them to show for request bodies and not just in the schema as seen here. If providing examples in the schema is enough, then I'm not sure how the schema should be changed (it would be better to check how other frameworks like FastAPI is generating the schema).
Thanks for the detailed reply!
Are there any examples that you're aware of that show how to include examples in the request? Or is that not possible at the moment? I'm still getting familiar with the framework but enjoying it so far!
Unfortunately, I don't think it's possible at the moment.
Do you think it would be worth attempting a PR to add this in? Or would it be better handled by those with more domain knowledge?
@robswc If you want to create a PR go for it, and if you need feedback/assistance let us know here or on discord!