Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions rest_framework/schemas/coreapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ def insert_into(target, keys, value):
target = target[key]

try:
if len(keys) == 1:
target[keys[-1]] = LinkNode()
target = target[keys[-1]]
target.links.append((keys[-1], value))
except TypeError:
msg = INSERT_INTO_COLLISION_FMT.format(
Expand Down
19 changes: 10 additions & 9 deletions tests/schemas/test_coreapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1203,24 +1203,25 @@ def test_from_router(self):
schema = generator.get_schema()

# not important here
desc_0 = schema['detail']['detail_export'].description
desc_1 = schema['detail_0'].description
desc_0 = schema['detail']['detail'].description
desc_1 = schema['detail']['detail_export'].description

expected = coreapi.Document(
url='',
title='Naming Colisions',
content={
'detail': {
'detail': coreapi.Link(
url='/from-routercollision/detail/',
action='get',
description=desc_0
),
'detail_export': coreapi.Link(
url='/from-routercollision/detail/export/',
action='get',
description=desc_0)
},
'detail_0': coreapi.Link(
url='/from-routercollision/detail/',
action='get',
description=desc_1
)
description=desc_1
)
}
}
)

Expand Down
108 changes: 108 additions & 0 deletions tests/schemas/test_schema_with_single_common_prefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import unittest

from django.conf.urls import include, url
from django.test import TestCase, override_settings

from rest_framework import serializers
from rest_framework.compat import coreapi
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.routers import SimpleRouter
from rest_framework.schemas import get_schema_view
from rest_framework.test import APIClient, APIRequestFactory
from rest_framework.viewsets import GenericViewSet

factory = APIRequestFactory()


class MockUser:
def is_authenticated(self):
return True


class ExampleSerializer(serializers.Serializer):
a = serializers.CharField(required=True, help_text='A field description')
b = serializers.CharField(required=False)
read_only = serializers.CharField(read_only=True)
hidden = serializers.HiddenField(default='hello')


class ExampleViewSet(GenericViewSet):
serializer_class = ExampleSerializer

@action(['GET'], detail=False)
def test1(self):
return Response({})

@action(['GET'], detail=False)
def test2(self):
return Response({})


with override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema'}):
if coreapi:
schema_view = get_schema_view(title='Example API')
else:
def schema_view(request):
pass

router = SimpleRouter()
router.register('example', ExampleViewSet, basename='example')
urlpatterns = [
url(r'^$', schema_view),
url(r'^example/', include(router.urls))
]


@unittest.skipUnless(coreapi, 'coreapi is not installed')
@override_settings(ROOT_URLCONF=__name__, REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema'})
class AnotherTestRouterGeneratedSchema(TestCase):
def test_anonymous_request(self):
client = APIClient()
response = client.get('/', HTTP_ACCEPT='application/coreapi+json')
assert response.status_code == 200
expected = coreapi.Document(
url='http://testserver/',
title='Example API',
content={
'test1': {
'test1': coreapi.Link(
url='/example/example/test1/',
action='get'
)
},
'test2': {
'test2': coreapi.Link(
url='/example/example/test2/',
action='get'
)
}
}
)
assert response.data == expected

def test_authenticated_request(self):
client = APIClient()
client.force_authenticate(MockUser())
response = client.get('/', HTTP_ACCEPT='application/coreapi+json')
assert response.status_code == 200

expected = coreapi.Document(
url='http://testserver/',
title='Example API',
content={
'test1': {
'test1': coreapi.Link(
url='/example/example/test1/',
action='get'
)
},
'test2': {
'test2': coreapi.Link(
url='/example/example/test2/',
action='get'
)
}
}
)
assert response.data == expected