Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F115671
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
20 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/open_api_spex/cast/error.ex b/lib/open_api_spex/cast/error.ex
index 1884bbe..448ca8f 100644
--- a/lib/open_api_spex/cast/error.ex
+++ b/lib/open_api_spex/cast/error.ex
@@ -1,368 +1,392 @@
defmodule OpenApiSpex.Cast.Error do
alias OpenApiSpex.TermType
@type all_of_error :: {:all_of, [String.t()]}
@type any_of_error :: {:any_of, [String.t()]}
@type exclusive_max_error :: {:exclusive_max, non_neg_integer(), non_neg_integer()}
@type exclusive_min_error :: {:exclusive_min, non_neg_integer(), non_neg_integer()}
@type invalid_enum_error :: {:invalid_enum}
@type invalid_format_error :: {:invalid_format, any()}
@type invalid_schema_error :: {:invalid_schema_type}
@type invalid_type_error :: {:invalid_type, String.t() | atom()}
@type max_items_error :: {:max_items, non_neg_integer(), non_neg_integer()}
@type max_length_error :: {:max_length, non_neg_integer()}
@type max_properties_error :: {:max_properties, non_neg_integer(), non_neg_integer()}
@type maximum_error :: {:maximum, integer(), integer()}
@type min_items_error :: {:min_items, non_neg_integer(), non_neg_integer()}
@type min_length_error :: {:min_length, non_neg_integer()}
@type minimum_error :: {:minimum, integer(), integer()}
@type missing_field_error :: {:missing_field, String.t() | atom()}
+ @type missing_header_error :: {:missing_header, String.t() | atom()}
+ @type invalid_header_error :: {:invalid_header, String.t() | atom()}
@type multiple_of_error :: {:multiple_of, non_neg_integer(), non_neg_integer()}
@type no_value_for_discriminator_error :: {:no_value_for_discriminator, String.t() | atom()}
@type invalid_discriminator_value_error :: {:invalid_discriminator_value, String.t() | atom()}
@type null_value_error :: {:null_value}
@type one_of_error :: {:one_of, [String.t()]}
@type unexpected_field_error :: {:unexpected_field, String.t() | atom()}
@type unique_items_error :: {:unique_items}
@type reason ::
:all_of
| :any_of
| :invalid_schema_type
| :exclusive_max
| :exclusive_min
| :invalid_discriminator_value
| :invalid_enum
| :invalid_format
| :invalid_type
| :max_items
| :max_length
| :max_properties
| :maximum
| :min_items
| :min_length
| :minimum
| :missing_field
+ | :missing_header
+ | :invalid_header
| :multiple_of
| :no_value_for_discriminator
| :null_value
| :one_of
| :unexpected_field
| :unique_items
@type args ::
all_of_error()
| any_of_error()
| invalid_schema_error()
| exclusive_max_error()
| exclusive_min_error()
| invalid_discriminator_value_error()
| invalid_enum_error()
| invalid_format_error()
| invalid_type_error()
| max_items_error()
| max_length_error()
| max_properties_error()
| maximum_error()
| min_items_error()
| min_length_error()
| minimum_error()
| missing_field_error()
+ | missing_header_error()
+ | invalid_header_error()
| multiple_of_error()
| no_value_for_discriminator_error()
| null_value_error()
| one_of_error()
| unexpected_field_error()
| unique_items_error()
@type t :: %__MODULE__{
reason: reason(),
value: any(),
format: String.t(),
name: String.t(),
path: list(String.t()),
length: non_neg_integer(),
meta: map()
}
defstruct reason: nil,
value: nil,
format: nil,
type: nil,
name: nil,
path: [],
length: 0,
meta: %{}
@spec new(map(), args()) :: %__MODULE__{}
def new(ctx, {:invalid_schema_type}) do
%__MODULE__{reason: :invalid_schema_type, type: ctx.schema.type}
|> add_context_fields(ctx)
end
def new(ctx, {:null_value}) do
type = ctx.schema && ctx.schema.type
%__MODULE__{reason: :null_value, type: type}
|> add_context_fields(ctx)
end
def new(ctx, {:all_of, schema_detail}) do
%__MODULE__{reason: :all_of, meta: %{invalid_schema: schema_detail}}
|> add_context_fields(ctx)
end
def new(ctx, {:any_of, schema_names}) do
%__MODULE__{reason: :any_of, meta: %{failed_schemas: schema_names}}
|> add_context_fields(ctx)
end
def new(ctx, {:one_of, schema_names}) do
%__MODULE__{reason: :one_of, meta: %{failed_schemas: schema_names}}
|> add_context_fields(ctx)
end
def new(ctx, {:min_length, length}) do
%__MODULE__{reason: :min_length, length: length}
|> add_context_fields(ctx)
end
def new(ctx, {:max_length, length}) do
%__MODULE__{reason: :max_length, length: length}
|> add_context_fields(ctx)
end
def new(ctx, {:multiple_of, multiple, item_count}) do
%__MODULE__{reason: :multiple_of, length: multiple, value: item_count}
|> add_context_fields(ctx)
end
def new(ctx, {:unique_items}) do
%__MODULE__{reason: :unique_items}
|> add_context_fields(ctx)
end
def new(ctx, {:min_items, min_items, item_count}) do
%__MODULE__{reason: :min_items, length: min_items, value: item_count}
|> add_context_fields(ctx)
end
def new(ctx, {:max_items, max_items, value}) do
%__MODULE__{reason: :max_items, length: max_items, value: value}
|> add_context_fields(ctx)
end
def new(ctx, {:minimum, minimum, value}) do
%__MODULE__{reason: :minimum, length: minimum, value: value}
|> add_context_fields(ctx)
end
def new(ctx, {:maximum, maximum, value}) do
%__MODULE__{reason: :maximum, length: maximum, value: value}
|> add_context_fields(ctx)
end
def new(ctx, {:exclusive_min, exclusive_min, value}) do
%__MODULE__{reason: :exclusive_min, length: exclusive_min, value: value}
|> add_context_fields(ctx)
end
def new(ctx, {:exclusive_max, exclusive_max, value}) do
%__MODULE__{reason: :exclusive_max, length: exclusive_max, value: value}
|> add_context_fields(ctx)
end
def new(ctx, {:invalid_type, type}) do
%__MODULE__{reason: :invalid_type, type: type}
|> add_context_fields(ctx)
end
def new(ctx, {:invalid_format, format}) do
%__MODULE__{reason: :invalid_format, format: format}
|> add_context_fields(ctx)
end
def new(ctx, {:invalid_enum}) do
%__MODULE__{reason: :invalid_enum}
|> add_context_fields(ctx)
end
def new(ctx, {:unexpected_field, name}) do
%__MODULE__{reason: :unexpected_field, name: name}
|> add_context_fields(ctx)
end
def new(ctx, {:missing_field, name}) do
%__MODULE__{reason: :missing_field, name: name}
|> add_context_fields(ctx)
end
+ def new(ctx, {:missing_header, name}) do
+ %__MODULE__{reason: :missing_header, name: name}
+ |> add_context_fields(ctx)
+ end
+
+ def new(ctx, {:invalid_header, name}) do
+ %__MODULE__{reason: :invalid_header, name: name}
+ |> add_context_fields(ctx)
+ end
+
def new(ctx, {:no_value_for_discriminator, field}) do
%__MODULE__{reason: :no_value_for_discriminator, name: field}
|> add_context_fields(ctx)
end
def new(ctx, {:invalid_discriminator_value, field}) do
%__MODULE__{reason: :invalid_discriminator_value, name: field}
|> add_context_fields(ctx)
end
def new(ctx, {:max_properties, max_properties, property_count}) do
%__MODULE__{
reason: :max_properties,
meta: %{max_properties: max_properties, property_count: property_count}
}
|> add_context_fields(ctx)
end
@spec message(t()) :: String.t()
def message(%{reason: :invalid_schema_type, type: type}) do
"Invalid schema.type. Got: #{inspect(type)}"
end
def message(%{reason: :null_value} = error) do
case error.type do
nil -> "null value"
type -> "null value where #{type} expected"
end
end
def message(%{reason: :all_of, meta: %{invalid_schema: invalid_schema}}) do
"Failed to cast value as #{invalid_schema}. Value must be castable using `allOf` schemas listed."
end
def message(%{reason: :any_of, meta: %{failed_schemas: failed_schemas}}) do
"Failed to cast value using any of: #{failed_schemas}"
end
def message(%{reason: :one_of, meta: %{failed_schemas: failed_schemas}}) do
"Failed to cast value to one of: #{failed_schemas}"
end
def message(%{reason: :min_length, length: length}) do
"String length is smaller than minLength: #{length}"
end
def message(%{reason: :max_length, length: length}) do
"String length is larger than maxLength: #{length}"
end
def message(%{reason: :unique_items}) do
"Array items must be unique"
end
def message(%{reason: :min_items, length: min, value: count}) do
"Array length #{count} is smaller than minItems: #{min}"
end
def message(%{reason: :max_items, length: max, value: count}) do
"Array length #{count} is larger than maxItems: #{max}"
end
def message(%{reason: :multiple_of, length: multiple, value: count}) do
"#{count} is not a multiple of #{multiple}"
end
def message(%{reason: :exclusive_max, length: max, value: value})
when value >= max do
"#{value} is larger than exclusive maximum #{max}"
end
def message(%{reason: :maximum, length: max, value: value})
when value > max do
"#{value} is larger than inclusive maximum #{max}"
end
def message(%{reason: :exclusive_min, length: min, value: value})
when value <= min do
"#{value} is smaller than exclusive minimum #{min}"
end
def message(%{reason: :minimum, length: min, value: value})
when value < min do
"#{value} is smaller than inclusive minimum #{min}"
end
def message(%{reason: :invalid_type, type: type, value: value}) do
"Invalid #{type}. Got: #{TermType.type(value)}"
end
def message(%{reason: :invalid_format, format: format}) do
"Invalid format. Expected #{inspect(format)}"
end
def message(%{reason: :invalid_enum}) do
"Invalid value for enum"
end
def message(%{reason: :polymorphic_failed, type: polymorphic_type}) do
"Failed to cast to any schema in #{polymorphic_type}"
end
def message(%{reason: :unexpected_field, name: name}) do
"Unexpected field: #{safe_string(name)}"
end
def message(%{reason: :no_value_for_discriminator, name: field}) do
"Value used as discriminator for `#{field}` matches no schemas"
end
def message(%{reason: :invalid_discriminator_value, name: field}) do
"No value provided for required discriminator `#{field}`"
end
def message(%{reason: :unknown_schema, name: name}) do
"Unknown schema: #{name}"
end
def message(%{reason: :missing_field, name: name}) do
"Missing field: #{name}"
end
+ def message(%{reason: :missing_header, name: name}) do
+ "Missing header: #{name}"
+ end
+
+ def message(%{reason: :invalid_header, name: name}) do
+ "Invalid value for header: #{name}"
+ end
+
def message(%{reason: :max_properties, meta: meta}) do
"Object property count #{meta.property_count} is greater than maxProperties: #{
meta.max_properties
}"
end
def message_with_path(error) do
prepend_path(error, message(error))
end
def path_to_string(%{path: path} = _error) do
path =
if path == [] do
""
else
path |> Enum.map(&to_string/1) |> Path.join()
end
"/" <> path
end
defp add_context_fields(error, ctx) do
%{error | path: Enum.reverse(ctx.path), value: ctx.value}
end
defp prepend_path(error, message) do
path =
case error.path do
[] -> "#"
_ -> "#" <> path_to_string(error)
end
path <> ": " <> message
end
defp safe_string(string) do
to_string(string) |> String.slice(0..39)
end
end
defimpl String.Chars, for: OpenApiSpex.Cast.Error do
def to_string(error) do
OpenApiSpex.Cast.Error.message(error)
end
end
diff --git a/lib/open_api_spex/operation2.ex b/lib/open_api_spex/operation2.ex
index 465b7b8..8644d3b 100644
--- a/lib/open_api_spex/operation2.ex
+++ b/lib/open_api_spex/operation2.ex
@@ -1,38 +1,48 @@
defmodule OpenApiSpex.Operation2 do
@moduledoc """
Casts and validates a request from a Plug conn.
"""
alias OpenApiSpex.{
Cast,
CastParameters,
Operation,
RequestBody,
Schema
}
alias OpenApiSpex.Cast.Error
alias Plug.Conn
@spec cast(Operation.t(), Conn.t(), String.t() | nil, Schema.schemas()) ::
{:error, [Error.t()]} | {:ok, Conn.t()}
def cast(operation = %Operation{}, conn = %Conn{}, content_type, schemas) do
with {:ok, conn} <- cast_parameters(conn, operation, schemas),
{:ok, body} <-
cast_request_body(operation.requestBody, conn.body_params, content_type, schemas) do
{:ok, %{conn | body_params: body}}
end
end
## Private functions
defp cast_parameters(conn, operation, schemas) do
CastParameters.cast(conn, operation, schemas)
end
defp cast_request_body(nil, _, _, _), do: {:ok, %{}}
+ defp cast_request_body(%{required: false}, _, nil, _), do: {:ok, %{}}
+
+ defp cast_request_body(%{required: true}, _, nil, _) do
+ {:error, [Error.new(%{path: [], value: nil}, {:missing_header, "content-type"})]}
+ end
+
defp cast_request_body(%RequestBody{content: content}, params, content_type, schemas) do
- schema = content[content_type].schema
- Cast.cast(schema, params, schemas)
+ case content do
+ %{^content_type => media_type} ->
+ Cast.cast(media_type.schema, params, schemas)
+ _ ->
+ {:error, [Error.new(%{path: [], value: content_type}, {:invalid_header, "content-type"})]}
+ end
end
end
diff --git a/test/operation2_test.exs b/test/operation2_test.exs
index 7a0918d..d5377d2 100644
--- a/test/operation2_test.exs
+++ b/test/operation2_test.exs
@@ -1,211 +1,236 @@
defmodule OpenApiSpex.Operation2Test do
use ExUnit.Case
alias OpenApiSpex.{Operation, Operation2, Schema}
alias OpenApiSpex.Cast.Error
defmodule SchemaFixtures do
@user %Schema{
type: :object,
properties: %{
user: %Schema{
type: :object,
properties: %{
email: %Schema{type: :string}
}
}
}
}
@user_list %Schema{
type: :array,
items: @user
}
@schemas %{"User" => @user, "UserList" => @user_list}
def user, do: @user
def user_list, do: @user_list
def schemas, do: @schemas
end
defmodule OperationFixtures do
@user_index %Operation{
operationId: "UserController.index",
parameters: [
Operation.parameter(:name, :query, :string, "Filter by user name"),
Operation.parameter(:age, :query, :integer, "User's age")
],
responses: %{
200 => Operation.response("User", "application/json", SchemaFixtures.user())
}
}
def user_index, do: @user_index
@create_user %Operation{
operationId: "UserController.create",
parameters: [
Operation.parameter(:name, :query, :string, "Filter by user name")
],
requestBody:
Operation.request_body("request body", "application/json", SchemaFixtures.user(),
required: true
),
responses: %{
200 => Operation.response("User list", "application/json", SchemaFixtures.user_list())
}
}
def create_user, do: @create_user
end
defmodule SpecModule do
@behaviour OpenApiSpex.OpenApi
@impl OpenApiSpex.OpenApi
def spec do
paths = %{
"/users" => %{
"post" => OperationFixtures.create_user()
}
}
%OpenApiSpex.OpenApi{
info: nil,
paths: paths,
components: %{
schemas: SchemaFixtures.schemas()
}
}
end
end
defmodule RenderError do
def init(_) do
nil
end
def call(_conn, _errors) do
raise "should not have errors"
end
end
describe "cast/4" do
test "cast request body" do
conn = create_conn(%{"user" => %{"email" => "foo@bar.com"}})
assert {:ok, conn} =
Operation2.cast(
OperationFixtures.create_user(),
conn,
"application/json",
SchemaFixtures.schemas()
)
assert %Plug.Conn{} = conn
end
test "cast request body - invalid data type" do
conn = create_conn(%{"user" => %{"email" => 123}})
assert {:error, errors} =
Operation2.cast(
OperationFixtures.create_user(),
conn,
"application/json",
SchemaFixtures.schemas()
)
assert [error] = errors
assert %Error{} = error
assert error.reason == :invalid_type
end
test "validate undefined query param name" do
query_params = %{"unknown" => "asdf"}
assert {:error, [error]} = do_index_cast(query_params)
assert %Error{} = error
assert error.reason == :unexpected_field
assert error.name == "unknown"
assert error.path == ["unknown"]
end
test "validate invalid data type for query param" do
query_params = %{"age" => "asdf"}
assert {:error, [error]} = do_index_cast(query_params)
assert %Error{} = error
assert error.reason == :invalid_type
assert error.type == :integer
assert error.value == "asdf"
end
test "validate missing required query param" do
parameter =
Operation.parameter(:name, :query, :string, "Filter by user name", required: true)
operation = %{OperationFixtures.user_index() | parameters: [parameter]}
assert {:error, [error]} = do_index_cast(%{}, operation: operation)
assert %Error{} = error
assert error.reason == :missing_field
assert error.name == :name
end
+ test "validate missing content-type header for required requestBody" do
+ conn = :post |> Plug.Test.conn("/api/users/") |> Plug.Conn.fetch_query_params()
+ operation = OperationFixtures.create_user()
+ assert {:error, [%Error{reason: :missing_header, name: "content-type"}]} = Operation2.cast(
+ operation,
+ conn,
+ nil,
+ SchemaFixtures.schemas()
+ )
+ end
+
+ test "validate invalid content-type header for required requestBody" do
+ conn =
+ create_conn(%{})
+ |> Plug.Conn.put_req_header("content-type", "text/html")
+
+ operation = OperationFixtures.create_user()
+ assert {:error, [%Error{reason: :invalid_header, name: "content-type"}]} = Operation2.cast(
+ operation,
+ conn,
+ "text/html",
+ SchemaFixtures.schemas()
+ )
+ end
+
test "validate invalid value for integer range" do
parameter =
Operation.parameter(
:age,
:query,
%Schema{type: :integer, minimum: 1, maximum: 99},
"Filter by user age",
required: true
)
operation = %{OperationFixtures.user_index() | parameters: [parameter]}
assert {:error, [error]} = do_index_cast(%{"age" => 100}, operation: operation)
assert %Error{} = error
assert error.reason == :maximum
assert {:error, [error]} = do_index_cast(%{"age" => 0}, operation: operation)
assert %Error{} = error
assert error.reason == :minimum
end
defp do_index_cast(query_params, opts \\ []) do
conn =
:get
|> Plug.Test.conn("/api/users?" <> URI.encode_query(query_params))
|> Plug.Conn.put_req_header("content-type", "application/json")
|> Plug.Conn.fetch_query_params()
|> build_params()
operation = opts[:operation] || OperationFixtures.user_index()
Operation2.cast(
operation,
conn,
"application/json",
SchemaFixtures.schemas()
)
end
defp create_conn(body_params) do
:post
|> Plug.Test.conn("/api/users")
|> Plug.Conn.put_req_header("content-type", "application/json")
|> Plug.Conn.fetch_query_params()
|> Map.put(:body_params, body_params)
|> build_params()
end
defp build_params(conn) do
params =
conn.path_params
|> Map.merge(conn.query_params)
|> Map.merge(conn.body_params)
%{conn | params: params}
end
end
end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Nov 28, 12:36 PM (1 d, 21 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41040
Default Alt Text
(20 KB)
Attached To
Mode
R22 open_api_spex
Attached
Detach File
Event Timeline
Log In to Comment