Skip to content

Commit

Permalink
Also handle non-numeric, non-list, non-string coords
Browse files Browse the repository at this point in the history
  • Loading branch information
s3cur3 committed Aug 30, 2024
1 parent e0aed25 commit 2c7a048
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/geo/json/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions test/geo/json_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 2c7a048

Please sign in to comment.