Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid values in RelationListFieldSerializer. #1818

Merged
merged 5 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/1818.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix response of `RelationListFieldSerializer` by filtering out invalid items. @Faakhir30
19 changes: 18 additions & 1 deletion src/plone/restapi/serializer/relationfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from z3c.relationfield.interfaces import IRelationChoice
from z3c.relationfield.interfaces import IRelationList
from z3c.relationfield.interfaces import IRelationValue
from z3c.relationfield import RelationValue
from zope.component import adapter
from zope.component import getMultiAdapter
from zope.globalrequest import getRequest
Expand All @@ -33,4 +34,20 @@ class RelationChoiceFieldSerializer(DefaultFieldSerializer):
@adapter(IRelationList, IDexterityContent, Interface)
@implementer(IFieldSerializer)
class RelationListFieldSerializer(DefaultFieldSerializer):
pass
def get_value(self, default=[]):
"""Return field value reduced to list of non-broken Relationvalues.

Args:
default (list, optional): Default field value. Defaults to empty list.

Returns:
list: List of RelationValues
"""
value = getattr(
self.field.interface(self.context), self.field.__name__, default
)
davisagli marked this conversation as resolved.
Show resolved Hide resolved
if not value:
return []
if isinstance(value, RelationValue):
return [value]
Faakhir30 marked this conversation as resolved.
Show resolved Hide resolved
return [el for el in value if el.to_object]
Faakhir30 marked this conversation as resolved.
Show resolved Hide resolved
Loading