question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow exportation of the Marshmallow Schema of a Document

See original GitHub issue

Currently, 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:

https://github.com/Scille/umongo/pull/33 drafts a way of exporting the Marshmallow Schema from the document.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
touilleMancommented, Aug 27, 2016

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 and as_marshmallow_field to get regular marshmallow field/schema form umongo ones.

Two points:

An umongo.fields.EmbeddedDocumentField will generate a marshamallow.fields.Nested which de-serializes data into a dict (and not a umongo.EmbeddedDocument !). Then this dict can be passed to the umongo.Document field assignation (or during the construction of the document) to be turned into a real umongo.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 with build_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 master

0reactions
lafrechcommented, Sep 9, 2016

Great. Your fix is much better than my hack.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Extending Schemas — marshmallow 3.19.0 documentation
One common use case is to wrap data in a namespace upon serialization and unwrap the data during deserialization. from marshmallow import Schema,...
Read more >
How can I use marshmallow to export a complex object to ...
I have this simple code that uses marshmallow to convert complex object into its json representation: from marshmallow import Schema, ...
Read more >
marshmallow-dataclass - Python Package Health Analysis
Python library to convert dataclasses into marshmallow schemas. For more information about how to use this package see README.
Read more >
Mapping and Validating with Marshmallow | Flask and Python ...
From that I explain the need to have a mapping with Marshmallow. I will use Marshmallow to map my database entities to JSON...
Read more >
Migrating Room databases | Android Developers
Room can export your database's schema information into a JSON file at compile time. To export the schema, set the room.schemaLocation annotation processor ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found