Page MenuHomePhorge

No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/open_api_spex/cast/primitive.ex b/lib/open_api_spex/cast/primitive.ex
index 519d8ac..a0ee49c 100644
--- a/lib/open_api_spex/cast/primitive.ex
+++ b/lib/open_api_spex/cast/primitive.ex
@@ -1,37 +1,37 @@
defmodule OpenApiSpex.Cast.Primitive do
@moduledoc false
alias OpenApiSpex.Cast
## boolean
def cast_boolean(%{value: value}) when is_boolean(value) do
{:ok, value}
end
def cast_boolean(%{value: "true"}), do: {:ok, true}
def cast_boolean(%{value: "false"}), do: {:ok, false}
def cast_boolean(ctx) do
Cast.error(ctx, {:invalid_type, :boolean})
end
## number
- def cast_number(%{value: value}) when is_number(value) do
+ def cast_number(%{value: value}) when is_float(value) do
{:ok, value}
end
def cast_number(%{value: value}) when is_integer(value) do
{:ok, value / 1}
end
def cast_number(%{value: value} = ctx) when is_binary(value) do
case Float.parse(value) do
{value, ""} -> {:ok, value}
_ -> Cast.error(ctx, {:invalid_type, :number})
end
end
def cast_number(ctx) do
Cast.error(ctx, {:invalid_type, :number})
end
end
diff --git a/test/cast/primitive_test.exs b/test/cast/primitive_test.exs
index c77f8a7..5f54355 100644
--- a/test/cast/primitive_test.exs
+++ b/test/cast/primitive_test.exs
@@ -1,28 +1,28 @@
defmodule OpenApiSpex.PrimitiveTest do
use ExUnit.Case
alias OpenApiSpex.Cast
alias OpenApiSpex.Cast.{Primitive, Error}
import Primitive
describe "cast/3" do
test "boolean" do
assert cast_boolean(%Cast{value: true}) == {:ok, true}
assert cast_boolean(%Cast{value: false}) == {:ok, false}
assert cast_boolean(%Cast{value: "true"}) == {:ok, true}
assert cast_boolean(%Cast{value: "false"}) == {:ok, false}
assert {:error, [error]} = cast_boolean(%Cast{value: "other"})
assert %Error{reason: :invalid_type} = error
assert error.value == "other"
end
test "number" do
- assert cast_number(%Cast{value: 1}) == {:ok, 1.0}
- assert cast_number(%Cast{value: 1.5}) == {:ok, 1.5}
- assert cast_number(%Cast{value: "1"}) == {:ok, 1.0}
- assert cast_number(%Cast{value: "1.5"}) == {:ok, 1.5}
+ assert cast_number(%Cast{value: 1}) === {:ok, 1.0}
+ assert cast_number(%Cast{value: 1.5}) === {:ok, 1.5}
+ assert cast_number(%Cast{value: "1"}) === {:ok, 1.0}
+ assert cast_number(%Cast{value: "1.5"}) === {:ok, 1.5}
assert {:error, [error]} = cast_number(%Cast{value: "other"})
assert %Error{reason: :invalid_type} = error
assert error.value == "other"
end
end
end

File Metadata

Mime Type
text/x-diff
Expires
Mon, Nov 25, 6:17 PM (1 d, 8 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39994
Default Alt Text
(2 KB)

Event Timeline