From 2c7a04815a1959d7ca209a32ffbe506bdc7b7b8b Mon Sep 17 00:00:00 2001 From: Tyler Young Date: Fri, 30 Aug 2024 19:05:37 -0400 Subject: [PATCH] Also handle non-numeric, non-list, non-string coords --- lib/geo/json/decoder.ex | 4 ++++ test/geo/json_test.exs | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/geo/json/decoder.ex b/lib/geo/json/decoder.ex index d615eb5..673bc6a 100644 --- a/lib/geo/json/decoder.ex +++ b/lib/geo/json/decoder.ex @@ -275,4 +275,8 @@ defmodule Geo.JSON.Decoder do raise ArgumentError, "expected a numeric coordinate, got: #{inspect(other)}" end) end + + defp ensure_numeric(other) do + raise ArgumentError, "expected a numeric coordinate, got: #{inspect(other)}" + end end diff --git a/test/geo/json_test.exs b/test/geo/json_test.exs index 1b7f059..193be51 100644 --- a/test/geo/json_test.exs +++ b/test/geo/json_test.exs @@ -431,6 +431,23 @@ defmodule Geo.JSON.Test do end end + test "Decode rejects geometries with garbage coordinates" do + json = """ + { + "properties": {}, + "geometry": { + "type": "Point", + "coordinates": {"x": 1.0, "y": 2.0} + }, + "type": "Feature" + } + """ + + assert_raise ArgumentError, fn -> + Jason.decode!(json) |> Geo.JSON.decode!() + end + end + property "encodes and decodes back to the correct Point struct" do check all( x <- float(),