Skip to content

Commit 572130d

Browse files
bopmzorbash
andauthored
Fix OTP-28 support (#672)
* 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. * Fix formatting --------- Co-authored-by: Dimitris Zorbas <[email protected]>
1 parent 2069321 commit 572130d

File tree

4 files changed

+9
-6
lines changed

4 files changed

+9
-6
lines changed

lib/open_api_spex/cast_parameters.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ defmodule OpenApiSpex.CastParameters do
44
alias OpenApiSpex.Cast.Error
55
alias Plug.Conn
66

7-
@default_parsers %{~r/^application\/.*json.*$/ => OpenApi.json_encoder()}
8-
97
@spec cast(Plug.Conn.t(), Operation.t(), OpenApi.t(), opts :: [OpenApiSpex.cast_opt()]) ::
108
{:error, [Error.t()]} | {:ok, Conn.t()}
119
def cast(conn, operation, spec, opts \\ []) do
@@ -120,7 +118,7 @@ defmodule OpenApiSpex.CastParameters do
120118
opts
121119
) do
122120
parsers = Map.get(ext || %{}, "x-parameter-content-parsers", %{})
123-
parsers = Map.merge(@default_parsers, parsers)
121+
parsers = Map.merge(default_parsers(), parsers)
124122

125123
conn
126124
|> get_params_by_location(
@@ -134,6 +132,9 @@ defmodule OpenApiSpex.CastParameters do
134132
end
135133
end
136134

135+
defp default_parsers,
136+
do: %{~r/^application\/.*json.*$/ => OpenApi.json_encoder()}
137+
137138
defp pre_parse_parameters(%{} = parameters, %{} = parameters_context, parsers) do
138139
Enum.reduce_while(parameters, Map.new(), fn {key, value}, acc ->
139140
case pre_parse_parameter(value, Map.get(parameters_context, key, %{}), parsers) do

lib/open_api_spex/controller_specs.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,9 @@ defmodule OpenApiSpex.ControllerSpecs do
386386

387387
extensions =
388388
spec
389-
|> Enum.filter(fn {key, _val} -> is_atom(key) && String.starts_with?(to_string(key), "x-") end)
389+
|> Enum.filter(fn {key, _val} ->
390+
is_atom(key) && String.starts_with?(to_string(key), "x-")
391+
end)
390392
|> Map.new(fn {key, value} -> {to_string(key), value} end)
391393

392394
%Operation{

test/cast/string_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ defmodule OpenApiSpex.CastStringTest do
2727
assert {:error, [error]} = cast(value: "hello", schema: schema)
2828
assert error.reason == :invalid_format
2929
assert error.value == "hello"
30-
assert error.format == ~r/\d-\d/
30+
assert error.format.source == "\\d-\\d"
3131
end
3232

3333
test "string with format (date time)" do

test/support/schemas.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ defmodule OpenApiSpexTest.Schemas do
186186
type: :object,
187187
properties: %{
188188
id: %Schema{type: :integer, description: "User ID"},
189-
name: %Schema{type: :string, description: "User name", pattern: ~r/[a-zA-Z][a-zA-Z0-9_]+/},
189+
name: %Schema{type: :string, description: "User name", pattern: "[a-zA-Z][a-zA-Z0-9_]+"},
190190
email: %Schema{type: :string, description: "Email address", format: :email},
191191
password: %Schema{type: :string, description: "Login password", writeOnly: true},
192192
age: %Schema{type: :integer, description: "Age"},

0 commit comments

Comments
 (0)