-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
Enhancement: Support OpenAPI examples defined in Structs, Pydantic models and dataclasses #2849
Comments
Hey @bogdanteleaga, I've re-labelled this as an enhancement, since it's not really a bug; Declaring examples this way is simply not supported at the moment, but it's a reasonable feature to add. |
If I remember correctly this used to work before the rename. Maybe that came with a bigger change than expected, but that's the main reason I considered it to be a bug. Looking at the other issue I still don't see an answer to the initial question: there is no way currently to set examples for the request body? |
That was in version 1.x, so it's considered a breaking change. The reason it used to work is that back then, the schema generation was done by Pydantic, which was good for Pydantic models, but tricky when you wanted to use other libraries such as msgspec or attrs. Now we're doing the schema generation ourselves, which has enabled a lot more features, but there's of course also some drawbacks and room for improvement, which this would fall under.
Sure, you can. You'll simply have to write it out explicitly: from litestar import post, Litestar
from typing import Annotated
from litestar.openapi.spec import Example
from litestar.params import BodyKwarg
from dataclasses import dataclass
@dataclass
class Foo:
bar: str
@post("/")
async def handler(
data: Annotated[
Foo,
BodyKwarg(
examples=[
Example(summary="This is how to use the body", value="some string")
]
),
]
) -> None:
pass |
Thank you for this example, but if |
@bogdanteleaga You can set e.g. |
So the example above rewritten like this @dataclass
class Foo:
foo: str
bar: str
@post("/")
async def handler(
data: Annotated[
Foo,
BodyKwarg(
examples=[
Example(
summary="This is how to use the body",
value={"foo": "a", "bar": "b"},
)
]
),
]
) -> None:
pass Results in this within the swagger UI
|
A fix for this issue has been released in v2.7.1 |
Description
Examples provided on json parameter fields do not get rendered in the Swagger UI.
The
/schema/openapi.json
does get generated similarly to #2660, but it seems this does not result in the examples being actually rendered in the Swagger UI.I included examples for defining the classes with any of pydantic, msgspec and dataclass.
URL to code causing the issue
No response
MCVE
Steps to reproduce
Screenshots
"![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"
Logs
No response
Litestar Version
2.4.2
Platform
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 Litestar dashboard
The text was updated successfully, but these errors were encountered: