tfranzel / drf-spectacular

Sane and flexible OpenAPI 3 schema generation for Django REST framework.
https://drf-spectacular.readthedocs.io
BSD 3-Clause "New" or "Revised" License
2.21k stars 248 forks source link

Missing staticfiles manifest entry for 'drf_spectacular_sidecar/swagger-ui-dist/swagger-ui.css' #1148

Open hmafzal opened 5 months ago

hmafzal commented 5 months ago

Describe the bug We are trying to enable Swagger UI through Sidecar. SwaggerUI seems to work fine locally, but when we deploy it no longer works. We see the following stack trace:

Missing staticfiles manifest entry for 'drf_spectacular_sidecar/swagger-ui-dist/swagger-ui.css'
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/ddtrace/contrib/django/patch.py", line 213, in wrapped
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 55, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/ddtrace/contrib/trace_utils.py", line 162, in wrapper
    return func(mod, pin, wrapped, instance, args, kwargs)
  File "/usr/local/lib/python3.10/site-packages/ddtrace/contrib/django/patch.py", line 213, in wrapped
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/django/views/generic/base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/ddtrace/contrib/trace_utils.py", line 162, in wrapper
    return func(mod, pin, wrapped, instance, args, kwargs)
  File "/usr/local/lib/python3.10/site-packages/ddtrace/contrib/django/patch.py", line 213, in wrapped
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python3.10/site-packages/ddtrace/contrib/trace_utils.py", line 162, in wrapper
    return func(mod, pin, wrapped, instance, args, kwargs)
  File "/usr/local/lib/python3.10/site-packages/ddtrace/contrib/django/restframework.py", line 19, in _traced_handle_exception
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/local/lib/python3.10/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/usr/local/lib/python3.10/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/ddtrace/contrib/trace_utils.py", line 162, in wrapper
    return func(mod, pin, wrapped, instance, args, kwargs)
  File "/usr/local/lib/python3.10/site-packages/ddtrace/contrib/django/patch.py", line 213, in wrapped
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/drf_spectacular/views.py", line 216, in get
    'swagger_ui_css': self._swagger_ui_resource('swagger-ui.css'),
  File "/usr/local/lib/python3.10/site-packages/drf_spectacular/views.py", line 180, in _swagger_ui_resource
    return _get_sidecar_url(f'swagger-ui-dist/{filename}')
  File "/usr/local/lib/python3.10/site-packages/drf_spectacular/views.py", line 118, in _get_sidecar_url
    return static(f'drf_spectacular_sidecar/{filepath}')
  File "/usr/local/lib/python3.10/site-packages/django/templatetags/static.py", line 179, in static
    return StaticNode.handle_simple(path)
  File "/usr/local/lib/python3.10/site-packages/django/templatetags/static.py", line 129, in handle_simple
    return staticfiles_storage.url(path)
ValueError: Missing staticfiles manifest entry for 'drf_spectacular_sidecar/swagger-ui-dist/swagger-ui.css'

To Reproduce We have strict CSP rules so we are following the FAQ here: https://drf-spectacular.readthedocs.io/en/latest/faq.html#my-swagger-ui-and-or-redoc-page-is-blank.

Using:

drf-spectacular==0.26.1
drf-spectacular-sidecar==2023.12.1

Expected behavior Swagger UI loads correctly in our deployed site.

tfranzel commented 5 months ago

CSP is irrelevant for your issue. Are you absolutely sure you have installed the sidecar package in your deployed container? I strongly suspect you have not, because why would it work for you locally?

try this: python manage.py findstatic --verbosity 2 drf_spectacular_sidecar/swagger-ui-dist/swagger-ui.css, which works fine for me.

hmafzal commented 5 months ago

@tfranzel when running that locally or in the deployed container, it's able to find it:

Found 'drf_spectacular_sidecar/swagger-ui-dist/swagger-ui.css' here:
  /usr/local/lib/python3.10/site-packages/drf_spectacular_sidecar/static/drf_spectacular_sidecar/swagger-ui-dist/swagger-ui.css
  /app/staticfiles/drf_spectacular_sidecar/swagger-ui-dist/swagger-ui.css
Looking in the following locations:
  /app/merge/static
  /usr/local/lib/python3.10/site-packages/rest_framework/static
  /usr/local/lib/python3.10/site-packages/adminsortable/static
  /usr/local/lib/python3.10/site-packages/versatileimagefield/static
  /usr/local/lib/python3.10/site-packages/nested_admin/static
  /usr/local/lib/python3.10/site-packages/drf_spectacular_sidecar/static
  /usr/local/lib/python3.10/site-packages/colorfield/static
  /usr/local/lib/python3.10/site-packages/django_extensions/static
  /usr/local/lib/python3.10/site-packages/django/contrib/admin/static
  /app/staticfiles
tfranzel commented 5 months ago

Sorry to say but it makes no sense. I believe you are comparing the wrong container, that is not really exactly what is deployed (flags?, conditional build?). Can't explain it otherwise, since you have it working locally.

hmafzal commented 5 months ago

@tfranzel I'm fairly certain I'm in the right container. Could there be any other setup steps that are required?

A few other notes, if they're helpful: