TypeError at /api/schema Object of type 'str' is not JSON serializable
Malakarakesh1993 opened this issue · 1 comments
Getting serializer error when accessing default api schema view.
Here's the url definition:
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView, SpectacularRedocView
urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/schema', SpectacularAPIView.as_view(), name='schema'),
And the rest is just the default library class-based views.
But trying to access api/v1/schema
throws Object of type 'str' is not JSON serializable
error and the str references to migration files upon searching in the codebase.
Here's the field that might be the cause:
('timezone', timezone_field.fields.TimeZoneField(choices=[('Africa/Abidjan', 'Africa/Abidjan'), ('Africa/Accra', 'Africa/Accra')], default='America/Los_Angeles')),
And here's what I managed to get from traceback:
<DstTzInfo 'Africa/Abidjan' LMT-1 day, 23:44:00 STD>
<json.encoder.JSONEncoder object at 0x7fe2fafed950>
Any idea on how can I resolve this issue?
Hi,
But trying to access api/v1/schema throws Object of type 'str' is not JSON serializable error and the str references to migration files upon searching in the codebase.
where does it say "str" is not serializable
? because your error message looks very much like a timezone
object.
It appears that somehow a timezone object found its way into the schema.
The JSON serializer is the same as DRF, so it serializes everything that DRF is able to.
It agree that this is likely due the the custom timezone field: https://github.com/mfogel/django-timezone-field/blob/4207dc0210d0771a3d2d3193a50a4ac59f38c12a/timezone_field/rest_framework.py#L8C7-L8C30
You may need to write a field extension for it and convert that timezone object to a actual str: