Skip to content

Commit

Permalink
fix: ensure operation_id is always set on conn.private (#606)
Browse files Browse the repository at this point in the history
* fix: ensure operation_id is always set on conn.private when an operation is known
  • Loading branch information
msutkowski authored May 15, 2024
1 parent 78cf5fd commit 3f54e57
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
13 changes: 1 addition & 12 deletions lib/open_api_spex/operation2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,12 @@ defmodule OpenApiSpex.Operation2 do
{:ok,
conn
|> cast_conn(body)
|> maybe_replace_body(body, replace_params)
|> put_operation_id(operation)}
|> maybe_replace_body(body, replace_params)}
end
end

## Private functions

defp put_operation_id(conn, operation) do
private_data =
conn
|> Map.get(:private)
|> Map.get(:open_api_spex, %{})
|> Map.put(:operation_id, operation.operationId)

Plug.Conn.put_private(conn, :open_api_spex, private_data)
end

defp cast_conn(conn, body) do
private_data =
conn
Expand Down
11 changes: 11 additions & 0 deletions lib/open_api_spex/plug/cast_and_validate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ defmodule OpenApiSpex.Plug.CastAndValidate do
) do
{spec, operation_lookup} = PutApiSpec.get_spec_and_operation_lookup(conn)
operation = operation_lookup[operation_id]
conn = put_operation_id(conn, operation)

cast_opts = opts |> Map.take([:replace_params]) |> Map.to_list()

Expand Down Expand Up @@ -150,4 +151,14 @@ defmodule OpenApiSpex.Plug.CastAndValidate do
def call(_conn, _opts) do
raise ":open_api_spex was not found under :private. Maybe PutApiSpec was not called before?"
end

defp put_operation_id(conn, operation) do
private_data =
conn
|> Map.get(:private)
|> Map.get(:open_api_spex, %{})
|> Map.put(:operation_id, operation.operationId)

Plug.Conn.put_private(conn, :open_api_spex, private_data)
end
end
4 changes: 3 additions & 1 deletion lib/open_api_spex/plug/swagger_ui_oauth2_redirect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule OpenApiSpex.Plug.SwaggerUIOAuth2Redirect do

import Plug.Conn

alias OpenApiSpex.Plug.SwaggerUI

@html """
<!-- HTML for static distribution bundle build -->
<!doctype html>
Expand Down Expand Up @@ -104,7 +106,7 @@ defmodule OpenApiSpex.Plug.SwaggerUIOAuth2Redirect do

@impl Plug
def call(conn, config) do
html = render(OpenApiSpex.Plug.SwaggerUI.get_nonce(conn, config, :script))
html = render(SwaggerUI.get_nonce(conn, config, :script))

conn
|> put_resp_content_type("text/html")
Expand Down
13 changes: 12 additions & 1 deletion test/support/pet_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,18 @@ defmodule OpenApiSpexTest.PetController do
@doc """
Get a list of pets.
"""
@doc responses: [ok: {"Pet list", "application/json", Schemas.PetsResponse}],
@doc parameters: [
age: [
in: :query,
type: %Schema{type: :integer, minimum: 1},
description: "Age of the pet",
example: 1
]
],
responses: [
ok: {"Pet list", "application/json", Schemas.PetsResponse},
unprocessable_entity: OpenApiSpex.JsonErrorResponse.response()
],
operation_id: "listPets"
def index(conn, _params) do
json(conn, %Schemas.PetsResponse{
Expand Down
10 changes: 10 additions & 0 deletions test/test_assertions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ defmodule OpenApiSpex.TestAssertionsTest do
TestAssertions.assert_operation_response(conn)
end

test "is able to find the operationId via conn when there is an error" do
conn =
:get
|> Plug.Test.conn("/api/pets?age=notanumber")
|> OpenApiSpexTest.Router.call([])

assert conn.status == 422
TestAssertions.assert_operation_response(conn)
end

test "success with a response code range" do
conn =
:get
Expand Down

0 comments on commit 3f54e57

Please sign in to comment.