Skip to content

Commit

Permalink
Merge pull request #184 from collective/deserializer-non-required
Browse files Browse the repository at this point in the history
For the JSON deserializer, skip empty values from non-required fields.
  • Loading branch information
petschki authored May 13, 2024
2 parents b8f8353 + c5d019e commit b1b3503
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Changelog
3.0.3 (unreleased)
------------------

- For the JSON deserializer, skip empty values from non-required fields.
This fixes a problem where empty non-required fields would break deserialization.
[thet]

- configure with `plone/meta`.
[petschki]

Expand Down
15 changes: 10 additions & 5 deletions src/collective/z3cform/datagridfield/deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@implementer(IFieldDeserializer)
@adapter(IRow, IDexterityContent, IBrowserRequest)
class DatagridRowDeserializer(DefaultFieldDeserializer):
def __call__(self, value):
def __call__(self, row):
row_data = {}

for name, field in getFields(self.field.schema).items():
Expand All @@ -35,13 +35,18 @@ def __call__(self, value):
deserializer = queryMultiAdapter(
(field, context, self.request), IFieldDeserializer
)

value = row.get(name)
if value is None:
# Skip empty values, e.g. for non-required fields
continue

if deserializer is None:
# simply add value
if name in value:
row_data[name] = value[name]
# simply add the value
row_data[name] = value
continue

row_data[name] = deserializer(value[name])
row_data[name] = deserializer(value)

return row_data

Expand Down

0 comments on commit b1b3503

Please sign in to comment.