diff --git a/folium/plugins/search.py b/folium/plugins/search.py index e82876b875..735d3b0ece 100644 --- a/folium/plugins/search.py +++ b/folium/plugins/search.py @@ -144,12 +144,7 @@ def render(self, **kwargs): if isinstance(self.layer, GeoJson): keys = tuple(self.layer.data["features"][0]["properties"].keys()) elif isinstance(self.layer, TopoJson): - obj_name = self.layer.object_path.split(".")[-1] - keys = tuple( - self.layer.data["objects"][obj_name]["geometries"][0][ - "properties" - ].keys() - ) # noqa + keys = tuple(self.layer._get_geometries()[0]["properties"].keys()) else: keys = None self.test_params(keys=keys) diff --git a/tests/plugins/test_search.py b/tests/plugins/test_search.py index 51542f8823..0a126f3bf0 100644 --- a/tests/plugins/test_search.py +++ b/tests/plugins/test_search.py @@ -1,3 +1,5 @@ +import pytest + import folium from folium.plugins import Search from folium.utilities import normalize @@ -24,3 +26,35 @@ def test_search_opens_popup_bound_to_geojson_group(): ) in rendered ) + + +def _non_collection_topology(): + return { + "type": "Topology", + "objects": { + "shape": { + "type": "Polygon", + "arcs": [[0]], + "properties": {"name": "A"}, + } + }, + "arcs": [[[0, 0], [1, 0], [0, 1], [-1, -1]]], + "transform": {"scale": [1, 1], "translate": [0, 0]}, + } + + +def test_search_topojson_non_collection_geometry(): + m = folium.Map() + layer = folium.TopoJson(_non_collection_topology(), "objects.shape").add_to(m) + Search(layer, search_label="name").add_to(m) + + assert f"{layer.get_name()}searchControl" in m.get_root().render() + + +def test_search_topojson_non_collection_geometry_bad_label(): + m = folium.Map() + layer = folium.TopoJson(_non_collection_topology(), "objects.shape").add_to(m) + Search(layer, search_label="missing").add_to(m) + + with pytest.raises(AssertionError, match="missing"): + m.get_root().render()