0

I am building a REST API on top of Flask and Connexion using marshmallow-mongoengine and MongoEngine to work with MongoDB.

Problem: If my input data includes additional, unknown fields they are always excluded from the resulting object when loading the data with marshmallow-mongoengine; even if my MongoEngine document should allow them as it extends DynamicDocument. For example,

# Imports
from marshmallow_mongoengine import ModelSchema
from mongoengine.document import DynamicDocument
from mongoengine.fields import StringField, UUIDField
from pprint import pprint

# MongoEngine document
class User(DynamicDocument):
    id = UUIDField(required=True, binary=False, primary_key=True)
    first_name = StringField(required=True)
    last_name = StringField(required=True)

# Marshmallow schema with the Marshmallow-MongoEngine mapping
class UserSchema(ModelSchema):
    class Meta:
        model = User

# Usage
# Initialization
user_schema = UserSchema()
user_data = {
    "id": "77640cb5-75c9-4794-9581-5d7e2009cb78",
    "first_name": "John",
    "last_name": "Doe",
    "age": 50 # additional, unknown field
}

# Load user data
user = user_schema.load(data=user_data).data
pprint(user.to_mongo())
# {'_id': '77640cb5-75c9-4794-9581-5d7e2009cb78',
#  'first_name': 'John',
#  'last_name': 'Doe'}

# Dump user
data = user_schema.dump(obj=user).data
pprint(data)
# {'first_name': 'John',
#  'id': '77640cb5-75c9-4794-9581-5d7e2009cb78',
#  'last_name': 'Doe'}

As can be seen from the example, the age field is excluded from the loaded user object.

Question: How to include additional, unknown fields when loading data with marshmallow-mongoengine? What am I missing? Or maybe there is a better way to work with MongoDB. Either way, I would like to have control over the fields to be (de)serialized and to be able to return JSON similar to the one shown in the output of "Dump user", but with the age field included.

mplaine
  • 15
  • 4
  • 1
    The latest version (0.9.1) of `marshmallow-mongoengine` uses `marshmallow` 2.18.1. `marshmallow` 3.x introduces a new feature for [handling unknown fields](https://marshmallow.readthedocs.io/en/3.0/quickstart.html#handling-unknown-fields). So, I upgraded `marshmallow` from 2.x to 3.0.0rc4 + made some small changes to the code according to https://marshmallow.readthedocs.io/en/3.0/upgrading.html . The main change was to define that _unknown_ fields should be included in the schema, as follows: `user_schema = UserSchema(unknown=INCLUDE)` – mplaine Mar 07 '19 at 15:59
  • 1
    While that helped with loading user data, it didn't fix the issue with dumping user. That is, the output of the former (load) is correct but the latter (dump) one still excludes the additional `age` property. I am assuming that this problem is related to `marshmallow-mongoengine`. – mplaine Mar 07 '19 at 16:03

0 Answers0