diff --git a/folium/features.py b/folium/features.py index 136f5e652b..4f0092fd65 100644 --- a/folium/features.py +++ b/folium/features.py @@ -668,7 +668,7 @@ class GeoJson(Layer): {%- endif %} {%- if not this.style %} - {{this.get_name()}}.setStyle(function(feature) {return feature.properties.style;}); + {{this.get_name()}}.setStyle(function(feature) {return (feature.properties || {}).style;}); {%- endif %} {% endmacro %} diff --git a/tests/selenium/test_geojson_selenium.py b/tests/selenium/test_geojson_selenium.py index 1908adb832..479779d993 100644 --- a/tests/selenium/test_geojson_selenium.py +++ b/tests/selenium/test_geojson_selenium.py @@ -36,3 +36,44 @@ def test_geojson(driver): assert control_label.text == "geojson" control_input = control_label.find_element(By.CSS_SELECTOR, value="input") assert control_input.get_attribute("checked") is None + + +def test_geojson_null_properties(driver): + """A feature with "properties": null must not break the map. + + RFC 7946 allows null properties. The default style callback read + feature.properties.style, which threw a TypeError and stopped the rest + of the map script, so nothing after it was drawn. + """ + square = [[[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]]] + data = { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": None, + "geometry": {"type": "Polygon", "coordinates": square}, + }, + { + "type": "Feature", + "properties": {"style": {"color": "red"}}, + "geometry": { + "type": "Polygon", + "coordinates": [[[x + 20, y] for x, y in square[0]]], + }, + }, + ], + } + m = folium.Map((5, 15), zoom_start=4) + folium.GeoJson(data).add_to(m) + folium.Marker((5, 15)).add_to(m) + + html = m.get_root().render() + with temp_html_filepath(html) as filepath: + driver.get_file(filepath) + assert driver.wait_until(".folium-map") + driver.verify_js_logs() + paths = driver.find_elements(By.CSS_SELECTOR, "path.leaflet-interactive") + assert len(paths) == 2 + assert paths[1].get_attribute("stroke") == "red" + assert driver.find_elements(By.CSS_SELECTOR, ".leaflet-marker-icon") diff --git a/tests/test_features.py b/tests/test_features.py index 5f2ae7b37c..e37314c50f 100644 --- a/tests/test_features.py +++ b/tests/test_features.py @@ -467,6 +467,32 @@ def test_geojson_empty_features_with_styling(): m.get_root().render() +def test_geojson_null_properties_default_style(): + """Without a style_function, features with null properties must not crash. + + RFC 7946 allows "properties": null. The default style callback used to + read feature.properties.style directly, which throws in the browser. + """ + m = Map() + data = { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": None, + "geometry": {"type": "Point", "coordinates": [0, 0]}, + } + ], + } + geojson = GeoJson(data).add_to(m) + rendered = m.get_root().render() + expected = ( + f"{geojson.get_name()}.setStyle(function(feature) " + "{return (feature.properties || {}).style;});" + ) + assert expected in rendered + + def test_geojson_event_handler(): """Test that event handlers are properly generated""" m = Map()