Allow exportation of the Marshmallow Schema of a Document
See original GitHub issueCurrently, the Schema of a Document can be obtained from Document.Schema. However, this Schema is made to [|de]serialize between “DB / data_proxy” and “OO world”, not between “OO world” and “client/JSON world”. (See diagram in the docs).
In other words, uMongo is made to be used like this:
document.dump(schema=schema)
# which is equivalent to
schema.dump(document._data._data)
# but not equivalent to
schema.dump(document)
The difference being that the data_proxy does not behave like the document:
- It may have keys named differently is “attribute” is used
- document returns
None
if a key is missing - …
Therefore, using the Schema to serialize a Document may work but it currently has corner cases.
@touilleMan confirms that the ability to export a Marshmallow Schema without the uMongo specificities is in the scope of uMongo and is a feature we want to have.
The idea could be to add a method or attribute to the document to provide that “cleaned up” Schema.
I’m opening this issue to centralize the reflexions about that.
Currently, here are the issues I found:
-
check_unknown_fields
raises ValidationError if passed adump_only
field even if value ismissing
, which is an issue when validating a document before deserialization. (Bug report: https://github.com/Scille/umongo/issues/18, PR: https://github.com/Scille/umongo/pull/19) - Some uMongo fields fail if value is
None
. Maybe this one is unrelated but just happened to occur while calling `schema.dump(document) (PR: https://github.com/Scille/umongo/pull/32) - Fields with “attribute” not being
None
will fail because the Schema tries to find the value in “attribute”, while in the document, it is available at the field’s name. To avoid this, we could set “attribute” toNone
in all the fields. (PR: https://github.com/Scille/umongo/pull/33)
https://github.com/Scille/umongo/pull/33 drafts a way of exporting the Marshmallow Schema from the document.
Issue Analytics
- State:
- Created 7 years ago
- Comments:13 (6 by maintainers)
Top GitHub Comments
I’ve done some big work on this feature, see branch https://github.com/Scille/umongo/tree/export_schema and 0a35ce841
Basically I provide
as_marshmallow_schema
andas_marshmallow_field
to get regular marshmallow field/schema form umongo ones.Two points:
An
umongo.fields.EmbeddedDocumentField
will generate amarshamallow.fields.Nested
which de-serializes data into a dict (and not aumongo.EmbeddedDocument
!). Then this dict can be passed to theumongo.Document
field assignation (or during the construction of the document) to be turned into a realumongo.EmbeddedDocument
.It’s the same thing with the tricky fields (like
GenericReferenceField
too).A parameter
mongo_world
is provided to configure the generated schema to work with the mongo world. Thus advanced user can directly do the unserialization with the generated marshamallow schema, then build the umongo object withbuild_from_mongo
(and then don’t have to do two validations of the same data)@lafrech Can you play a bit with this (see the
tests/test_marshmallow.py
for small examples) and give me your feedback ? I think I still have to update the documentation & examples before merging this on the masterGreat. Your fix is much better than my hack.