Comments (6)
We use the APIs provided by dataclasses
, msgspec
, and pydantic
to parse the details of a model such as the default. Both, msgspec
and dataclasses
, do not support specifying the field
within the annotation but rather, you have to assign it whereas pydantic does (I think it does).
Also, I don't think Parameter
is meant to be used for the request/response bodies. They're meant to be used to give extra details for parameters like query params, path params etc.
from litestar.
So how should I specify defaults and other OpenAPI data, then, for those cases? Quite puzzled at this point.
from litestar.
It seems I was mistaken since the default isn't being generated as part of the schema even when specifying the default value correctly.
from dataclasses import dataclass
from litestar import post
from litestar.app import Litestar
from litestar.openapi.spec import Schema
@dataclass
class DataclassBody:
field4: str = "dummy"
@post("/2")
async def dataclass_handler(data: DataclassBody) -> None:
...
app = Litestar([dataclass_handler])
schema = app.openapi_schema.components.schemas["DataclassBody"]
assert schema.properties
schema = schema.properties["field4"]
assert isinstance(schema, Schema)
assert schema.default == "dummy" # This fails.
from litestar.
There's a few things going on here (some of which have also been discussed elsewhere I think):
@dataclass
class DataclassBody:
field1: Annotated[str, Parameter(default="dummy")] # default generated, but declared as required
field3: str = Parameter(default="dummy")
field4: str = "dummy"
field5: str = dataclasses.field(default="dummy")
field1
is simply an invalid way of defining a default for a dataclass. You are essentially "lying" to Litestar by telling it "even though I have not defined a default for this field, I want you to say that it has a default value of"dummy"
".field3
is setting a default value for the field, but, according to dataclass semantics, the default value isParameter(default="dummy")
. Since this is neither an annotation, nor the default value you meant to specify, it's not surprising this isn't workingfield4
andfield5
should work, and it not working is a bug (and I think a regression, will have to check this out)
class MsgspecBody(Struct):
field1: Annotated[str, Parameter(default="dummy")] # default generated, but declared as required
field2: Annotated[str, msgspec.field(default="dummy")] # no default, marked as required
field3: str = Parameter(default="dummy")
field4: str = "dummy"
field5: str = msgspec.field(default="dummy")
field1
/field2
/field3
: Same as with the dataclass, this is not a valid way to define a default way for a msgspec.Structfield4
/field5
: Also same as with the dataclass, this should work
It's important to keep in mind that while Litestar does allow to extend modelling capabilities of the chosen libraries, it does not alter them. You still have to adhere to the semantics and patterns these libraries use and produce a valid model for Litestar to use.
from litestar.
This issue has been closed in #3285. The change will be included in the upcoming patch release.
from litestar.
A fix for this issue has been released in v2.8.0
from litestar.
Related Issues (20)
- DTO backend does not transferring mappings with nested models HOT 9
- 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 3
- 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`
- Docs: Migrating to Litestar from Django HOT 1
- Decoupled, transactional communication of domain events and commands
- Docs: Support for domain driven designs
- Enhancement: Repository pattern interfaces HOT 1
- Enhancement: Kubernetes application health endpoints HOT 1
- Enhancement: Allow status code categories in route handlers responses
- Enhancement: Provide option to set DTOs for nested fields HOT 7
- Bug: mounted app path interferes with regular paths HOT 4
- Examples not shown when using DTO HOT 1
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.