Repository navigation
Closed
Labels
Description
What's wrong?
Hi.
I'm using a modified code from the example , but I'm using msgspec instead of pydantic.
import secrets
import sys
import uuid
from typing import Optional
import msgspec
from django.conf import settings
from django.core.management import execute_from_command_line
from django.urls import include
from dmr import Body, Controller
from dmr.openapi import build_schema
from dmr.openapi.views import OpenAPIJsonView, SwaggerView
from dmr.plugins.msgspec import MsgspecSerializer
from dmr.routing import Router, path
if not settings.configured:
settings.configure(
ROOT_URLCONF=__name__,
ALLOWED_HOSTS="*",
DEBUG=True,
INSTALLED_APPS=["dmr", "django.contrib.staticfiles"],
STATIC_URL="/static/",
STATICFILES_FINDERS=[
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
],
TEMPLATES=[
{
"APP_DIRS": True,
"BACKEND": "django.template.backends.django.DjangoTemplates",
},
],
# Secret key for tests, will be new on each run,
# in production it must be the same token, kept in secret: SECRET_KEY=secrets.token_hex(),
)
class TailsSchema(msgspec.Struct):
id: int
name: str
class UserCreateModel(msgspec.Struct):
email: str
tail: Optional[TailsSchema] = None
tail_optional: TailsSchema | None = None
class UserController(Controller[MsgspecSerializer]):
async def post(
self,
parsed_body: Body[UserCreateModel],
) -> UserCreateModel:
return UserCreateModel(
email=parsed_body.email,
tail=TailsSchema(id=1, name="pony"),
)
router = Router(
"api/",
[
path("user/", UserController.as_view(), name="users"),
],
)
schema = build_schema(router)
urlpatterns = [
path(router.prefix, include((router.urls, "your_app"), namespace="api")),
path("docs/openapi.json/", OpenAPIJsonView.as_view(schema), name="openapi"),
path("docs/swagger/", SwaggerView.as_view(schema), name="swagger"),
]
if __name__ == "__main__":
# Use `python THIS_FILE_NAME.py runserver` to run the example.
# Then visit `http://localhost:8000/docs/swagger` to view the docs.
execute_from_command_line(sys.argv)The following is visible in swagger
{
"email": "string",
"tail": null,
"tail_optional": null
}if I use pydantic, swagger displays everything correctly.
Maybe I'm doing something wrong...
How it should be?
expected in swagger (exemple from pydantic)
{
"email": "string",
"tail": {
"id": 0,
"name": "string"
},
"tail_optional": {
"id": 0,
"name": "string"
}
}Used versions
django-modern-rest[msgspec,openapi]==0.7.0OS information
Linux
Activity
sobolevn commentedon Apr 28, 2026
Ok, here's why this happens.
msgspec:pydantic:And
swaggerjust takes the first schema fromanyOfto use as an example.The fix is ready on our side :)
Please, wait for the next release to get it.
Fix how `msgspec` generates `anyOf` examples, refs #990
Fix how `msgspec` generates `anyOf` examples (#991)