From f597099ad352b77ec6879354bdf34e6f4fde48cc Mon Sep 17 00:00:00 2001 From: Dimitris Zorbas Date: Mon, 6 Feb 2023 20:30:38 +0100 Subject: [PATCH 1/2] Accept dates and datetimes in formatted string schemas --- lib/open_api_spex/cast/string.ex | 5 +++++ test/cast/string_test.exs | 10 ++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/open_api_spex/cast/string.ex b/lib/open_api_spex/cast/string.ex index 6820c04c..f9627029 100644 --- a/lib/open_api_spex/cast/string.ex +++ b/lib/open_api_spex/cast/string.ex @@ -6,10 +6,13 @@ defmodule OpenApiSpex.Cast.String do schema struct. """ + alias OpenApiSpex.{Cast, Cast.Error} @schema_fields [:maxLength, :minLength, :pattern] + def cast(%{value: %Date{} = date, schema: %{format: :date}}), do: {:ok, date} + def cast(%{value: value, schema: %{format: :date}} = ctx) when is_binary(value) do case Date.from_iso8601(value) do {:ok, %Date{} = date} -> @@ -20,6 +23,8 @@ defmodule OpenApiSpex.Cast.String do end end + def cast(%{value: %DateTime{} = date_time, schema: %{format: :"date-time"}}), do: {:ok, date_time} + def cast(%{value: value, schema: %{format: :"date-time"}} = ctx) when is_binary(value) do case DateTime.from_iso8601(value) do {:ok, %DateTime{} = date_time, _offset} -> diff --git a/test/cast/string_test.exs b/test/cast/string_test.exs index b22e7f84..427c616a 100644 --- a/test/cast/string_test.exs +++ b/test/cast/string_test.exs @@ -32,8 +32,11 @@ defmodule OpenApiSpex.CastStringTest do test "string with format (date time)" do schema = %Schema{type: :string, format: :"date-time"} - time_string = DateTime.utc_now() |> DateTime.to_string() + time = DateTime.utc_now() + time_string = to_string(time) + assert {:ok, %DateTime{}} = cast(value: time_string, schema: schema) + assert {:ok, ^time} = cast(value: time, schema: schema) assert {:error, [error]} = cast(value: "hello", schema: schema) assert error.reason == :invalid_format assert error.value == "hello" @@ -42,8 +45,11 @@ defmodule OpenApiSpex.CastStringTest do test "string with format (date)" do schema = %Schema{type: :string, format: :date} - date_string = DateTime.utc_now() |> DateTime.to_date() |> Date.to_string() + date = Date.utc_today() + date_string = to_string(date) + assert {:ok, %Date{}} = cast(value: date_string, schema: schema) + assert {:ok, ^date} = cast(value: date, schema: schema) assert {:error, [error]} = cast(value: "hello", schema: schema) assert error.reason == :invalid_format assert error.value == "hello" From 59aeefc48115b127deec3dc2afc9e80d48c383de Mon Sep 17 00:00:00 2001 From: Dimitris Zorbas Date: Tue, 7 Feb 2023 11:41:52 +0100 Subject: [PATCH 2/2] Pin Ubuntu version of CI test runs ubuntu-latest now points to 22.04 for which hex.pm does not have older OTP builds. See: * https://repo.hex.pm/builds/otp/ubuntu-22.04/builds.txt * https://repo.hex.pm/builds/otp/ubuntu-20.04/builds.txt --- .github/workflows/elixir.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml index 0842c541..e2cf4200 100644 --- a/.github/workflows/elixir.yml +++ b/.github/workflows/elixir.yml @@ -61,7 +61,7 @@ jobs: run: mix do deps.get, test test: - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 name: Test (OTP ${{matrix.otp}} / Elixir ${{matrix.elixir}}) strategy: matrix: