open-api-spex / open_api_spex

Open API Specifications for Elixir Plug applications
Mozilla Public License 2.0
721 stars 185 forks source link

Operation is casted and validated even when `operation :operation_name, false` #399

Open stefanluptak opened 2 years ago

stefanluptak commented 2 years ago

When I have something like this:

defmodule MyAppWeb.MyController do
  use MyAppWeb, :controller
  use OpenApiSpex.ControllerSpecs

  plug OpenApiSpex.Plug.CastAndValidate, json_render_error_v2: true

  operation :dummy1,
    responses: %{200 => {"Index", "application/json", SomeSchema}}
  def dummy1(conn, params) do
    # ...
  end

  operation :dummy2, false
  def dummy2(conn, params) do
    # ...
  end
end

When I do a request to the dummy2 action, it's still being casted and validated which results into an error.

I believe, that operation with spec false should be skipped.

mbuhot commented 2 years ago

To clarify, are you hitting an error on this line?: https://github.com/open-api-spex/open_api_spex/blob/master/lib/open_api_spex/plug/cast_and_validate.ex#L111

The short-term fix would be to use a guard in your controller plug pipeline:

plug OpenApiSpex.Plug.CastAndValidate, json_render_error_v2: true when not action in [:dummy2]