Page MenuHomePhorge

No OneTemporary

Size
23 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/open_api_spex.ex b/lib/open_api_spex.ex
index bcd1142..65a8acd 100644
--- a/lib/open_api_spex.ex
+++ b/lib/open_api_spex.ex
@@ -1,274 +1,279 @@
defmodule OpenApiSpex do
@moduledoc """
Provides the entry-points for defining schemas, validating and casting.
"""
alias OpenApiSpex.{
Components,
OpenApi,
Operation,
Operation2,
Reference,
Schema,
SchemaException,
SchemaResolver,
SchemaConsistency
}
alias OpenApiSpex.Cast.Error
@doc """
Adds schemas to the api spec from the modules specified in the Operations.
Eg, if the response schema for an operation is defined with:
responses: %{
200 => Operation.response("User", "application/json", UserResponse)
}
Then the `UserResponse.schema()` function will be called to load the schema, and
a `Reference` to the loaded schema will be used in the operation response.
See `OpenApiSpex.schema` macro for a convenient syntax for defining schema modules.
"""
@spec resolve_schema_modules(OpenApi.t()) :: OpenApi.t()
def resolve_schema_modules(spec = %OpenApi{}) do
SchemaResolver.resolve_schema_modules(spec)
end
@doc """
Cast and validate a value against a given Schema.
"""
def cast_value(value, schema = %schema_mod{}) when schema_mod in [Schema, Reference] do
OpenApiSpex.Cast.cast(schema, value)
end
@doc """
Cast and validate a value against a given Schema belonging to a given OpenApi spec.
"""
def cast_value(value, schema = %schema_mod{}, spec = %OpenApi{})
when schema_mod in [Schema, Reference] do
OpenApiSpex.Cast.cast(schema, value, spec.components.schemas)
end
def cast_and_validate(
spec = %OpenApi{},
operation = %Operation{},
conn = %Plug.Conn{},
content_type \\ nil
) do
Operation2.cast(operation, conn, content_type, spec.components)
end
@doc """
Cast params to conform to a `OpenApiSpex.Schema`.
See `OpenApiSpex.Schema.cast/3` for additional examples and details.
"""
@spec cast(OpenApi.t(), Schema.t() | Reference.t(), any) :: {:ok, any} | {:error, String.t()}
@deprecated "Use OpenApiSpex.cast_value/3 or cast_value/2 instead"
def cast(spec = %OpenApi{}, schema = %Schema{}, params) do
Schema.cast(schema, params, spec.components.schemas)
end
def cast(spec = %OpenApi{}, schema = %Reference{}, params) do
Schema.cast(schema, params, spec.components.schemas)
end
@doc """
Cast all params in `Plug.Conn` to conform to the schemas for `OpenApiSpex.Operation`.
Returns `{:ok, Plug.Conn.t}` with `params` and `body_params` fields updated if successful,
or `{:error, reason}` if casting fails.
`content_type` may optionally be supplied to select the `requestBody` schema.
"""
@spec cast(OpenApi.t(), Operation.t(), Plug.Conn.t(), content_type | nil) ::
{:ok, Plug.Conn.t()} | {:error, String.t()}
when content_type: String.t()
@deprecated "Use OpenApiSpex.cast_and_validate/3 instead"
def cast(spec = %OpenApi{}, operation = %Operation{}, conn = %Plug.Conn{}, content_type \\ nil) do
Operation.cast(operation, conn, content_type, spec.components.schemas)
end
@doc """
Validate params against `OpenApiSpex.Schema`.
See `OpenApiSpex.Schema.validate/3` for examples of error messages.
"""
@spec validate(OpenApi.t(), Schema.t() | Reference.t(), any) :: :ok | {:error, String.t()}
@deprecated "Use OpenApiSpex.cast_value/3 or cast_value/2 instead"
def validate(spec = %OpenApi{}, schema = %Schema{}, params) do
Schema.validate(schema, params, spec.components.schemas)
end
def validate(spec = %OpenApi{}, schema = %Reference{}, params) do
Schema.validate(schema, params, spec.components.schemas)
end
@doc """
Validate all params in `Plug.Conn` against `OpenApiSpex.Operation` `parameter` and `requestBody` schemas.
`content_type` may be optionally supplied to select the `requestBody` schema.
"""
@spec validate(OpenApi.t(), Operation.t(), Plug.Conn.t(), content_type | nil) ::
:ok | {:error, String.t()}
when content_type: String.t()
@deprecated "Use OpenApiSpex.cast_and_validate/4 instead"
def validate(
spec = %OpenApi{},
operation = %Operation{},
conn = %Plug.Conn{},
content_type \\ nil
) do
Operation.validate(operation, conn, content_type, spec.components.schemas)
end
def path_to_string(%Error{} = error) do
Error.path_to_string(error)
end
def error_message(%Error{} = error) do
Error.message(error)
end
@doc """
Declares a struct based `OpenApiSpex.Schema`
- defines the schema/0 callback
- ensures the schema is linked to the module by "x-struct" extension property
- defines a struct with keys matching the schema properties
- defines a @type `t` for the struct
- derives a `Jason.Encoder` and/or `Poison.Encoder` for the struct
See `OpenApiSpex.Schema` for additional examples and details.
## Example
require OpenApiSpex
defmodule User do
OpenApiSpex.schema %{
title: "User",
description: "A user of the app",
type: :object,
properties: %{
id: %Schema{type: :integer, description: "User ID"},
name: %Schema{type: :string, description: "User name", pattern: ~r/[a-zA-Z][a-zA-Z0-9_]+/},
email: %Schema{type: :string, description: "Email address", format: :email},
inserted_at: %Schema{type: :string, description: "Creation timestamp", format: :'date-time'},
updated_at: %Schema{type: :string, description: "Update timestamp", format: :'date-time'}
},
required: [:name, :email],
example: %{
"id" => 123,
"name" => "Joe User",
"email" => "joe@gmail.com",
"inserted_at" => "2017-09-12T12:34:55Z",
"updated_at" => "2017-09-13T10:11:12Z"
}
}
end
"""
defmacro schema(body) do
quote do
@compile {:report_warnings, false}
@behaviour OpenApiSpex.Schema
@schema struct(
OpenApiSpex.Schema,
- Map.put(Map.delete(unquote(body), :__struct__), :"x-struct", __MODULE__)
+ unquote(body)
+ |> Map.delete(:__struct__)
+ |> Map.put(:"x-struct", __MODULE__)
+ |> update_in([:title], fn title ->
+ title || __MODULE__ |> Module.split() |> List.last()
+ end)
)
def schema, do: @schema
@derive Enum.filter([Poison.Encoder, Jason.Encoder], &Code.ensure_loaded?/1)
defstruct Schema.properties(@schema)
@type t :: %__MODULE__{}
Map.from_struct(@schema) |> OpenApiSpex.validate_compiled_schema()
# Throwing warnings to prevent runtime bugs (like in issue #144)
@schema
|> SchemaConsistency.warnings()
|> Enum.each(&IO.warn("Inconsistent schema: #{&1}", Macro.Env.stacktrace(__ENV__)))
end
end
@doc """
Creates an `%OpenApi{}` struct from a map.
This is useful when importing existing JSON or YAML encoded schemas.
## Example
# Importing an existing JSON encoded schema
open_api_spec_from_json = "encoded_schema.json"
|> File.read!()
|> Jason.decode!()
|> OpenApiSpex.OpenApi.Decode.decode()
# Importing an existing YAML encoded schema
open_api_spec_from_yaml = "encoded_schema.yaml"
|> YamlElixir.read_all_from_file!()
|> OpenApiSpex.OpenApi.Decode.decode()
"""
def schema_from_map(map), do: OpenApiSpex.OpenApi.from_map(map)
@doc """
Validate the compiled schema's properties to ensure the schema is not improperly
defined. Only errors which would cause a given schema to _always_ fail should be
raised here.
"""
def validate_compiled_schema(schema) do
Enum.each(schema, fn prop_and_val ->
:ok = validate_compiled_schema(prop_and_val, schema)
end)
end
def validate_compiled_schema({_, %Schema{} = schema}, _parent) do
validate_compiled_schema(Map.from_struct(schema))
end
@doc """
Used for validating the schema at compile time, otherwise we're forced
to raise errors for improperly defined schemas at runtime.
"""
def validate_compiled_schema({:discriminator, %{propertyName: property, mapping: _}}, %{
anyOf: schemas
})
when is_list(schemas) do
Enum.each(schemas, fn schema ->
case schema do
%Schema{title: title} when is_binary(title) -> :ok
_ -> error!(:discriminator_schema_missing_title, schema, property_name: property)
end
end)
end
def validate_compiled_schema({:discriminator, %{propertyName: _, mapping: _}}, schema) do
case {schema.anyOf, schema.allOf, schema.oneOf} do
{nil, nil, nil} ->
error!(:discriminator_missing_composite_key, schema)
_ ->
:ok
end
end
def validate_compiled_schema({_property, _value}, _schema), do: :ok
@doc """
Raises compile time errors for improperly defined schemas.
"""
@spec error!(atom(), Schema.t(), keyword()) :: no_return()
@spec error!(atom(), Schema.t()) :: no_return()
def error!(error, schema, details \\ []) do
raise SchemaException, %{error: error, schema: schema, details: details}
end
@doc """
Resolve a schema or reference to a schema.
"""
@spec resolve_schema(Schema.t() | Reference.t(), Components.schemas_map()) :: Schema.t()
def resolve_schema(%Schema{} = schema, _), do: schema
def resolve_schema(%Reference{} = ref, schemas), do: Reference.resolve_schema(ref, schemas)
end
diff --git a/test/schema_test.exs b/test/schema_test.exs
index 9d63bee..753679e 100644
--- a/test/schema_test.exs
+++ b/test/schema_test.exs
@@ -1,487 +1,514 @@
defmodule OpenApiSpex.SchemaTest do
use ExUnit.Case
alias OpenApiSpex.Schema
alias OpenApiSpexTest.{ApiSpec, Schemas}
import OpenApiSpex.TestAssertions
doctest Schema
describe "schema/1" do
test "EntityWithDict Schema example matches schema" do
api_spec = ApiSpec.spec()
assert_schema(Schemas.EntityWithDict.schema().example, "EntityWithDict", api_spec)
end
test "User Schema example matches schema" do
spec = ApiSpec.spec()
assert_schema(Schemas.User.schema().example, "User", spec)
assert_schema(Schemas.UserRequest.schema().example, "UserRequest", spec)
assert_schema(Schemas.UserResponse.schema().example, "UserResponse", spec)
assert_schema(Schemas.UsersResponse.schema().example, "UsersResponse", spec)
end
test "Array Schema example matches schema" do
api_spec = ApiSpec.spec()
assert_schema(Schemas.Array.schema().example, "Array", api_spec)
end
test "Primitive Schema example matches schema" do
api_spec = ApiSpec.spec()
assert_schema(Schemas.Primitive.schema().example, "Primitive", api_spec)
end
end
+ describe "schema/1 auto-populates title" do
+ defmodule Name do
+ require OpenApiSpex
+
+ OpenApiSpex.schema(%{
+ type: :string
+ })
+ end
+
+ defmodule Age do
+ require OpenApiSpex
+
+ OpenApiSpex.schema(%{
+ title: "CustomAge",
+ type: :integer
+ })
+ end
+
+ test "autopopulated title" do
+ assert Name.schema().title == "Name"
+ end
+
+ test "custome title" do
+ assert Age.schema().title == "CustomAge"
+ end
+ end
+
describe "cast/3" do
test "cast request schema" do
api_spec = ApiSpec.spec()
schemas = api_spec.components.schemas
user_request_schema = schemas["UserRequest"]
input = %{
"user" => %{
"id" => 123,
"name" => "asdf",
"email" => "foo@bar.com",
"updated_at" => "2017-09-12T14:44:55Z"
}
}
{:ok, output} = Schema.cast(user_request_schema, input, schemas)
assert output == %OpenApiSpexTest.Schemas.UserRequest{
user: %OpenApiSpexTest.Schemas.User{
id: 123,
name: "asdf",
email: "foo@bar.com",
updated_at: DateTime.from_naive!(~N[2017-09-12T14:44:55], "Etc/UTC")
}
}
end
test "cast request schema with unexpected fields returns error" do
api_spec = ApiSpec.spec()
schemas = api_spec.components.schemas
user_request_schema = schemas["UserRequest"]
input = %{
"user" => %{
"id" => 123,
"name" => "asdf",
"email" => "foo@bar.com",
"updated_at" => "2017-09-12T14:44:55Z",
"unexpected_field" => "unexpected value"
}
}
assert {:error, _} = Schema.cast(user_request_schema, input, schemas)
end
test "Cast Cat from Pet schema" do
api_spec = ApiSpec.spec()
schemas = api_spec.components.schemas
pet_schema = schemas["Pet"]
input = %{
"pet_type" => "Cat",
"meow" => "meow"
}
assert {:ok, %Schemas.Cat{meow: "meow", pet_type: "Cat"}} =
Schema.cast(pet_schema, input, schemas)
end
test "Cast Dog from oneOf [cat, dog] schema" do
api_spec = ApiSpec.spec()
schemas = api_spec.components.schemas
cat_or_dog = Map.fetch!(schemas, "CatOrDog")
input = %{
"pet_type" => "Dog",
"bark" => "bowow"
}
assert {:ok, %Schemas.Dog{bark: "bowow", pet_type: "Dog"}} =
Schema.cast(cat_or_dog, input, schemas)
end
test "Cast Cat from oneOf [cat, dog] schema" do
api_spec = ApiSpec.spec()
schemas = api_spec.components.schemas
cat_or_dog = Map.fetch!(schemas, "CatOrDog")
input = %{
"pet_type" => "Cat",
"meow" => "meow"
}
assert {:ok, %Schemas.Cat{meow: "meow", pet_type: "Cat"}} =
Schema.cast(cat_or_dog, input, schemas)
end
test "Cast number to string or number" do
schema = %Schema{
oneOf: [
%Schema{type: :number},
%Schema{type: :string}
]
}
# The following object is valid against both schemas, so it will result in an error
# – it should be valid against only one of the schemas, since we are using the oneOf keyword.
# @see https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/#oneof
result = Schema.cast(schema, "123", %{})
assert {:error, _} = result
end
test "Cast integer to float" do
schema = %Schema{type: :number}
assert Schema.cast(schema, 123, %{}) === {:ok, 123.0}
end
test "Cast string to oneOf number or date-time" do
schema = %Schema{
oneOf: [
%Schema{type: :number},
%Schema{type: :string, format: :"date-time"}
]
}
assert {:ok, %DateTime{}} = Schema.cast(schema, "2018-04-01T12:34:56Z", %{})
end
test "Cast string to anyOf number or date-time" do
schema = %Schema{
oneOf: [
%Schema{type: :number},
%Schema{type: :string, format: :"date-time"}
]
}
assert {:ok, %DateTime{}} = Schema.cast(schema, "2018-04-01T12:34:56Z", %{})
end
end
describe "Integer validation" do
test "Validate schema type integer when value is object" do
schema = %Schema{
type: :integer
}
assert {:error, _} = Schema.validate(schema, %{}, %{})
end
end
describe "Number validation" do
test "Validate schema type number when value is object" do
schema = %Schema{
type: :integer
}
assert {:error, _} = Schema.validate(schema, %{}, %{})
end
test "Validate schema type number when value is exclusive" do
schema = %Schema{
type: :integer,
minimum: -1,
maximum: 1,
exclusiveMinimum: true,
exclusiveMaximum: true
}
assert {:error, _} = Schema.validate(schema, 1, %{})
assert {:error, _} = Schema.validate(schema, -1, %{})
end
end
describe "String validation" do
test "Validate schema type string when value is object" do
schema = %Schema{
type: :string
}
assert {:error, _} = Schema.validate(schema, %{}, %{})
end
test "Validate schema type string when value is DateTime" do
schema = %Schema{
type: :string
}
assert {:error, _} = Schema.validate(schema, DateTime.utc_now(), %{})
end
test "Validate non-empty string with expected value" do
schema = %Schema{type: :string, minLength: 1}
assert :ok = Schema.validate(schema, "BLIP", %{})
end
end
describe "DateTime validation" do
test "Validate schema type string with format date-time when value is DateTime" do
schema = %Schema{
type: :string,
format: :"date-time"
}
assert :ok = Schema.validate(schema, DateTime.utc_now(), %{})
end
end
describe "Date Validation" do
test "Validate schema type string with format date when value is Date" do
schema = %Schema{
type: :string,
format: :date
}
assert :ok = Schema.validate(schema, Date.utc_today(), %{})
end
end
describe "Enum validation" do
test "Validate string enum with unexpected value" do
schema = %Schema{
type: :string,
enum: ["foo", "bar"]
}
assert {:error, _} = Schema.validate(schema, "baz", %{})
end
test "Validate string enum with expected value" do
schema = %Schema{
type: :string,
enum: ["foo", "bar"]
}
assert :ok = Schema.validate(schema, "bar", %{})
end
end
describe "Object validation" do
test "Validate schema type object when value is array" do
schema = %Schema{
type: :object
}
assert {:error, _} = Schema.validate(schema, [], %{})
end
test "Validate schema type object when value is DateTime" do
schema = %Schema{
type: :object
}
assert {:error, _} = Schema.validate(schema, DateTime.utc_now(), %{})
end
end
describe "Array validation" do
test "Validate schema type array when value is object" do
schema = %Schema{
type: :array
}
assert {:error, _} = Schema.validate(schema, %{}, %{})
end
end
describe "Boolean validation" do
test "Validate schema type boolean when value is object" do
schema = %Schema{
type: :boolean
}
assert {:error, _} = Schema.validate(schema, %{}, %{})
end
end
describe "AnyOf validation" do
test "Validate anyOf schema with valid value" do
schema = %Schema{
anyOf: [
%Schema{type: :array},
%Schema{type: :string}
]
}
assert :ok = Schema.validate(schema, "a string", %{})
end
test "Validate anyOf with value matching more than one schema" do
schema = %Schema{
anyOf: [
%Schema{type: :number},
%Schema{type: :integer}
]
}
assert :ok = Schema.validate(schema, 42, %{})
end
test "Validate anyOf schema with invalid value" do
schema = %Schema{
anyOf: [
%Schema{type: :string},
%Schema{type: :array}
]
}
assert {:error, _} = Schema.validate(schema, 3.14159, %{})
end
end
describe "OneOf validation" do
test "Validate oneOf schema with valid value" do
schema = %Schema{
oneOf: [
%Schema{type: :string},
%Schema{type: :array}
]
}
assert :ok = Schema.validate(schema, [1, 2, 3], %{})
end
test "Validate oneOf schema with invalid value" do
schema = %Schema{
oneOf: [
%Schema{type: :string},
%Schema{type: :array}
]
}
assert {:error, _} = Schema.validate(schema, 3.14159, %{})
end
test "Validate oneOf schema when matching multiple schemas" do
schema = %Schema{
oneOf: [
%Schema{type: :object, properties: %{a: %Schema{type: :string}}},
%Schema{type: :object, properties: %{b: %Schema{type: :string}}}
]
}
assert {:error, _} = Schema.validate(schema, %{a: "a", b: "b"}, %{})
end
end
describe "AllOf validation" do
test "Validate allOf schema with valid value" do
schema = %Schema{
allOf: [
%Schema{type: :object, properties: %{a: %Schema{type: :string}}},
%Schema{type: :object, properties: %{b: %Schema{type: :string}}}
]
}
assert :ok = Schema.validate(schema, %{a: "a", b: "b"}, %{})
end
test "Validate allOf schema with invalid value" do
schema = %Schema{
allOf: [
%Schema{type: :object, properties: %{a: %Schema{type: :string}}},
%Schema{type: :object, properties: %{b: %Schema{type: :string}}}
]
}
assert {:error, msg} = Schema.validate(schema, %{a: 1, b: 2}, %{})
assert msg =~ "#/a"
assert msg =~ "#/b"
end
test "Validate allOf with value matching not all schemas" do
schema = %Schema{
allOf: [
%Schema{
type: :integer,
minimum: 5
},
%Schema{
type: :integer,
maximum: 40
}
]
}
assert {:error, _} = Schema.validate(schema, 42, %{})
end
end
describe "Not validation" do
test "Validate not schema with valid value" do
schema = %Schema{
not: %Schema{type: :object}
}
assert :ok = Schema.validate(schema, 1, %{})
end
test "Validate not schema with invalid value" do
schema = %Schema{
not: %Schema{type: :object}
}
assert {:error, _} = Schema.validate(schema, %{a: 1}, %{})
end
test "Verify 'not' validation" do
schema = %Schema{not: %Schema{type: :boolean}}
assert :ok = Schema.validate(schema, 42, %{})
assert :ok = Schema.validate(schema, "42", %{})
assert :ok = Schema.validate(schema, nil, %{})
assert :ok = Schema.validate(schema, 4.2, %{})
assert :ok = Schema.validate(schema, [4], %{})
assert :ok = Schema.validate(schema, %{}, %{})
assert {:error, _} = Schema.validate(schema, true, %{})
assert {:error, _} = Schema.validate(schema, false, %{})
end
end
describe "Nullable validation" do
test "Validate nullable-ified with expected value" do
schema = %Schema{
nullable: true,
type: :string,
minLength: 1
}
assert :ok = Schema.validate(schema, "BLIP", %{})
end
test "Validate nullable with expected value" do
schema = %Schema{type: :string, nullable: true}
assert :ok = Schema.validate(schema, nil, %{})
end
test "Validate nullable with unexpected value" do
schema = %Schema{type: :string, nullable: true}
assert :ok = Schema.validate(schema, "bla", %{})
end
end
describe "Default property value" do
test "Available in structure" do
size = %Schemas.Size{}
assert "cm" == size.unit
assert 100 == size.value
end
test "Available after cast" do
api_spec = ApiSpec.spec()
schemas = api_spec.components.schemas
size = Map.fetch!(schemas, "Size")
assert {:ok, %Schemas.Size{value: 100, unit: "cm"}} ==
Schema.cast(size, %{}, schemas)
assert {:ok, %Schemas.Size{value: 110, unit: "cm"}} ==
Schema.cast(size, %{value: 110}, schemas)
end
end
end

File Metadata

Mime Type
text/x-diff
Expires
Tue, Nov 26, 6:27 PM (1 d, 13 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
40456
Default Alt Text
(23 KB)

Event Timeline