axnsan12 / drf-yasg

Automated generation of real Swagger/OpenAPI 2.0 schemas from Django REST Framework code.
https://drf-yasg.readthedocs.io/en/stable/
Other
3.42k stars 439 forks source link

You should set `default_version` to a string and not an integer. #384

Open ricardocamposilva opened 5 years ago

ricardocamposilva commented 5 years ago

You should set default_version to a string and not an integer.

Originally posted by @axnsan12 in https://github.com/axnsan12/drf-yasg/issues/375#issuecomment-501496456

from django.conf import settings
from django.contrib import admin
from django.urls import include, path, re_path
from django.conf.urls.static import static
from django.views.generic import RedirectView
from django.contrib.staticfiles.views import serve
from rest_framework.permissions import AllowAny
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from api import utils
# utils.apply_monkeypatches()

handler404 = utils.custom404
handler500 = utils.custom500
schema_view = get_schema_view(
    openapi.Info(
        title='G.AI.A API',
        default_version='v1.1',
        description='API documentation for G.AI.A',
        terms_of_service="https://www.google.com/policies/terms/",
        contact=openapi.Contact(email='ricardo.campos@indico.net.br'),
    ),
    public=True,
    validators=['ssv'],
    permission_classes=(AllowAny,)
)

urlpatterns = [
    path('ws/accounts/', include('django.contrib.auth.urls')),
    path('ws/admin/', admin.site.urls),
    path('ws/api-auth/', include('rest_framework.urls', namespace='drf')),
    path('ws/docs/swagger', schema_view.with_ui('swagger'), name='schema-swagger-ui'),
    path('ws/docs/redoc', schema_view.with_ui('redoc'), name='schema-redoc'),
    path('ws/docs', RedirectView.as_view(url='redoc')),
    path('health/', include('watchman.urls')),
    path('ws/anymail/', include('anymail.urls')),
    path('ws/api/', include('api.urls')),
    path('ws/bot/', include('bot.urls')),
    re_path('.*', serve, kwargs={'path': 'index.html', 'insecure': True}),

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Still getting: SwaggerValidationError at /ws/docs/swagger spec validation failed: {'ssv': '("1 is not of type \'string\'\\n\\nFailed validating \'type\' in schema[\'properties\'][\'info\'][\'properties\'][\'version\']:\\n {\'description\': \'A semantic version number of the API.\',\\n \'type\': \'string\'}\\n\\nOn instance[\'info\'][\'version\']:\\n 1", <ValidationError: "1 is not of type \'string\'">)'}

if you want more detailed traceback follows below:

Traceback (most recent call last):
  File "/home/ricardo/Documents/projects/CAOS/gaiabmg-app/.venv/lib/python3.6/site-packages/drf_yasg/generators.py", line 94, in get_api_endpoints
    path = self.replace_version(path, callback)
  File "/home/ricardo/Documents/projects/CAOS/gaiabmg-app/.venv/lib/python3.6/site-packages/drf_yasg/generators.py", line 69, in replace_version
    path = path.replace(version_param, version)
TypeError: replace() argument 2 must be str, not int
PanchoPuncho commented 5 years ago

Same problem here. I use semantic_version in my django project and it expects a string, so the swagger page will not render after an Internal Server Error is thrown. The error states, "TypeError: expected string or bytes-like object".