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.

Question: how is the "only" attribute of a schema intended to be used?

See original GitHub issue

From the marshmallow docs:

only (tuple) – A list or tuple of fields to serialize. If None, all fields will be serialized. Nested fields can be represented with dot delimiters.

This gives me the impression that it is a separate attribute from fields that is used on serialization to know which fields to serialize. This is partly true in that if only is set on a schema instance and dump is called, it does indeed affect which fields are serialized. But if you look at the schema instance after dump is called, it actually has “destructively” modified the fields attribute of the schema. From my perspective, it seems as though fields should remain unchanged – only should merely inform the serialization process during the execution of the serialization as opposed to altering the schema instance. I am possibly misunderstanding how it was intended to be used, but the docs may need to be clarified in that case.

A little example to further explain my use case and why I hoped it would behave the way I imagined (using marshmallow with Flask):

from schemas import SchemaClass

@app.route('/collection/<resource_id>')
def route_handler(**kwargs):
    return getattr(ViewHelperClass, request.method.lower())(**kwargs)


class ViewHelperClass:
    @staticmethod
    @SchemaClass(many=False, exclude=['field1'], dump_only=['field2', 'field3'])
    def get(**kwargs):
       # Do some stuff
       # return something

In my case, I’ve provided a base schema which all other schemas (including SchemaClass in the above example) that extends the functionality a bit to allow an instance of a schema to decorate a function. This is highly useful, because I know all the attributes of the schema instance prior to execution time.

However, I allow the client to request only certain fields be returned: api.example.com/collection/1?fields=field3,field4. The only way to satisfy that request is by setting the only attribute of the schema. This has some unfortunate side effects, though, because the schema instance is not recycled for the next request, meaning the fields have been permanently altered for future requests when dump is called with a subset of fields supplied to only.

Sorry for the long explanation, but hopefully that helps clarify the issue I’m having. To reiterate my question: was only to be used exclusively on one-off instances of schemas? If that is the case and to remain the case, would it be possible to provide an argument for serialization that allows specifying this? For example, schema_inst.dump(data, only=only_the_fields_I_want)?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
lafrechcommented, Apr 4, 2018

No problem, you’re welcome. I was just trying to understand. Glad you’re not stuck.

0reactions
lafrechcommented, Apr 26, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

XML Schema Part 0: Primer Second Edition - W3C
The fixed attribute is used in both attribute and element declarations to ensure that the attributes and elements are set to particular values....
Read more >
What are database schemas? 5 minute guide with examples
A logical database schema represents how the data is organized in terms of tables. It also explains how attributes from tables are linked ......
Read more >
What is a Database Schema - Lucidchart
As part of a data dictionary, a database schema indicates how the entities that make up the database relate to one another, including...
Read more >
Basics of Schema Composition | Adobe Experience Platform
Record data: Provides information about the attributes of a subject. ... Primary identities only need to be set for schemas intended to be...
Read more >
MODS User Guidelines: Metadata Object Description Schema
displayLabel - This attribute is intended to be used when additional text ... Dates may be recorded here only if considered part of...
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