From e869b15a204944fe389898e9102a3ac843d0782e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Tue, 24 Dec 2024 15:16:07 +0100 Subject: [PATCH] fix: remove highlightResult from response (#337) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 🧭 What and Why 🎟 Related Issue: ### Changes included: there is a regression in the new api clients, we don't remove the `highlightResult` anymore of the returned objects from the browse response. in the meantime of a fix there, we can fix it in the integration to prevent different behaviors, as the API doesn't allow saving rules with `highlightResult` --- algoliasearch_django/models.py | 12 ++++++++++-- runtests.py | 2 +- tests/test_index.py | 8 +++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/algoliasearch_django/models.py b/algoliasearch_django/models.py index 5e55822..97819cc 100644 --- a/algoliasearch_django/models.py +++ b/algoliasearch_django/models.py @@ -36,6 +36,12 @@ def get_model_attr(name): return partial(_getattr, name=name) +def sanitize(hit): + if "_highlightResult" in hit: + hit.pop("_highlightResult") + return hit + + class AlgoliaIndexError(Exception): """Something went wrong with an Algolia Index.""" @@ -493,7 +499,8 @@ def reindex_all(self, batch_size=1000): rules = [] self.__client.browse_rules( - self.index_name, lambda _resp: rules.extend(_resp.hits) + self.index_name, + lambda _resp: rules.extend([sanitize(_hit.to_dict()) for _hit in _resp.hits]), ) if len(rules): logger.debug("Got rules for index %s: %s", self.index_name, rules) @@ -501,7 +508,8 @@ def reindex_all(self, batch_size=1000): synonyms = [] self.__client.browse_synonyms( - self.index_name, lambda _resp: synonyms.extend(_resp.hits) + self.index_name, + lambda _resp: synonyms.extend([sanitize(_hit.to_dict()) for _hit in _resp.hits]), ) if len(synonyms): logger.debug("Got synonyms for index %s: %s", self.index_name, rules) diff --git a/runtests.py b/runtests.py index 8d97daa..ff520a7 100755 --- a/runtests.py +++ b/runtests.py @@ -16,7 +16,7 @@ def main(): # kept here to run a single test # failures = test_runner.run_tests( # [ - # "tests.test_index.IndexTestCase.test_reindex_with_rules" + # "tests.test_index.IndexTestCase" # ] # ) failures = test_runner.run_tests(["tests"]) diff --git a/tests/test_index.py b/tests/test_index.py index 33fbad3..62ecdd8 100644 --- a/tests/test_index.py +++ b/tests/test_index.py @@ -10,6 +10,12 @@ from .models import User, Website, Example +def sanitize(hit): + if "_highlightResult" in hit: + hit.pop("_highlightResult") + return hit + + class IndexTestCase(TestCase): def setUp(self): self.client = algolia_engine.client @@ -320,7 +326,7 @@ class WebsiteIndex(AlgoliaIndex): synonyms = [] self.client.browse_synonyms( self.index.index_name, - lambda _resp: synonyms.extend([_hit.to_dict() for _hit in _resp.hits]), + lambda _resp: synonyms.extend([sanitize(_hit.to_dict()) for _hit in _resp.hits]), ) self.assertEqual(len(synonyms), 1, "There should only be one synonym") self.assertIn(