plone / plone/plone.restapi

CollectionFieldSerializer throws AttributeError if Catalog Object not available

Open
#1,407 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
109
Forks
107
Avg merge
2d 3h
Merged PRs (30d)
4

Description

CatalogSource in a vocabulary might not serialize if the target object, out of whichever reasons, is deleted.
Extends the error handling with an AttributeError:

@adapter(ICollection, IDexterityContent, Interface)
@implementer(IFieldSerializer)
class CollectionFieldSerializer(DefaultFieldSerializer):
    def __call__(self):
        # Binding is necessary for named vocabularies
        if IField.providedBy(self.field):
            self.field = self.field.bind(self.context)
        value = self.get_value()
        value_type = self.field.value_type
        if (
            value is not None
            and IChoice.providedBy(value_type)
            and IVocabularyTokenized.providedBy(value_type.vocabulary)
        ):
            values = []
            for v in value:
                try:
                    term = value_type.vocabulary.getTerm(v)
                    values.append({"token": term.token, "title": term.title})
                except (LookupError, AttributeError):
                    log.warning("Term lookup error: %r" % v)
            value = values
        return json_compatible(value)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with CollectionFieldSerializer.call shown in the issue and trace how CatalogSource terms are resolved when the target object is missing. Verify that a missing catalog object no longer raises AttributeError while valid terms still serialize with their token and title.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.