Skip to content

Commit

Permalink
add expressive error in case of misconfiguration #156
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Sep 23, 2020
1 parent 21de8c6 commit 53f4204
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion drf_spectacular/generators.py
Expand Up @@ -7,6 +7,7 @@
from rest_framework.schemas.generators import EndpointEnumerator as BaseEndpointEnumerator

from drf_spectacular.extensions import OpenApiViewExtension
from drf_spectacular.openapi import AutoSchema
from drf_spectacular.plumbing import (
ComponentRegistry, alpha_operation_sorter, build_root_object, camelize_operation, error,
is_versioning_supported, modify_for_versioning, normalize_result_object,
Expand Down Expand Up @@ -149,7 +150,11 @@ def parse(self, request, public):
if not version or not operation_matches_version(view, version):
continue

# beware that every access to schema yields a fresh object (descriptor pattern)
assert isinstance(view.schema, AutoSchema), (
f'Incompatible AutoSchema used on View. Is DRF\'s DEFAULT_SCHEMA_CLASS '
f'pointing to "drf_spectacular.openapi.AutoSchema" or any other drf-spectacular '
f'compatible AutoSchema?'
)
operation = view.schema.get_operation(path, path_regex, method, self.registry)

# operation was manually removed via @extend_schema
Expand Down
16 changes: 16 additions & 0 deletions tests/test_warnings.py
@@ -1,8 +1,12 @@
from unittest import mock

import pytest
from django.db import models
from django.urls import path
from rest_framework import mixins, serializers, views, viewsets
from rest_framework.authentication import BaseAuthentication
from rest_framework.decorators import action, api_view
from rest_framework.schemas import AutoSchema as DRFAutoSchema
from rest_framework.views import APIView

from drf_spectacular.generators import SchemaGenerator
Expand Down Expand Up @@ -219,3 +223,15 @@ def view_func(request, format=None):
assert schema['paths']['/pi/']['get']['operationId'] == 'pi_retrieve'
assert schema['paths']['/pi/{foo}']['get']['operationId'] == 'pi_retrieve_2'
assert 'operationId "pi_retrieve" has collisions' in capsys.readouterr().err


@mock.patch('rest_framework.settings.api_settings.DEFAULT_SCHEMA_CLASS', DRFAutoSchema)
def test_compatible_auto_schema_class_on_view(no_warnings):
@extend_schema(responses=OpenApiTypes.FLOAT)
@api_view(['GET'])
def view_func(request):
pass # pragma: no cover

with pytest.raises(AssertionError) as excinfo:
generate_schema('/x/', view_function=view_func)
assert "Incompatible AutoSchema" in str(excinfo.value)

0 comments on commit 53f4204

Please sign in to comment.