def test_multiple_nested_routes(self): schema = coreapi.Document( url='', title='Example API', content={ 'animals': { 'dog': { 'vet': { 'list': coreapi.Link(url='/animals/dog/{id}/vet', action='get', fields=[ coreapi.Field( 'id', required=True, location='path', schema=coreschema.String()) ]) }, 'read': coreapi.Link(url='/animals/dog/{id}', action='get', fields=[ coreapi.Field( 'id', required=True, location='path', schema=coreschema.String()) ]) }, 'cat': { 'list': coreapi.Link(url='/animals/cat/', action='get', fields=[ coreapi.Field( 'id', required=True, location='path', schema=coreschema.String()) ]), 'create': coreapi.Link(url='/aniamls/cat', action='post', fields=[]) } } }) section = schema['animals'] flat_links = schema_links(section) assert len(flat_links) is 4 assert 'cat > create' in flat_links assert 'cat > list' in flat_links assert 'dog > read' in flat_links assert 'dog > vet > list' in flat_links
def test_default_actions_and_single_custom_action(self): schema = coreapi.Document( url='', title='Example API', content={ 'users': { 'create': coreapi.Link(url='/users/', action='post', fields=[]), 'list': coreapi.Link(url='/users/', action='get', fields=[]), 'read': coreapi.Link(url='/users/{id}/', action='get', fields=[ coreapi.Field('id', required=True, location='path', schema=coreschema.String()) ]), 'update': coreapi.Link(url='/users/{id}/', action='patch', fields=[ coreapi.Field('id', required=True, location='path', schema=coreschema.String()) ]), 'friends': coreapi.Link(url='/users/{id}/friends', action='get', fields=[ coreapi.Field('id', required=True, location='path', schema=coreschema.String()) ]) } }) section = schema['users'] flat_links = schema_links(section) assert len(flat_links) is 5 assert 'list' in flat_links assert 'create' in flat_links assert 'read' in flat_links assert 'update' in flat_links assert 'friends' in flat_links
def get_serializer_fields(self, path, method): """ Return a list of `coreapi.Field` instances corresponding to any request body input, as determined by the serializer class. """ view = self.view if method not in ('PUT', 'PATCH', 'POST'): return [] if not hasattr(view, 'get_serializer'): return [] try: serializer = view.get_serializer() except exceptions.APIException: serializer = None warnings.warn('{}.get_serializer() raised an exception during ' 'schema generation. Serializer fields will not be ' 'generated for {} {}.'.format( view.__class__.__name__, method, path)) if isinstance(serializer, serializers.ListSerializer): return [ coreapi.Field(name='data', location='body', required=True, schema=coreschema.Array()) ] if not isinstance(serializer, serializers.Serializer): return [] fields = [] for field in serializer.fields.values(): if field.read_only or isinstance(field, serializers.HiddenField): continue required = field.required and method != 'PATCH' field = coreapi.Field(name=field.field_name, location='form', required=required, schema=field_to_schema(field)) fields.append(field) return fields
def get_schema_fields(self, view): assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`' assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`' return [ coreapi.Field(name=self.limit_query_param, required=False, location='query', schema=coreschema.Integer( title='Limit', description=force_text( self.limit_query_description))), coreapi.Field(name=self.offset_query_param, required=False, location='query', schema=coreschema.Integer( title='Offset', description=force_text( self.offset_query_description))) ]
class ObtainAuthToken(APIView): throttle_classes = () permission_classes = () parser_classes = ( parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser, ) renderer_classes = (renderers.JSONRenderer, ) serializer_class = AuthTokenSerializer if coreapi is not None and coreschema is not None: schema = ManualSchema( fields=[ coreapi.Field( name="username", required=True, location='form', schema=coreschema.String( title="Username", description="Valid username for authentication", ), ), coreapi.Field( name="password", required=True, location='form', schema=coreschema.String( title="Password", description="Valid password for authentication", ), ), ], encoding="application/json", ) def post(self, request, *args, **kwargs): serializer = self.serializer_class(data=request.data, context={'request': request}) serializer.is_valid(raise_exception=True) user = serializer.validated_data['user'] token, created = Token.objects.get_or_create(user=user) return Response({'token': token.key})
def get_schema_fields(self, view): assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`' assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`' return [ coreapi.Field(name=self.search_param, required=False, location='query', schema=coreschema.String( title=force_text(self.search_title), description=force_text(self.search_description))) ]
def get_schema(): return coreapi.Document( url='https://api.example.com/', title='Example API', content={ 'simple_link': coreapi.Link('/example/', description='example link'), 'headers': coreapi.Link('/headers/'), 'location': { 'query': coreapi.Link('/example/', fields=[ coreapi.Field(name='example', schema=coreschema.String(description='example field')) ]), 'form': coreapi.Link('/example/', action='post', fields=[ coreapi.Field(name='example') ]), 'body': coreapi.Link('/example/', action='post', fields=[ coreapi.Field(name='example', location='body') ]), 'path': coreapi.Link('/example/{id}', fields=[ coreapi.Field(name='id', location='path') ]) }, 'encoding': { 'multipart': coreapi.Link('/example/', action='post', encoding='multipart/form-data', fields=[ coreapi.Field(name='example') ]), 'multipart-body': coreapi.Link('/example/', action='post', encoding='multipart/form-data', fields=[ coreapi.Field(name='example', location='body') ]), 'urlencoded': coreapi.Link('/example/', action='post', encoding='application/x-www-form-urlencoded', fields=[ coreapi.Field(name='example') ]), 'urlencoded-body': coreapi.Link('/example/', action='post', encoding='application/x-www-form-urlencoded', fields=[ coreapi.Field(name='example', location='body') ]), 'raw_upload': coreapi.Link('/upload/', action='post', encoding='application/octet-stream', fields=[ coreapi.Field(name='example', location='body') ]), }, 'response': { 'download': coreapi.Link('/download/'), 'text': coreapi.Link('/text/') } } )
def get_schema_fields(self, view): assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`' assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`' fields = [ coreapi.Field(name=self.cursor_query_param, required=False, location='query', schema=coreschema.String( title='Cursor', description=force_text( self.cursor_query_description))) ] if self.page_size_query_param is not None: fields.append( coreapi.Field(name=self.page_size_query_param, required=False, location='query', schema=coreschema.Integer( title='Page size', description=force_text( self.page_size_query_description)))) return fields
def get_path_fields(self, path, method): """ Return a list of `coreapi.Field` instances corresponding to any templated path variables. """ view = self.view model = getattr(getattr(view, 'queryset', None), 'model', None) fields = [] for variable in uritemplate.variables(path): title = '' description = '' schema_cls = coreschema.String kwargs = {} if model is not None: # Attempt to infer a field description if possible. try: model_field = model._meta.get_field(variable) except Exception: model_field = None if model_field is not None and model_field.verbose_name: title = force_text(model_field.verbose_name) if model_field is not None and model_field.help_text: description = force_text(model_field.help_text) elif model_field is not None and model_field.primary_key: description = get_pk_description(model, model_field) if hasattr(view, 'lookup_value_regex' ) and view.lookup_field == variable: kwargs['pattern'] = view.lookup_value_regex elif isinstance(model_field, models.AutoField): schema_cls = coreschema.Integer field = coreapi.Field(name=variable, location='path', required=True, schema=schema_cls(title=title, description=description, **kwargs)) fields.append(field) return fields