Skip to content

[BUG] TypeError: Object of type HttpUrl is not JSON serializable #1695

@joejoinerr

Description

@joejoinerr

Describe the bug
I am still running into the same issue outlined in #1334, which is marked as fixed. There is another user on that issue that has commented with the same problem.

The pydantic.HttpUrl type cannot be used in Ninja schemas without triggering the error:

Object of type HttpUrl is not JSON serializable
Traceback (most recent call last):
  File "/home/joejoinerr/Dev Projects/test-project/.venv/lib/python3.13/site-packages/ninja/operation.py", line 354, in run
    return self._result_to_response(request, result, temporal_response)
           ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joejoinerr/Dev Projects/test-project/.venv/lib/python3.13/site-packages/ninja/operation.py", line 298, in _result_to_response
    return self.api.create_response(
           ~~~~~~~~~~~~~~~~~~~~~~~~^
        request, result, temporal_response=temporal_response
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/home/joejoinerr/Dev Projects/test-project/.venv/lib/python3.13/site-packages/ninja/main.py", line 469, in create_response
    content = self.renderer.render(request, data, response_status=status)
  File "/home/joejoinerr/Dev Projects/test-project/.venv/lib/python3.13/site-packages/ninja/renderers.py", line 25, in render
    return json.dumps(data, cls=self.encoder_class, **self.json_dumps_params)
           ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/joejoinerr/.local/share/uv/python/cpython-3.13.3-linux-x86_64-gnu/lib/python3.13/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
          ~~~~~~^^^^^
  File "/home/joejoinerr/.local/share/uv/python/cpython-3.13.3-linux-x86_64-gnu/lib/python3.13/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/home/joejoinerr/.local/share/uv/python/cpython-3.13.3-linux-x86_64-gnu/lib/python3.13/json/encoder.py", line 261, in iterencode
    return _iterencode(o, 0)
  File "/home/joejoinerr/Dev Projects/test-project/.venv/lib/python3.13/site-packages/ninja/responses.py", line 31, in default
    return super().default(o)
           ~~~~~~~~~~~~~~~^^^
  File "/home/joejoinerr/Dev Projects/test-project/.venv/lib/python3.13/site-packages/django/core/serializers/json.py", line 113, in default
    return super().default(o)
           ~~~~~~~~~~~~~~~^^^
  File "/home/joejoinerr/.local/share/uv/python/cpython-3.13.3-linux-x86_64-gnu/lib/python3.13/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
                    f'is not JSON serializable')
TypeError: Object of type HttpUrl is not JSON serializable

Versions (please complete the following information):

  • Python version: 3.13
  • Django version: 6.0.3
  • Django-Ninja version: 1.5.3
  • Pydantic version: 2.12.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions