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

CastAndValidate plug does not set Cast.read_write_scope #499

Closed
maennchen opened this issue Sep 26, 2022 · 4 comments · Fixed by #572
Closed

CastAndValidate plug does not set Cast.read_write_scope #499

maennchen opened this issue Sep 26, 2022 · 4 comments · Fixed by #572

Comments

@maennchen
Copy link

The plug OpenApiSpex.Plug.CastAndValidate does not set the OpenApiSpex.Cast.read_write_scope field.

This leads to the plug considering readOnly fields to be required in the request body.

@maennchen
Copy link
Author

Something along those lines should fix that issue:

diff --git a/lib/open_api_spex/cast.ex b/lib/open_api_spex/cast.ex
index 81b5564..f35d3b4 100644
--- a/lib/open_api_spex/cast.ex
+++ b/lib/open_api_spex/cast.ex
@@ -100,10 +100,21 @@ defmodule OpenApiSpex.Cast do
 
   """
 
-  @spec cast(schema_or_reference | nil, term(), map(), [cast_opt()]) ::
+  @spec cast(schema_or_reference | nil, term(), map(), [
+          cast_opt() | {:read_write_scope, read_write_scope()}
+        ]) ::
           {:ok, term()} | {:error, [Error.t()]}
   def cast(schema, value, schemas \\ %{}, opts \\ []) do
-    ctx = %__MODULE__{schema: schema, value: value, schemas: schemas, opts: opts}
+    {read_write_scope, opts} = Keyword.pop(opts, :read_write_scope, nil)
+
+    ctx = %__MODULE__{
+      schema: schema,
+      value: value,
+      schemas: schemas,
+      opts: opts,
+      read_write_scope: read_write_scope
+    }
+
     cast(ctx)
   end
 

diff --git a/lib/open_api_spex/plug/cast_and_validate.ex b/lib/open_api_spex/plug/cast_and_validate.ex
index e8af40f..ce7fa94 100644
--- a/lib/open_api_spex/plug/cast_and_validate.ex
+++ b/lib/open_api_spex/plug/cast_and_validate.ex
@@ -79,7 +79,13 @@ defmodule OpenApiSpex.Plug.CastAndValidate do
 
     cast_opts = opts |> Map.take([:replace_params]) |> Map.to_list()
 
-    case OpenApiSpex.cast_and_validate(spec, operation, conn, nil, cast_opts) do
+    case OpenApiSpex.cast_and_validate(
+           spec,
+           operation,
+           conn,
+           nil,
+           cast_opts ++ [read_write_scope: :write]
+         ) do
       {:ok, conn} ->
         conn

@kaharlichenko
Copy link

Hi. Is there anything I can do to help to get this fixed? There are two PRs trying to address this issue. Are they missing anything?

@mbuhot
Copy link
Collaborator

mbuhot commented May 24, 2024

Hi @kaharlichenko, I've merged #572. Please try it out and if it's working for you we can tag a release.

@kaharlichenko
Copy link

@mbuhot, this bit was important, but alas it was insufficient. The plug still doesn't do anything to set the read_write_scope. I tried to fill this gap in #612. Please take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants