From d48b989769afa08c26bc4d001752e030ed0d0af6 Mon Sep 17 00:00:00 2001 From: James Date: Fri, 25 Nov 2022 09:53:56 +0000 Subject: [PATCH] jsontogeojson: Fix crash --- libcoveofds/geojson.py | 2 +- .../json_to_geojson/dont_crash_1.json | 24 +++++++++++++++++++ tests/test_json_to_geojson.py | 17 +++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/json_to_geojson/dont_crash_1.json diff --git a/libcoveofds/geojson.py b/libcoveofds/geojson.py index ba50c73..3afe126 100644 --- a/libcoveofds/geojson.py +++ b/libcoveofds/geojson.py @@ -101,7 +101,7 @@ def _dereference_object(self, ref, list): if "id" in ref: for item in list: - if item.get("id") == ref["id"]: + if isinstance(item, dict) and item.get("id") == ref["id"]: return item return ref diff --git a/tests/fixtures/json_to_geojson/dont_crash_1.json b/tests/fixtures/json_to_geojson/dont_crash_1.json new file mode 100644 index 0000000..8813697 --- /dev/null +++ b/tests/fixtures/json_to_geojson/dont_crash_1.json @@ -0,0 +1,24 @@ +{ + "networks": [ + { + "id": "a096d627-72e1-4f9b-b129-951b1737bff4", + "name": "Ghana Fibre Network", + "organisations": [ + "my mate down the pub" + ], + "phases": [ + { + "funders": [ + {"id": "1"} + ] + } + ], + "links": [ + { + "rel": "describedby", + "href": "https://raw.githubusercontent.com/Open-Telecoms-Data/open-fibre-data-standard/0__1__0__beta/schema/network-schema.json" + } + ] + } + ] +} \ No newline at end of file diff --git a/tests/test_json_to_geojson.py b/tests/test_json_to_geojson.py index 7cc06eb..23008a3 100644 --- a/tests/test_json_to_geojson.py +++ b/tests/test_json_to_geojson.py @@ -65,3 +65,20 @@ def test_json_to_geojson(filename): with open(meta_expected_filename) as fp: meta_expected_data = json.load(fp) assert meta_expected_data == converter.get_meta_json() + + +def test_dont_crash_1(): + """Just put as much bad stuff as possible in the input and make sure it doesn't crash! + We don't care about testing output. Other tests can do that.""" + json_filename = os.path.join( + os.path.dirname(os.path.realpath(__file__)), + "fixtures", + "json_to_geojson", + "dont_crash_1.json", + ) + + with open(json_filename) as fp: + json_data = json.load(fp) + + converter = JSONToGeoJSONConverter() + converter.process_package(json_data)