Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F111953
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
7 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/open_api_spex/operation.ex b/lib/open_api_spex/operation.ex
index b10357c..b81bed6 100644
--- a/lib/open_api_spex/operation.ex
+++ b/lib/open_api_spex/operation.ex
@@ -1,102 +1,111 @@
defmodule OpenApiSpex.Operation do
alias OpenApiSpex.{
- ExternalDocumentation, Parameter, Reference,
- RequestBody, Responses, Callback,
- SecurityRequirement, Server, MediaType, Response
+ Callback,
+ ExternalDocumentation,
+ MediaType,
+ Parameter,
+ Reference,
+ RequestBody,
+ Response,
+ Responses,
+ Schema,
+ SecurityRequirement,
+ Server,
}
defstruct [
:tags,
:summary,
:description,
:externalDocs,
:operationId,
:parameters,
:requestBody,
:responses,
:callbacks,
:deprecated,
:security,
:servers
]
@type t :: %__MODULE__{
tags: [String.t],
summary: String.t,
description: String.t,
externalDocs: ExternalDocumentation.t,
operationId: String.t,
parameters: [Parameter.t | Reference.t],
requestBody: [RequestBody.t | Reference.t],
responses: Responses.t,
callbacks: %{
String.t => Callback.t | Reference.t
},
deprecated: boolean,
security: [SecurityRequirement.t],
servers: [Server.t]
}
@doc """
Constructs an Operation struct from the plug and opts specified in the given route
"""
@spec from_route(PathItem.route) :: t
def from_route(route) do
from_plug(route.plug, route.opts)
end
@doc """
Constructs an Operation struct from plug module and opts
"""
@spec from_plug(module, any) :: t
def from_plug(plug, opts) do
plug.open_api_operation(opts)
end
@doc """
Shorthand for constructing a Parameter name, location, type, description and optional examples
"""
- @spec parameter(String.t, String.t, String.t, keyword) :: RequestBody.t
+ @spec parameter(atom, atom, atom, String.t, keyword) :: RequestBody.t
def parameter(name, location, type, description, opts \\ []) do
params =
[name: name, in: location, description: description, required: location == :path]
|> Keyword.merge(opts)
Parameter
|> struct(params)
|> Parameter.put_schema(type)
end
@doc """
Shorthand for constructing a RequestBody with description, media_type, schema and optional examples
"""
- @spec request_body(String.t, String.t, String.t, keyword) :: RequestBody.t
+ @spec request_body(String.t, String.t, (Schema.t | Reference.t | module), keyword) :: RequestBody.t
def request_body(description, media_type, schema_ref, opts \\ []) do
%RequestBody{
description: description,
content: %{
media_type => %MediaType{
schema: schema_ref,
example: opts[:example],
examples: opts[:examples]
}
- }
+ },
+ required: opts[:required] || false
}
end
@doc """
Shorthand for constructing a Response with description, media_type, schema and optional examples
"""
- @spec response(String.t, String.t, String.t, keyword) :: Response.t
+ @spec response(String.t, String.t, (Schema.t | Reference.t | module), keyword) :: Response.t
def response(description, media_type, schema_ref, opts \\ []) do
%Response{
description: description,
content: %{
media_type => %MediaType {
schema: schema_ref,
example: opts[:example],
examples: opts[:examples]
}
}
}
end
end
\ No newline at end of file
diff --git a/lib/open_api_spex/parameter.ex b/lib/open_api_spex/parameter.ex
index 0cce4ce..821f8ea 100644
--- a/lib/open_api_spex/parameter.ex
+++ b/lib/open_api_spex/parameter.ex
@@ -1,58 +1,64 @@
defmodule OpenApiSpex.Parameter do
alias OpenApiSpex.{
Schema, Reference, Example, MediaType, Parameter
}
defstruct [
:name,
:in,
:description,
:required,
:deprecated,
:allowEmptyValue,
:style,
:explode,
:allowReserved,
:schema,
:example,
:examples,
:content,
]
@type style :: :matrix | :label | :form | :simple | :spaceDelimited | :pipeDelimited | :deep
@type t :: %__MODULE__{
name: String.t,
in: :query | :header | :path | :cookie,
description: String.t,
required: boolean,
deprecated: boolean,
allowEmptyValue: boolean,
style: style,
explode: boolean,
allowReserved: boolean,
schema: Schema.t | Reference.t,
example: any,
examples: %{String.t => Example.t | Reference.t},
content: %{String.t => MediaType.t}
}
@doc """
Sets the schema for a parameter from a simple type, reference or Schema
"""
@spec put_schema(t, Reference.t | Schema.t | atom | String.t) :: t
def put_schema(parameter = %Parameter{}, type = %Reference{}) do
%{parameter | schema: type}
end
def put_schema(parameter = %Parameter{}, type = %Schema{}) do
%{parameter | schema: type}
end
- def put_schema(parameter = %Parameter{}, type) when is_binary(type) or is_atom(type) do
+ def put_schema(parameter = %Parameter{}, type) when is_binary(type) do
%{parameter | schema: %Schema{type: type}}
end
+ def put_schema(parameter = %Parameter{}, type) when type in [:boolean, :integer, :number, :string, :array, :object] do
+ %{parameter | schema: %Schema{type: type}}
+ end
+ def put_schema(parameter = %Parameter{}, type) when is_atom(type) do
+ %{parameter | schema: type}
+ end
def schema(%Parameter{schema: schema = %{}}) do
schema
end
def schema(%Parameter{content: content = %{}}) do
{_type, %MediaType{schema: schema}} = Enum.at(content, 0)
schema
end
end
diff --git a/test/support/user_controller.ex b/test/support/user_controller.ex
index 175771e..b0f3e63 100644
--- a/test/support/user_controller.ex
+++ b/test/support/user_controller.ex
@@ -1,70 +1,70 @@
defmodule OpenApiSpexTest.UserController do
use Phoenix.Controller
alias OpenApiSpex.Operation
alias OpenApiSpexTest.Schemas
alias Plug.Conn
plug OpenApiSpex.Plug.Cast
plug OpenApiSpex.Plug.Validate
def open_api_operation(action) do
apply(__MODULE__, :"#{action}_operation", [])
end
def show_operation() do
import Operation
%Operation{
tags: ["users"],
summary: "Show user",
description: "Show a user by ID",
operationId: "UserController.show",
parameters: [
- parameter(:id, :path, :integer, "User ID", example: 123)
+ parameter(:id, :path, :integer, "User ID", example: 123, minimum: 1)
],
responses: %{
200 => response("User", "application/json", Schemas.UserResponse)
}
}
end
def show(conn, _params) do
conn
|> Conn.send_resp(200, "HELLO")
end
def index_operation() do
import Operation
%Operation{
tags: ["users"],
summary: "List users",
description: "List all useres",
operationId: "UserController.index",
parameters: [],
responses: %{
200 => response("User List Response", "application/json", Schemas.UsersResponse)
}
}
end
def index(conn, _params) do
conn
|> Conn.send_resp(200, "HELLO")
end
def create_operation() do
import Operation
%Operation{
tags: ["users"],
summary: "Create user",
description: "Create a user",
operationId: "UserController.create",
parameters: [],
requestBody: request_body("The user attributes", "application/json", Schemas.UserRequest),
responses: %{
201 => response("User", "application/json", Schemas.UserResponse)
}
}
end
def create(conn, _params) do
conn
|> Conn.send_resp(201, "DONE")
end
end
\ No newline at end of file
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Fri, Nov 22, 10:31 AM (13 h, 58 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
38683
Default Alt Text
(7 KB)
Attached To
Mode
R22 open_api_spex
Attached
Detach File
Event Timeline
Log In to Comment