Skip to content

OpenAPI and msgspec #990

Closed
Closed

Description

@Dnnbg123

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.0

OS information

Linux

Activity

  1. sobolevn commented on Apr 28, 2026

    @sobolevn
    Member

    Ok, here's why this happens.

    msgspec:

    "_UserCreateModel": {
      "properties": {
        "tails": {
          "anyOf": [
            {
              "type": "null"
            },
            {
              "$ref": "#/components/schemas/_TailsSchema"
            }
          ]
        },
        // ...
    }

    pydantic:

    "_UserCreateModel": {
      "properties": {
        "tails": {
          "anyOf": [
            {
              "$ref": "#/components/schemas/_TailsSchema"
            },
            {
              "type": "null"
            }
          ]
        },
        // ...
    }

    And swagger just takes the first schema from anyOf to use as an example.

    The fix is ready on our side :)
    Please, wait for the next release to get it.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingopenapi

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions