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.

New feature for iterating over fields_dict in marshalling/unmarshalling

See original GitHub issue

jsonschema(and swagger) have specific options for unexpected additional fields.

And, I want to translate this feature to marshmallow schema.

marshmallow’s current implementation

The way of iteration in marshmallow’s current implementation, is just calling iteritems() with fields_dict. So, it is difficult that define schema allowing unexpected field.

for examples, in swagger (openAPI2.0) the schema definition, like bellow.

definitions:
  person:
    type: object
    properties:
      name:
        type: string
    additionalPropeties: integer
    required:
      - name

Then, about person schema, passing {"name": "foo"} is ok, and {"name": "foo", "x": 20, "y": 10} is also ok(unexpected field is treated as integer).

it is a bit difficult, making marshmallow’s version of this. (schema meta’s additional is candidate of expected field names, … and so on.)

proposal

If marshmallow has new hook point that changing the way of iteration, it is enable to implement marshmallow’s version, more easily.

For example, get_marshalling_iterator()(this is not good name), is existed in Schema class.

# def default_iterator(data, fields_dict):
#     return iteritems(fields_dict)


class S(Schema):
    def get_marshalling_iterator(self, additional_field=fields.Int()):
        def iterate_with_additional_field(data, fields_dict):
            for k in data:
                field = fields_dict.get(k, additional_field)
                yield k, field

        return iterate_with_additional_field

    name = fields.String(required=True)
  • 💭 get_marshalling_iterator is not good name, maybe
  • 💭 the position of hook point(Schema class’s instance method) is not good, maybe

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sloriacommented, Jul 5, 2018

It is now possible to allow additional properties to be included in deserialized data (see #524).

I believe the requested feature here would be covered by #853. Let’s continue discussion there.

0reactions
podhmocommented, Apr 13, 2018

It is decided that supporting additional_properties via Meta attribute, closing this issue, and creating new issue, is better?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Looping/iterate over the second level nested JSON in go lang
I have some requirement like I want to read/get all the Key and value in String type for some processing adn I can't...
Read more >
marshmallow - Read the Docs
By default Schemas will unmarshal an input dictionary to an output dictionary whose keys are identical to the field names. However, if you...
Read more >
marshmallow.schema — marshmallow 2.21.0 documentation
_creation_index, ) else: return fields # This function allows Schemas to inherit from non-Schema classes and ensures # inheritance according to the MRO...
Read more >
A Complete Guide to JSON in Golang (With Examples)
This post will describe how to marshal and unmarshal JSON in Go. ... These maps can be iterated over, so an unknown number...
Read more >
3 Ways To Iterate Over Python Dictionaries Using For Loops
How to add a new keys to a dictionary? In this article, I will attempt to provide you with a succinct and clear...
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