vitalik / django-ninja

💨 Fast, Async-ready, Openapi, type hints based framework for building APIs
https://django-ninja.dev
MIT License
7.23k stars 429 forks source link

[BUG] Converter `uuid` is already registered - deprecated in django 6.0 #1266

Open mathiasertl opened 2 months ago

mathiasertl commented 2 months ago

Describe the bug

When running python -Wd ca/manage.py check I get a deprecation warning (paths trimmed for privacy):

user@host:~/src/...$ python -Wd ca/manage.py check
.../lib/python3.12/site-packages/ninja/signature/utils.py:96: RemovedInDjango60Warning: Converter 'uuid' is already registered. Support for overriding registered converters is deprecated and will be removed in Django 6.0.
  register_converter(NinjaUUIDConverter, "uuid")

django-ninja registers a converter called uuid, which is already registered by Django. Support for overriding a converter will be removed in Django 6.0. This converter is also problematic as it alters the behavior of views unrelated to django-ninja: A standard Django view using the uuid converter will receive a different type of value depending on if django-ninja is used or not - this was also already raised in #280.

From the looks of it, I think the converter can be safely removed. If not, it has to be renamed. Please advice on your preferred course of action, I'm happy to make a PR.

Versions (please complete the following information):

Xdynix commented 2 months ago

Sharing a monkeypatch I used to suppress this behavior. The behavior of the uuid converter has always been to return a UUID instance since Django 2.0. I don't really think it should behave differently in Django Ninja than the int converter (i.e. returning str instead of the actual type).

Add the following code to your settings.py.

def monkeypatch_ninja_uuid_converter() -> None:
    import importlib
    import sys

    import django.urls

    module_name = "ninja.signature.utils"
    sys.modules.pop(module_name, None)

    original_register_converter = django.urls.register_converter

    def fake_register_converter(*_: Any, **__: Any) -> None:
        pass

    django.urls.register_converter = fake_register_converter
    importlib.import_module(module_name)

    django.urls.register_converter = original_register_converter

monkeypatch_ninja_uuid_converter()