Skip to content

Commit 08c4d36

Browse files
juannorrissloria
andauthored
Fix usage of intersection/union for OrderedSet in Nested field (#1627)
* Add basic set methods to OrderedSet Add `union`, `intersection` and `difference` methods to `OrderedSet`. This fixes a bug where a `Nested` field initialized with an instance of a schema (instead of a class) would fail to set the `only` property when serializing. * Parametrize OrderedSet tests * Fix issue by using set operators instead of methods * Update changelog Co-authored-by: Steven Loria <sloria1@gmail.com>
1 parent 2ca48bf commit 08c4d36

4 files changed

Lines changed: 26 additions & 2 deletions

File tree

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,4 @@ Contributors (chronological)
150150
- Laurie Opperman `@EpicWink <https://github.com/EpicWink>`_
151151
- Ram Rachum `@cool-RR <https://github.com/cool-RR>`_
152152
- `@weeix <https://github.com/weeix>`_
153+
- Juan Norris `@juannorris <https://github.com/juannorris>`_

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ Support:
1212

1313
- Document ``default_error_messages`` on field classes (:pr:`1619`). Thanks :user:`weeix`.
1414

15+
Bug fixes:
16+
17+
- Fix passing ``only`` and ``exclude`` to ``Nested`` with an ordered ``Schema`` (:pr:`1627`).
18+
Thanks :user:`juannorris` for the PR.
19+
1520
3.6.1 (2020-06-02)
1621
******************
1722

src/marshmallow/fields.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,10 +522,10 @@ def schema(self):
522522
original = self._schema.only
523523
else: # only=None -> all fields
524524
original = self._schema.fields.keys()
525-
self._schema.only = set_class(self.only).intersection(original)
525+
self._schema.only = set_class(self.only) & set_class(original)
526526
if self.exclude:
527527
original = self._schema.exclude
528-
self._schema.exclude = set_class(self.exclude).union(original)
528+
self._schema.exclude = set_class(self.exclude) | set_class(original)
529529
self._schema._init_fields()
530530
else:
531531
if isinstance(nested, type) and issubclass(nested, SchemaABC):

tests/test_fields.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,24 @@ class MySchema(Schema):
277277
with pytest.raises(ValidationError):
278278
MySchema().load({"nested": {"x": 1}})
279279

280+
@pytest.mark.parametrize(
281+
("param", "fields_list"), [("only", ["foo"]), ("exclude", ["bar"])]
282+
)
283+
def test_ordered_instanced_nested_schema_only_and_exclude(self, param, fields_list):
284+
class NestedSchema(Schema):
285+
foo = fields.String()
286+
bar = fields.String()
287+
288+
class Meta:
289+
ordered = True
290+
291+
class MySchema(Schema):
292+
nested = fields.Nested(NestedSchema(), **{param: fields_list})
293+
294+
assert MySchema().dump({"nested": {"foo": "baz", "bar": "bax"}}) == {
295+
"nested": {"foo": "baz"}
296+
}
297+
280298

281299
class TestListNested:
282300
@pytest.mark.parametrize("param", ("only", "exclude", "dump_only", "load_only"))

0 commit comments

Comments
 (0)