litestar icon indicating copy to clipboard operation
litestar copied to clipboard

Bug: Examples not appearing in OpenAPI request

Open robswc opened this issue 1 year ago • 10 comments

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

  1. Build app
  2. 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.
Fund with Polar

robswc avatar Feb 06 '24 00:02 robswc

I think your MCVE got cut off :) and is missing some imports

JacobCoffee avatar Feb 06 '24 01:02 JacobCoffee

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 avatar Feb 06 '24 02:02 robswc

@robswc Can you describe what output you're expecting and what you're actually getting?

provinzkraut avatar Feb 06 '24 17:02 provinzkraut

@robswc Can you describe what output you're expecting and what you're actually getting?

Will do!

image

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.

robswc avatar Feb 06 '24 22:02 robswc

Also, for a bit more context, it looks like arguments passed to Body show on swagger.

image

Body(title="test", description="this comes through", ...

robswc avatar Feb 06 '24 22:02 robswc

The reason for this is twofold.

  1. 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.
  2. The examples need to be stored as part of the content-type for 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).

guacs avatar Feb 11 '24 13:02 guacs

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!

robswc avatar Feb 11 '24 17:02 robswc

Unfortunately, I don't think it's possible at the moment.

guacs avatar Feb 12 '24 14:02 guacs

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 avatar Feb 12 '24 22:02 robswc

@robswc If you want to create a PR go for it, and if you need feedback/assistance let us know here or on discord!

provinzkraut avatar Feb 13 '24 11:02 provinzkraut