Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add django rest framework mongoengine support #181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
58 changes: 58 additions & 0 deletions drf_spectacular/contrib/django_rest_framework_mongoengine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from mongoengine import document, fields # type: ignore
from rest_framework.utils.model_meta import get_field_info
from rest_framework_mongoengine import serializers, viewsets # type: ignore

Check warning on line 3 in drf_spectacular/contrib/django_rest_framework_mongoengine.py

View check run for this annotation

Codecov / codecov/patch

drf_spectacular/contrib/django_rest_framework_mongoengine.py#L1-L3

Added lines #L1 - L3 were not covered by tests

from drf_spectacular.openapi import AutoSchema
from drf_spectacular.plumbing import (

Check warning on line 6 in drf_spectacular/contrib/django_rest_framework_mongoengine.py

View check run for this annotation

Codecov / codecov/patch

drf_spectacular/contrib/django_rest_framework_mongoengine.py#L5-L6

Added lines #L5 - L6 were not covered by tests
anyisinstance, build_basic_type, error, get_lib_doc_excludes, warn,
)
from drf_spectacular.types import OpenApiTypes

Check warning on line 9 in drf_spectacular/contrib/django_rest_framework_mongoengine.py

View check run for this annotation

Codecov / codecov/patch

drf_spectacular/contrib/django_rest_framework_mongoengine.py#L9

Added line #L9 was not covered by tests


class MongoEngineAutoSchema(AutoSchema):
def _map_model_field(self, model_field, direction):
assert isinstance(model_field, fields.BaseField)

Check warning on line 14 in drf_spectacular/contrib/django_rest_framework_mongoengine.py

View check run for this annotation

Codecov / codecov/patch

drf_spectacular/contrib/django_rest_framework_mongoengine.py#L12-L14

Added lines #L12 - L14 were not covered by tests
# to get a fully initialized serializer field we use DRF's own init logic
try:
field_cls, field_kwargs = serializers.DocumentSerializer().build_field(

Check warning on line 17 in drf_spectacular/contrib/django_rest_framework_mongoengine.py

View check run for this annotation

Codecov / codecov/patch

drf_spectacular/contrib/django_rest_framework_mongoengine.py#L16-L17

Added lines #L16 - L17 were not covered by tests
field_name=model_field.name,
info=get_field_info(model_field.model),
model_class=model_field.model,
nested_depth=0,
)
field = field_cls(**field_kwargs)
except: # noqa
field = None

Check warning on line 25 in drf_spectacular/contrib/django_rest_framework_mongoengine.py

View check run for this annotation

Codecov / codecov/patch

drf_spectacular/contrib/django_rest_framework_mongoengine.py#L23-L25

Added lines #L23 - L25 were not covered by tests

if field and not anyisinstance(field, [fields.EmbeddedDocumentField, fields.EmbeddedDocumentListField]):
return self._map_serializer_field(field, direction)
elif isinstance(model_field, fields.ReferenceField):
return self._map_model_field(model_field.target_field, direction)
elif hasattr(document, model_field.__class__.__name__):

Check warning on line 31 in drf_spectacular/contrib/django_rest_framework_mongoengine.py

View check run for this annotation

Codecov / codecov/patch

drf_spectacular/contrib/django_rest_framework_mongoengine.py#L27-L31

Added lines #L27 - L31 were not covered by tests
# be graceful when the document field is not explicitly mapped to a serializer
internal_type = getattr(document, model_field.__class__.__name__)
field_cls = serializers.DocumentSerializer.serializer_field_mapping.get(internal_type)
if not field_cls:
warn(

Check warning on line 36 in drf_spectacular/contrib/django_rest_framework_mongoengine.py

View check run for this annotation

Codecov / codecov/patch

drf_spectacular/contrib/django_rest_framework_mongoengine.py#L33-L36

Added lines #L33 - L36 were not covered by tests
f'document field "{model_field.__class__.__name__}" has no mapping in '
f'DocumentSerializer. it may be a deprecated field. defaulting to "string"'
)
return build_basic_type(OpenApiTypes.STR)
return self._map_serializer_field(field_cls(), direction)

Check warning on line 41 in drf_spectacular/contrib/django_rest_framework_mongoengine.py

View check run for this annotation

Codecov / codecov/patch

drf_spectacular/contrib/django_rest_framework_mongoengine.py#L40-L41

Added lines #L40 - L41 were not covered by tests
else:
error(

Check warning on line 43 in drf_spectacular/contrib/django_rest_framework_mongoengine.py

View check run for this annotation

Codecov / codecov/patch

drf_spectacular/contrib/django_rest_framework_mongoengine.py#L43

Added line #L43 was not covered by tests
f'could not resolve document field "{model_field}". failed to resolve through '
f'serializer_field_mapping, get_internal_type(), or any override mechanism. '
f'defaulting to "string"'
)
return build_basic_type(OpenApiTypes.STR)

Check warning on line 48 in drf_spectacular/contrib/django_rest_framework_mongoengine.py

View check run for this annotation

Codecov / codecov/patch

drf_spectacular/contrib/django_rest_framework_mongoengine.py#L48

Added line #L48 was not covered by tests


def get_mongoengine_extended_doc_excludes():
return get_lib_doc_excludes() + [

Check warning on line 52 in drf_spectacular/contrib/django_rest_framework_mongoengine.py

View check run for this annotation

Codecov / codecov/patch

drf_spectacular/contrib/django_rest_framework_mongoengine.py#L51-L52

Added lines #L51 - L52 were not covered by tests
serializers.DocumentSerializer,
serializers.EmbeddedDocumentSerializer,
viewsets.ModelViewSet,
viewsets.GenericViewSet,
viewsets.ReadOnlyModelViewSet
]
6 changes: 5 additions & 1 deletion requirements/optionals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ django-polymorphic>=2.1
django-rest-polymorphic>=0.1.8
django-oauth-toolkit>=1.2.0
djangorestframework-camel-case>=1.1.2
django-filter>=2.3.0
django-filter>=2.3.0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pymongo is not required as it is a dependency of mongoengine

mongomock will likely also come in handy for testing

pymongo>=3.11.0
mongoengine>=0.20.0
django-mongoengine>=0.4.2
django-rest-framework-mongoengine>=3.4.1