-
Notifications
You must be signed in to change notification settings - Fork 270
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
ngocngoan
wants to merge
1
commit into
tfranzel:master
Choose a base branch
from
ngocngoan:feature/drf-mongoengine
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
drf_spectacular/contrib/django_rest_framework_mongoengine.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
from drf_spectacular.openapi import AutoSchema | ||
from drf_spectacular.plumbing import ( | ||
anyisinstance, build_basic_type, error, get_lib_doc_excludes, warn, | ||
) | ||
from drf_spectacular.types import OpenApiTypes | ||
|
||
|
||
class MongoEngineAutoSchema(AutoSchema): | ||
def _map_model_field(self, model_field, direction): | ||
assert isinstance(model_field, fields.BaseField) | ||
# to get a fully initialized serializer field we use DRF's own init logic | ||
try: | ||
field_cls, field_kwargs = serializers.DocumentSerializer().build_field( | ||
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 | ||
|
||
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__): | ||
# 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( | ||
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) | ||
else: | ||
error( | ||
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) | ||
|
||
|
||
def get_mongoengine_extended_doc_excludes(): | ||
return get_lib_doc_excludes() + [ | ||
serializers.DocumentSerializer, | ||
serializers.EmbeddedDocumentSerializer, | ||
viewsets.ModelViewSet, | ||
viewsets.GenericViewSet, | ||
viewsets.ReadOnlyModelViewSet | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ofmongoengine
mongomock
will likely also come in handy for testing