Skip to content

fix: add examples below application/json in openapi schema file#4572

Open
Kumzy wants to merge 7 commits intomainfrom
4558-bug-examples-not-generated-when-added-to-a-body
Open

fix: add examples below application/json in openapi schema file#4572
Kumzy wants to merge 7 commits intomainfrom
4558-bug-examples-not-generated-when-added-to-a-body

Conversation

@Kumzy
Copy link
Copy Markdown
Member

@Kumzy Kumzy commented Feb 2, 2026

Closes

#4558

Running the following file, examples are correctly setup regarding openapi documentation, they are visible using /schema endpoint.

from __future__ import annotations

from typing import Annotated, Any

import uvicorn

from litestar import Litestar, post
from litestar.openapi.config import OpenAPIConfig
from litestar.openapi.spec import Example
from litestar.params import Body


@post("/items")
async def create_item(
    data: Annotated[
        dict[str, Any],
        Body(
            examples=[
                Example(summary="Example A", value={"name": "Widget"}),
                Example(id="custom-id", summary="Example B", value={"name": "Gadget"}),
            ]
        ),
    ],
) -> dict[str, Any]:
    return data


app = Litestar(
    route_handlers=[create_item],
    openapi_config=OpenAPIConfig(title="Test API", version="1.0.0"),
)

if __name__ == "__main__":
    uvicorn.run("test:app", reload=True)

📚 Documentation preview 📚: https://litestar-org.github.io/litestar-docs-preview/4572

@Kumzy Kumzy linked an issue Feb 2, 2026 that may be closed by this pull request
4 tasks
@github-actions github-actions bot added area/openapi This PR involves changes to the OpenAPI schema area/private-api This PR involves changes to the privatized API size: small type/bug pr/external Triage Required 🏥 This requires triage labels Feb 2, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.87%. Comparing base (9587ba1) to head (b64b772).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4572   +/-   ##
=======================================
  Coverage   97.87%   97.87%           
=======================================
  Files         292      292           
  Lines       15077    15081    +4     
  Branches     1703     1704    +1     
=======================================
+ Hits        14757    14761    +4     
  Misses        184      184           
  Partials      136      136           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Kumzy Kumzy marked this pull request as ready for review March 11, 2026 07:49
@Kumzy Kumzy requested review from a team as code owners March 11, 2026 07:49
"""

examples: dict[str, Example | Reference] | None = None
examples: Mapping[str, Example | Reference] | None = None
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change technically, if you disallow .update in users' typing. It is not a big deal, but I would keep this annotation unchaged if possible :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/openapi This PR involves changes to the OpenAPI schema area/private-api This PR involves changes to the privatized API pr/external size: small Triage Required 🏥 This requires triage type/bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: examples not generated when added to a Body

2 participants