diff --git a/lib/open_api_spex/cast/number.ex b/lib/open_api_spex/cast/number.ex index 51e606e2..9fbadd11 100644 --- a/lib/open_api_spex/cast/number.ex +++ b/lib/open_api_spex/cast/number.ex @@ -6,7 +6,11 @@ defmodule OpenApiSpex.Cast.Number do # TODO We need a way to distinguish numbers in (JSON) body vs in request parameters # so we can reject strings values for properties of `type: :number` @spec cast(ctx :: Cast.t()) :: {:ok, Cast.t()} | Cast.Error.t() - def cast(%{value: value} = ctx) when is_number(value) do + def cast(%{value: value} = ctx) when is_integer(value) do + cast(%{ctx | value: value / 1}) + end + + def cast(%{value: value} = ctx) when is_float(value) do case cast_number(ctx) do {:cast, ctx} -> cast(ctx) result -> result diff --git a/test/cast/number_test.exs b/test/cast/number_test.exs index 398008d6..3568432a 100644 --- a/test/cast/number_test.exs +++ b/test/cast/number_test.exs @@ -8,7 +8,7 @@ defmodule OpenApiSpex.CastNumberTest do describe "cast/1" do test "basics" do schema = %Schema{type: :number} - assert cast(value: 1, schema: schema) === {:ok, 1} + assert cast(value: 1, schema: schema) === {:ok, 1.0} assert cast(value: 1.5, schema: schema) === {:ok, 1.5} assert cast(value: "1", schema: schema) === {:ok, 1.0} assert cast(value: "1.5", schema: schema) === {:ok, 1.5} @@ -19,11 +19,11 @@ defmodule OpenApiSpex.CastNumberTest do test "with minimum" do schema = %Schema{type: :number, minimum: 2} - assert cast(value: 3, schema: schema) === {:ok, 3} - assert cast(value: 2, schema: schema) === {:ok, 2} + assert cast(value: 3, schema: schema) === {:ok, 3.0} + assert cast(value: 2, schema: schema) === {:ok, 2.0} assert {:error, [error]} = cast(value: 1, schema: schema) assert error.reason == :minimum - assert error.value === 1 + assert error.value === 1.0 # error.length is the minimum assert error.length === 2 assert Error.message(error) =~ "smaller than inclusive minimum" @@ -31,11 +31,11 @@ defmodule OpenApiSpex.CastNumberTest do test "with maximum" do schema = %Schema{type: :number, maximum: 2} - assert cast(value: 1, schema: schema) === {:ok, 1} - assert cast(value: 2, schema: schema) === {:ok, 2} + assert cast(value: 1, schema: schema) === {:ok, 1.0} + assert cast(value: 2, schema: schema) === {:ok, 2.0} assert {:error, [error]} = cast(value: 3, schema: schema) assert error.reason === :maximum - assert error.value === 3 + assert error.value === 3.0 # error.length is the maximum assert error.length === 2 assert Error.message(error) =~ "larger than inclusive maximum" @@ -43,10 +43,10 @@ defmodule OpenApiSpex.CastNumberTest do test "with minimum w/ exclusiveMinimum" do schema = %Schema{type: :number, minimum: 2, exclusiveMinimum: true} - assert cast(value: 3, schema: schema) == {:ok, 3} + assert cast(value: 3, schema: schema) == {:ok, 3.0} assert {:error, [error]} = cast(value: 2, schema: schema) assert error.reason == :exclusive_min - assert error.value == 2 + assert error.value == 2.0 # error.length is the minimum assert error.length == 2 assert Error.message(error) =~ "smaller than exclusive minimum" @@ -54,10 +54,10 @@ defmodule OpenApiSpex.CastNumberTest do test "with maximum w/ exclusiveMaximum" do schema = %Schema{type: :number, maximum: 2, exclusiveMaximum: true} - assert cast(value: 1, schema: schema) == {:ok, 1} + assert cast(value: 1, schema: schema) == {:ok, 1.0} assert {:error, [error]} = cast(value: 2, schema: schema) assert error.reason == :exclusive_max - assert error.value == 2 + assert error.value == 2.0 # error.length is the maximum assert error.length == 2 assert Error.message(error) =~ "larger than exclusive maximum"