From 6f716a7214189d41fd5702a72c6cbcbda4778dc7 Mon Sep 17 00:00:00 2001 From: Sergey Moiseev Date: Tue, 1 Jul 2025 19:06:08 +0300 Subject: [PATCH 1/2] Refactor: Update default parsers and schema pattern definitions This commit introduces changes to `OpenApiSpex.CastParameters` and `OpenApiSpexTest.Schemas`. - Moved `@default_parsers` to a private function `default_parsers/0` in `OpenApiSpex.CastParameters` to ensure it's evaluated at runtime, preventing potential compilation issues with `OpenApi.json_encoder()`. - Updated the `pattern` definition in `OpenApiSpexTest.Schemas` to use a string literal instead of a regex literal for consistency and to avoid potential issues with regex compilation. Fix: Adjust string pattern test assertion Following the refactoring, a test in `OpenApiSpex.CastStringTest` failed due to a change in how regex patterns were handled. This fix corrects the assertion for string pattern matching to compare the `source` of the regex instead of the regex struct directly, resolving the test failure. --- lib/open_api_spex/cast_parameters.ex | 7 +++++-- test/cast/string_test.exs | 2 +- test/support/schemas.ex | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/open_api_spex/cast_parameters.ex b/lib/open_api_spex/cast_parameters.ex index 1155cb79..61936ceb 100644 --- a/lib/open_api_spex/cast_parameters.ex +++ b/lib/open_api_spex/cast_parameters.ex @@ -4,7 +4,7 @@ defmodule OpenApiSpex.CastParameters do alias OpenApiSpex.Cast.Error alias Plug.Conn - @default_parsers %{~r/^application\/.*json.*$/ => OpenApi.json_encoder()} + @spec cast(Plug.Conn.t(), Operation.t(), OpenApi.t(), opts :: [OpenApiSpex.cast_opt()]) :: {:error, [Error.t()]} | {:ok, Conn.t()} @@ -120,7 +120,7 @@ defmodule OpenApiSpex.CastParameters do opts ) do parsers = Map.get(ext || %{}, "x-parameter-content-parsers", %{}) - parsers = Map.merge(@default_parsers, parsers) + parsers = Map.merge(default_parsers(), parsers) conn |> get_params_by_location( @@ -134,6 +134,9 @@ defmodule OpenApiSpex.CastParameters do end end + defp default_parsers, + do: %{~r/^application\/.*json.*$/ => OpenApi.json_encoder()} + defp pre_parse_parameters(%{} = parameters, %{} = parameters_context, parsers) do Enum.reduce_while(parameters, Map.new(), fn {key, value}, acc -> case pre_parse_parameter(value, Map.get(parameters_context, key, %{}), parsers) do diff --git a/test/cast/string_test.exs b/test/cast/string_test.exs index 427c616a..eabb5703 100644 --- a/test/cast/string_test.exs +++ b/test/cast/string_test.exs @@ -27,7 +27,7 @@ defmodule OpenApiSpex.CastStringTest do assert {:error, [error]} = cast(value: "hello", schema: schema) assert error.reason == :invalid_format assert error.value == "hello" - assert error.format == ~r/\d-\d/ + assert error.format.source == "\\d-\\d" end test "string with format (date time)" do diff --git a/test/support/schemas.ex b/test/support/schemas.ex index 65f687fd..0f93748f 100644 --- a/test/support/schemas.ex +++ b/test/support/schemas.ex @@ -186,7 +186,7 @@ defmodule OpenApiSpexTest.Schemas do type: :object, properties: %{ id: %Schema{type: :integer, description: "User ID"}, - name: %Schema{type: :string, description: "User name", pattern: ~r/[a-zA-Z][a-zA-Z0-9_]+/}, + name: %Schema{type: :string, description: "User name", pattern: "[a-zA-Z][a-zA-Z0-9_]+"}, email: %Schema{type: :string, description: "Email address", format: :email}, password: %Schema{type: :string, description: "Login password", writeOnly: true}, age: %Schema{type: :integer, description: "Age"}, From c7ee013994ecd098fc22bbf554ae73567b215b00 Mon Sep 17 00:00:00 2001 From: Dimitris Zorbas Date: Tue, 1 Jul 2025 20:27:57 +0300 Subject: [PATCH 2/2] Fix formatting --- lib/open_api_spex/cast_parameters.ex | 2 -- lib/open_api_spex/controller_specs.ex | 4 +++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/open_api_spex/cast_parameters.ex b/lib/open_api_spex/cast_parameters.ex index 61936ceb..9ec6f92b 100644 --- a/lib/open_api_spex/cast_parameters.ex +++ b/lib/open_api_spex/cast_parameters.ex @@ -4,8 +4,6 @@ defmodule OpenApiSpex.CastParameters do alias OpenApiSpex.Cast.Error alias Plug.Conn - - @spec cast(Plug.Conn.t(), Operation.t(), OpenApi.t(), opts :: [OpenApiSpex.cast_opt()]) :: {:error, [Error.t()]} | {:ok, Conn.t()} def cast(conn, operation, spec, opts \\ []) do diff --git a/lib/open_api_spex/controller_specs.ex b/lib/open_api_spex/controller_specs.ex index 144843e0..d1595579 100644 --- a/lib/open_api_spex/controller_specs.ex +++ b/lib/open_api_spex/controller_specs.ex @@ -386,7 +386,9 @@ defmodule OpenApiSpex.ControllerSpecs do extensions = spec - |> Enum.filter(fn {key, _val} -> is_atom(key) && String.starts_with?(to_string(key), "x-") end) + |> Enum.filter(fn {key, _val} -> + is_atom(key) && String.starts_with?(to_string(key), "x-") + end) |> Map.new(fn {key, value} -> {to_string(key), value} end) %Operation{