first fixes to openapi3 tests

This commit is contained in:
Thorsten Franzel 2019-12-13 11:59:34 +01:00
parent 9486df8d04
commit d24a15f166
2 changed files with 174 additions and 136 deletions

View File

@ -329,7 +329,7 @@ class AutoSchema(ViewInspector):
if isinstance(field, serializers.ManyRelatedField):
return {
'type': 'array',
'items': self._map_field(field.child_relation)
'items': self._map_field(method, field.child_relation)
}
if isinstance(field, serializers.PrimaryKeyRelatedField):
model = getattr(field.queryset, 'model', None)
@ -361,7 +361,7 @@ class AutoSchema(ViewInspector):
'items': {},
}
if not isinstance(field.child, _UnvalidatedField):
map_field = self._map_field(field.child)
map_field = self._map_field(method, field.child)
items = {
"type": map_field.get('type')
}

View File

@ -8,7 +8,7 @@ from rest_framework.compat import uritemplate
from rest_framework.parsers import JSONParser, MultiPartParser
from rest_framework.renderers import JSONRenderer
from rest_framework.request import Request
from rest_framework.schemas.openapi import AutoSchema, SchemaGenerator
from rest_framework.schemas.openapi import AutoSchema, SchemaGenerator, ComponentRegistry
from . import views
@ -57,7 +57,7 @@ class TestFieldMapping(TestCase):
]
for field, mapping in cases:
with self.subTest(field=field):
assert inspector._map_field(field) == mapping
assert inspector._map_field('GET', field) == mapping
def test_lazy_string_field(self):
class Serializer(serializers.Serializer):
@ -65,7 +65,7 @@ class TestFieldMapping(TestCase):
inspector = AutoSchema()
data = inspector._map_serializer(Serializer())
data = inspector._map_serializer('GET', Serializer())
assert isinstance(data['properties']['text']['description'], str), "description must be str"
@ -83,26 +83,32 @@ class TestOperationIntrospection(TestCase):
)
inspector = AutoSchema()
inspector.view = view
inspector.init(ComponentRegistry())
operation = inspector.get_operation(path, method)
assert operation == {
'operationId': 'listDocStringExamples',
'description': 'A description of my GET operation.',
'operationId': 'example_list',
'description': 'get: A description of my GET operation.\npost: A description of my POST operation.',
'parameters': [],
'tags': [''],
'security': [{'cookieAuth': []}, {'basicAuth': []}, {}],
'responses': {
'200': {
'description': '',
'content': {
'application/json': {
'schema': {
'type': 'array',
'items': {},
},
},
'items': {
'type': 'object',
'description': 'Unspecified response body'
}
}
},
},
'description': ''
},
}
}
def test_path_with_id_parameter(self):
path = '/example/{id}/'
@ -114,131 +120,156 @@ class TestOperationIntrospection(TestCase):
create_request(path)
)
inspector = AutoSchema()
inspector.init(ComponentRegistry())
inspector.view = view
operation = inspector.get_operation(path, method)
assert operation == {
'operationId': 'RetrieveDocStringExampleDetail',
'description': 'A description of my GET operation.',
'parameters': [{
'description': '',
'in': 'path',
'operationId': 'example_retrieve',
'description': '\n\nA description of my GET operation.',
'parameters': [
{
'name': 'id',
'in': 'path',
'required': True,
'description': '',
'schema': {
'type': 'string',
},
}],
'type': 'string'
}
}
],
'tags': [''],
'security': [{'cookieAuth': []}, {'basicAuth': []}, {}],
'responses': {
'200': {
'description': '',
'content': {
'application/json': {
'schema': {
'type': 'object',
'description': 'Unspecified response body'
}
}
},
},
},
},
},
'description': ''
}
}
}
def test_request_body(self):
path = '/'
method = 'POST'
class Serializer(serializers.Serializer):
class ExampleSerializer(serializers.Serializer):
text = serializers.CharField()
read_only = serializers.CharField(read_only=True)
class View(generics.GenericAPIView):
serializer_class = Serializer
class View(generics.CreateAPIView):
serializer_class = ExampleSerializer
view = create_view(
View,
method,
create_request(path)
)
registry = ComponentRegistry()
inspector = AutoSchema()
inspector.view = view
inspector.init(registry)
inspector.get_operation(path, method)
request_body = inspector._get_request_body(path, method)
assert request_body['content']['application/json']['schema']['required'] == ['text']
assert list(request_body['content']['application/json']['schema']['properties'].keys()) == ['text']
schema = registry.schemas['Example']
assert schema['required'] == ['text']
assert schema['properties']['read_only']['readOnly'] is True
def test_empty_required(self):
path = '/'
method = 'POST'
class Serializer(serializers.Serializer):
class ExampleSerializer(serializers.Serializer):
read_only = serializers.CharField(read_only=True)
write_only = serializers.CharField(write_only=True, required=False)
class View(generics.GenericAPIView):
serializer_class = Serializer
class View(generics.CreateAPIView):
serializer_class = ExampleSerializer
view = create_view(
View,
method,
create_request(path)
)
registry = ComponentRegistry()
inspector = AutoSchema()
inspector.view = view
inspector.init(registry)
inspector.get_operation(path, method)
request_body = inspector._get_request_body(path, method)
schema = registry.schemas['Example']
# there should be no empty 'required' property, see #6834
assert 'required' not in request_body['content']['application/json']['schema']
for response in inspector._get_responses(path, method).values():
assert 'required' not in response['content']['application/json']['schema']
assert 'required' not in schema
def test_empty_required_with_patch_method(self):
path = '/'
method = 'PATCH'
class Serializer(serializers.Serializer):
class ExampleSerializer(serializers.Serializer):
read_only = serializers.CharField(read_only=True)
write_only = serializers.CharField(write_only=True, required=False)
class View(generics.GenericAPIView):
serializer_class = Serializer
class View(generics.UpdateAPIView):
serializer_class = ExampleSerializer
view = create_view(
View,
method,
create_request(path)
)
registry = ComponentRegistry()
inspector = AutoSchema()
inspector.view = view
inspector.init(registry)
inspector.get_operation(path, method)
request_body = inspector._get_request_body(path, method)
schema = registry.schemas['PatchedExample']
# there should be no empty 'required' property, see #6834
assert 'required' not in request_body['content']['application/json']['schema']
for response in inspector._get_responses(path, method).values():
assert 'required' not in response['content']['application/json']['schema']
assert 'required' not in schema
for field_schema in schema['properties']:
assert 'required' not in field_schema
def test_response_body_generation(self):
path = '/'
method = 'POST'
class Serializer(serializers.Serializer):
class ExampleSerializer(serializers.Serializer):
text = serializers.CharField()
write_only = serializers.CharField(write_only=True)
class View(generics.GenericAPIView):
serializer_class = Serializer
class View(generics.CreateAPIView):
serializer_class = ExampleSerializer
view = create_view(
View,
method,
create_request(path)
)
registry = ComponentRegistry()
inspector = AutoSchema()
inspector.view = view
inspector.init(registry)
responses = inspector._get_responses(path, method)
assert responses['200']['content']['application/json']['schema']['required'] == ['text']
assert list(responses['200']['content']['application/json']['schema']['properties'].keys()) == ['text']
assert 'description' in responses['200']
operation = inspector.get_operation(path, method)
assert operation['responses'] == {
'200': {
'content': {
'application/json': {
'schema': {'$ref': '#/components/schemas/Example'}
}
},
'description': ''
}
}
assert registry.schemas['Example']['required'] == ['text', 'write_only']
assert list(registry.schemas['Example']['properties'].keys()) == ['text', 'write_only']
def test_response_body_nested_serializer(self):
path = '/'
@ -247,28 +278,32 @@ class TestOperationIntrospection(TestCase):
class NestedSerializer(serializers.Serializer):
number = serializers.IntegerField()
class Serializer(serializers.Serializer):
class ExampleSerializer(serializers.Serializer):
text = serializers.CharField()
nested = NestedSerializer()
class View(generics.GenericAPIView):
serializer_class = Serializer
class View(generics.CreateAPIView):
serializer_class = ExampleSerializer
view = create_view(
View,
method,
create_request(path),
)
registry = ComponentRegistry()
inspector = AutoSchema()
inspector.view = view
inspector.init(registry)
responses = inspector._get_responses(path, method)
schema = responses['200']['content']['application/json']['schema']
assert sorted(schema['required']) == ['nested', 'text']
assert sorted(list(schema['properties'].keys())) == ['nested', 'text']
assert schema['properties']['nested']['type'] == 'object'
assert list(schema['properties']['nested']['properties'].keys()) == ['number']
assert schema['properties']['nested']['required'] == ['number']
operation = inspector.get_operation(path, method)
example_schema = registry.schemas['Example']
nested_schema = registry.schemas['Nested']
assert sorted(example_schema['required']) == ['nested', 'text']
assert sorted(list(example_schema['properties'].keys())) == ['nested', 'text']
assert example_schema['properties']['nested']['type'] == 'object'
assert list(nested_schema['properties'].keys()) == ['number']
assert nested_schema['required'] == ['number']
def test_list_response_body_generation(self):
"""Test that an array schema is returned for list views."""
@ -278,7 +313,7 @@ class TestOperationIntrospection(TestCase):
class ItemSerializer(serializers.Serializer):
text = serializers.CharField()
class View(generics.GenericAPIView):
class View(generics.ListAPIView):
serializer_class = ItemSerializer
view = create_view(
@ -286,29 +321,25 @@ class TestOperationIntrospection(TestCase):
method,
create_request(path),
)
registry = ComponentRegistry()
inspector = AutoSchema()
inspector.view = view
inspector.init(registry)
responses = inspector._get_responses(path, method)
assert responses == {
operation = inspector.get_operation(path, method)
assert operation['responses'] == {
'200': {
'description': '',
'content': {
'application/json': {
'schema': {
'type': 'array',
'items': {
'properties': {
'text': {
'type': 'string',
},
},
'required': ['text'],
},
},
},
},
'items': {'$ref': '#/components/schemas/Item'},
}
}
},
'description': ''
}
}
def test_paginated_list_response_body_generation(self):
@ -326,7 +357,7 @@ class TestOperationIntrospection(TestCase):
class ItemSerializer(serializers.Serializer):
text = serializers.CharField()
class View(generics.GenericAPIView):
class View(generics.ListAPIView):
serializer_class = ItemSerializer
pagination_class = Pagination
@ -337,9 +368,10 @@ class TestOperationIntrospection(TestCase):
)
inspector = AutoSchema()
inspector.view = view
inspector.init(ComponentRegistry())
responses = inspector._get_responses(path, method)
assert responses == {
operation = inspector.get_operation(path, method)
assert operation['responses'] == {
'200': {
'description': '',
'content': {
@ -348,14 +380,7 @@ class TestOperationIntrospection(TestCase):
'type': 'object',
'item': {
'type': 'array',
'items': {
'properties': {
'text': {
'type': 'string',
},
},
'required': ['text'],
},
'items': {'$ref': '#/components/schemas/Item'},
},
},
},
@ -378,11 +403,12 @@ class TestOperationIntrospection(TestCase):
)
inspector = AutoSchema()
inspector.view = view
inspector.init(ComponentRegistry())
responses = inspector._get_responses(path, method)
assert responses == {
operation = inspector.get_operation(path, method)
assert operation['responses'] == {
'204': {
'description': '',
'description': 'No response body',
},
}
@ -402,19 +428,20 @@ class TestOperationIntrospection(TestCase):
)
inspector = AutoSchema()
inspector.view = view
inspector.init(ComponentRegistry())
request_body = inspector._get_request_body(path, method)
assert len(request_body['content'].keys()) == 2
assert 'multipart/form-data' in request_body['content']
assert 'application/json' in request_body['content']
operation = inspector.get_operation(path, method)
content = operation['requestBody']['content']
assert len(content.keys()) == 2
assert 'multipart/form-data' in content
assert 'application/json' in content
def test_renderer_mapping(self):
"""Test that view's renderers are mapped to OA media types"""
path = '/{id}/'
method = 'GET'
class View(generics.CreateAPIView):
class View(generics.ListCreateAPIView):
serializer_class = views.ExampleSerializer
renderer_classes = [JSONRenderer]
@ -423,13 +450,15 @@ class TestOperationIntrospection(TestCase):
method,
create_request(path),
)
registry = ComponentRegistry()
inspector = AutoSchema()
inspector.view = view
inspector.init(registry)
responses = inspector._get_responses(path, method)
operation = inspector.get_operation(path, method)
# TODO this should be changed once the multiple response
# schema support is there
success_response = responses['200']
success_response = operation['responses']['200']
assert len(success_response['content'].keys()) == 1
assert 'application/json' in success_response['content']
@ -449,13 +478,15 @@ class TestOperationIntrospection(TestCase):
method,
create_request(path),
)
registry = ComponentRegistry()
inspector = AutoSchema()
inspector.view = view
inspector.init(registry)
request_body = inspector._get_request_body(path, method)
mp_media = request_body['content']['multipart/form-data']
attachment = mp_media['schema']['properties']['attachment']
assert attachment['format'] == 'binary'
operation = inspector.get_operation(path, method)
assert 'multipart/form-data' in operation['requestBody']['content']
assert registry.schemas['Item']['properties']['attachment']['format'] == 'binary'
def test_retrieve_response_body_generation(self):
"""
@ -476,7 +507,7 @@ class TestOperationIntrospection(TestCase):
class ItemSerializer(serializers.Serializer):
text = serializers.CharField()
class View(generics.GenericAPIView):
class View(generics.RetrieveAPIView):
serializer_class = ItemSerializer
pagination_class = Pagination
@ -485,26 +516,30 @@ class TestOperationIntrospection(TestCase):
method,
create_request(path),
)
registry = ComponentRegistry()
inspector = AutoSchema()
inspector.view = view
inspector.init(registry)
responses = inspector._get_responses(path, method)
assert responses == {
operation = inspector.get_operation(path, method)
assert operation['responses'] == {
'200': {
'description': '',
'content': {
'application/json': {
'schema': {
'schema': {'$ref': '#/components/schemas/Item'}
}
},
'description': ''
}
}
assert registry.schemas['Item'] == {
'properties': {
'text': {
'type': 'string',
},
},
'required': ['text'],
},
},
},
},
}
def test_operation_id_generation(self):
@ -518,9 +553,10 @@ class TestOperationIntrospection(TestCase):
)
inspector = AutoSchema()
inspector.view = view
inspector.init(ComponentRegistry())
operationId = inspector._get_operation_id(path, method)
assert operationId == 'listExamples'
assert operationId == 'list'
def test_repeat_operation_ids(self):
router = routers.SimpleRouter()
@ -532,10 +568,9 @@ class TestOperationIntrospection(TestCase):
request = create_request('/')
schema = generator.get_schema(request=request)
schema_str = str(schema)
print(schema_str)
assert schema_str.count("operationId") == 2
assert schema_str.count("newExample") == 1
assert schema_str.count("oldExample") == 1
assert schema_str.count("account_new_retrieve") == 1
assert schema_str.count("account_old_retrieve") == 1
def test_serializer_datefield(self):
path = '/'
@ -545,12 +580,13 @@ class TestOperationIntrospection(TestCase):
method,
create_request(path),
)
registry = ComponentRegistry()
inspector = AutoSchema()
inspector.view = view
inspector.init(registry)
inspector.get_operation(path, method)
responses = inspector._get_responses(path, method)
response_schema = responses['200']['content']['application/json']['schema']
properties = response_schema['items']['properties']
properties = registry.schemas['Example']['properties']
assert properties['date']['type'] == properties['datetime']['type'] == 'string'
assert properties['date']['format'] == 'date'
assert properties['datetime']['format'] == 'date-time'
@ -563,12 +599,13 @@ class TestOperationIntrospection(TestCase):
method,
create_request(path),
)
registry = ComponentRegistry()
inspector = AutoSchema()
inspector.view = view
inspector.init(registry)
inspector.get_operation(path, method)
responses = inspector._get_responses(path, method)
response_schema = responses['200']['content']['application/json']['schema']
properties = response_schema['items']['properties']
properties = registry.schemas['Example']['properties']
assert properties['hstore']['type'] == 'object'
def test_serializer_callable_default(self):
@ -595,12 +632,13 @@ class TestOperationIntrospection(TestCase):
method,
create_request(path),
)
registry = ComponentRegistry()
inspector = AutoSchema()
inspector.view = view
inspector.init(registry)
inspector.get_operation(path, method)
responses = inspector._get_responses(path, method)
response_schema = responses['200']['content']['application/json']['schema']
properties = response_schema['items']['properties']
properties = registry.schemas['ExampleValidated']['properties']
assert properties['integer']['type'] == 'integer'
assert properties['integer']['maximum'] == 99
@ -659,7 +697,7 @@ class TestGenerator(TestCase):
generator = SchemaGenerator(patterns=patterns)
generator._initialise_endpoints()
paths = generator.get_paths()
paths = generator.parse()
assert '/example/' in paths
example_operations = paths['/example/']
@ -676,7 +714,7 @@ class TestGenerator(TestCase):
generator = SchemaGenerator(patterns=patterns)
generator._initialise_endpoints()
paths = generator.get_paths()
paths = generator.parse()
assert '/v1/example/' in paths
assert '/v1/example/{id}/' in paths
@ -689,7 +727,7 @@ class TestGenerator(TestCase):
generator = SchemaGenerator(patterns=patterns, url='/api')
generator._initialise_endpoints()
paths = generator.get_paths()
paths = generator.parse()
assert '/api/example/' in paths
assert '/api/example/{id}/' in paths
@ -732,4 +770,4 @@ class TestGenerator(TestCase):
schema = generator.get_schema(request=request)
assert schema['info']['title'] == ''
assert schema['info']['version'] == ''
assert schema['info']['version'] == '0.0.0'