Comments (5)
You need to set create_examples=True
on the application itself; It is off by default.
from litestar import Litestar
from litestar.openapi import OpenAPIConfig
app = Litestar(
openapi_config=OpenAPIConfig(
create_examples=True,
title="OpenAPI",
version="1.0.0",
),
)
from litestar.
Thanks. Just couldn't find it, docs don't mention it (one more thing to add there...)
Though, I have an actual application (ie. not MCVE) that I'm converting, and it doesn't have create_examples=True
set, yet it generates those examples. So, there must be bug/glitch in there somewhere.
I will debug more (probably tomorrow) and report back.
from litestar.
@provinzkraut Okay, traced it out. This one's actually quite funny. In my actual app, I had annotated the response type as dict[str, Any]
and not as Response
(temporarily, for laziness) as the return type of the endpoint, like:
from typing import Any
import uvicorn
from litestar import Litestar, post
from litestar.openapi import ResponseSpec
from pydantic import BaseModel
class Response(BaseModel):
text: str
num: int
@post("/", responses={201: ResponseSpec(Response)})
async def endpoint() -> dict[str, Any]: # <---- HERE
return {"text": "hello", "num": 1}
app = Litestar(
route_handlers=[endpoint],
)
uvicorn.run(app)
This leads to Litestar _generating examples. Annotating the function return type with the Pydantic model, it does not (as create_examples=False
).
My usage here is wrong (annotating a different type than in @post(responses=...)
). However, I think the validation could be stricter, perhaps so that this would rather raise, then proceed with unexpected(?) behavior. What do you think?
from litestar.
Just to confirm, this only happens when you include a ResponseSpec
?
from litestar.
Yes. It reproduces with dict[str, Any]
as response type and with ResponseSpec
defined.
With dict[str, Any]
and without ResponseSpec
the whole component is missing from the schema (probably as supposed):
{
"info": {
"title": "Litestar API",
"version": "1.0.0"
},
"openapi": "3.1.0",
"servers": [
{
"url": "/"
}
],
"paths": {
"/": {
"post": {
"summary": "Endpoint",
"operationId": "Endpoint",
"responses": {
"201": {
"description": "Document created, URL follows",
"headers": {},
"content": {
"application/json": {
"schema": {
"additionalProperties": {},
"type": "object"
}
}
}
}
},
"deprecated": false
}
}
},
"components": {
"schemas": {}
}
}
from litestar.
Related Issues (20)
- Docs: Update security usage examples to use SQLAlchemy v2 `mapped_column`
- Check: MiniJinja v2 breaking changes HOT 2
- Docs: Add common recipes/FAQ section HOT 1
- Bug: Litestar instance or factory not found HOT 1
- Enhancement: allow finer tuning `LoggingConfig` for exception logging
- Bug: SQLAlchemy models with default=func.now() error with Unexpected default type HOT 3
- Bug: [OpenAPI] Upload file is not possible when defined as Optional HOT 2
- Bug: Using plugin results in DTO given to dto parameter not being used for return HOT 2
- CI: Fix devcontainer config HOT 1
- DTO backend does not transferring mappings with nested models HOT 10
- Bug: SerializationException when running modeling-and-features demo from docs HOT 3
- Bug: Can't convert sqlalchemy model to pydantic model which is inherited from BaseModel HOT 1
- Enhancement: Add Pydantic's error dictionary to ValidationException's extra dict
- Enhancement: make `SessionMiddleware` return custom types HOT 3
- Bug: OpenAPI docs fail to generate with certain validators and pydantic v1 HOT 4
- Bug: Multi-body response incompatible with LoggingMiddleware HOT 2
- Bug: test failures
- Bug: `return_dto` is silently ignored if return data type does not match DTO definition HOT 1
- Bug: Test error
- Enhancement: add `default_json_serializer` to `__all__` in `litestar.logging.config`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from litestar.