Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cast numbers as floats #611

Merged
merged 1 commit into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/open_api_spex/cast/number.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions test/cast/number_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -19,45 +19,45 @@ 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"
end

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"
end

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"
end

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"
Expand Down
Loading