Skip to content
This repository was archived by the owner on Nov 7, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
inputs: ["{mix,.formatter}.exs", "{config,lib,rel,test,examples}/**/*.{ex,exs}"],
import_deps: [:plug, :phoenix],
import_deps: [:plug],
locals_without_parens: [
transport: 2,
action_fallback: 1,
Expand Down
2 changes: 1 addition & 1 deletion examples/phoenix_app/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ config :logger, :console,
format: "$time $metadata[$level] $message\n",
metadata: [:request_id]

import_config "#{Mix.env}.exs"
import_config "#{Mix.env()}.exs"
3 changes: 1 addition & 2 deletions examples/phoenix_app/config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ config :phoenix_app, PhoenixAppWeb.Endpoint,

config :logger, level: :warn

config :phoenix_app, PhoenixApp.Repo,
pool: Ecto.Adapters.SQL.Sandbox
config :phoenix_app, PhoenixApp.Repo, pool: Ecto.Adapters.SQL.Sandbox

config :phoenix_app, PhoenixApp.Repo,
adapter: Sqlite.Ecto2,
Expand Down
4 changes: 2 additions & 2 deletions examples/phoenix_app/lib/phoenix_app/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ defmodule PhoenixApp.Accounts.User do
use Ecto.Schema

schema "users" do
field :name, :string
field :email, :string
field(:name, :string)
field(:email, :string)
timestamps()
end
end
3 changes: 2 additions & 1 deletion examples/phoenix_app/lib/phoenix_app/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ defmodule PhoenixApp.Application do
def start(_type, _args) do
children = [
PhoenixAppWeb.Endpoint,
PhoenixApp.Repo,
PhoenixApp.Repo
]

Supervisor.start_link(children, strategy: :one_for_one, name: PhoenixApp.Supervisor)
end

Expand Down
2 changes: 1 addition & 1 deletion examples/phoenix_app/lib/phoenix_app/repo.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
defmodule PhoenixApp.Repo do
use Ecto.Repo, otp_app: :phoenix_app, adapter: Sqlite.Ecto2
end
end
2 changes: 1 addition & 1 deletion examples/phoenix_app/lib/phoenix_app_web/api_spec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ defmodule PhoenixAppWeb.ApiSpec do
}
|> OpenApiSpex.resolve_schema_modules()
end
end
end
26 changes: 15 additions & 11 deletions examples/phoenix_app/lib/phoenix_app_web/schemas.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ defmodule PhoenixAppWeb.Schemas do
alias OpenApiSpex.Schema

defmodule User do
OpenApiSpex.schema %{
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_]+/},
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'}
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: %{
Expand All @@ -22,11 +26,11 @@ defmodule PhoenixAppWeb.Schemas do
"inserted_at" => "2017-09-12T12:34:55Z",
"updated_at" => "2017-09-13T10:11:12Z"
}
}
})
end

defmodule UserRequest do
OpenApiSpex.schema %{
OpenApiSpex.schema(%{
title: "UserRequest",
description: "POST body for creating a user",
type: :object,
Expand All @@ -40,11 +44,11 @@ defmodule PhoenixAppWeb.Schemas do
"email" => "[email protected]"
}
}
}
})
end

defmodule UserResponse do
OpenApiSpex.schema %{
OpenApiSpex.schema(%{
title: "UserResponse",
description: "Response schema for single user",
type: :object,
Expand All @@ -61,11 +65,11 @@ defmodule PhoenixAppWeb.Schemas do
}
},
"x-struct": __MODULE__
}
})
end

defmodule UsersResponse do
OpenApiSpex.schema %{
OpenApiSpex.schema(%{
title: "UsersResponse",
description: "Response schema for multiple users",
type: :object,
Expand All @@ -86,6 +90,6 @@ defmodule PhoenixAppWeb.Schemas do
}
]
}
}
})
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule PhoenixApp.Repo.Migrations.AddUsers do

def change do
create table(:users) do
add :name, :string, null: false
add :email, :string, null: false
add(:name, :string, null: false)
add(:email, :string, null: false)
timestamps()
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ defmodule PhoenixAppWeb.UserControllerTest do
test "create user", %{conn: conn, spec: spec} do
conn
|> Plug.Conn.put_req_header("content-type", "application/json")
|> post(user_path(conn, :create, 1), %{"user" => %{"name" => "Joe", "email" => "[email protected]"}})
|> post(user_path(conn, :create, 1), %{
"user" => %{"name" => "Joe", "email" => "[email protected]"}
})
|> json_response(201)
|> assert_schema("UserResponse", spec)
end

test "get user", %{conn: conn, spec: spec} do
%{id: id} = PhoenixApp.Repo.insert!(%PhoenixApp.Accounts.User{name: "Carl", email: "[email protected]"})
%{id: id} =
PhoenixApp.Repo.insert!(%PhoenixApp.Accounts.User{name: "Carl", email: "[email protected]"})

conn
|> Plug.Conn.put_req_header("accept", "application/json")
Expand All @@ -25,9 +28,14 @@ defmodule PhoenixAppWeb.UserControllerTest do
end

test "list users", %{conn: conn, spec: spec} do
%{id: id1} = PhoenixApp.Repo.insert!(%PhoenixApp.Accounts.User{name: "Aaron", email: "[email protected]"})
%{id: id2} = PhoenixApp.Repo.insert!(%PhoenixApp.Accounts.User{name: "Benjamin", email: "[email protected]"})
%{id: id3} = PhoenixApp.Repo.insert!(%PhoenixApp.Accounts.User{name: "Chuck", email: "[email protected]"})
%{id: id1} =
PhoenixApp.Repo.insert!(%PhoenixApp.Accounts.User{name: "Aaron", email: "[email protected]"})

%{id: id2} =
PhoenixApp.Repo.insert!(%PhoenixApp.Accounts.User{name: "Benjamin", email: "[email protected]"})

%{id: id3} =
PhoenixApp.Repo.insert!(%PhoenixApp.Accounts.User{name: "Chuck", email: "[email protected]"})

response =
conn
Expand Down
2 changes: 2 additions & 0 deletions examples/phoenix_app/test/support/conn_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ defmodule PhoenixAppWeb.ConnCase do

setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(PhoenixApp.Repo)

unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(PhoenixApp.Repo, {:shared, self()})
end

{:ok, conn: Phoenix.ConnTest.build_conn()}
end
end
1 change: 0 additions & 1 deletion examples/plug_app/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ config :plug_app, PlugApp.Repo,
database: "priv/repo/plug_app_#{Mix.env()}.db"

config :logger, level: :debug

4 changes: 2 additions & 2 deletions examples/plug_app/lib/plug_app/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ defmodule PlugApp.Accounts.User do
use Ecto.Schema

schema "users" do
field :name, :string
field :email, :string
field(:name, :string)
field(:email, :string)
timestamps()
end
end
18 changes: 10 additions & 8 deletions examples/plug_app/lib/plug_app/api_spec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ defmodule PlugApp.ApiSpec do
version: "1.0"
},
paths: %{
"/api/users/{id}" => OpenApiSpex.PathItem.from_routes([
%{verb: :get, plug: PlugApp.UserHandler.Show, opts: []}
]),
"/api/users" => OpenApiSpex.PathItem.from_routes([
%{verb: :get, plug: PlugApp.UserHandler.Index, opts: []},
%{verb: :post, plug: PlugApp.UserHandler.Create, opts: []}
])
"/api/users/{id}" =>
OpenApiSpex.PathItem.from_routes([
%{verb: :get, plug: PlugApp.UserHandler.Show, opts: []}
]),
"/api/users" =>
OpenApiSpex.PathItem.from_routes([
%{verb: :get, plug: PlugApp.UserHandler.Index, opts: []},
%{verb: :post, plug: PlugApp.UserHandler.Create, opts: []}
])
}
}
|> OpenApiSpex.resolve_schema_modules()
end
end
end
2 changes: 1 addition & 1 deletion examples/plug_app/lib/plug_app/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule PlugApp.Application do
# List all child processes to be supervised
children = [
PlugApp.Repo,
Plug.Adapters.Cowboy.child_spec(:http, PlugApp.Router, [], [port: 4000])
Plug.Adapters.Cowboy.child_spec(:http, PlugApp.Router, [], port: 4000)
]

# See https://hexdocs.pm/elixir/Supervisor.html
Expand Down
2 changes: 1 addition & 1 deletion examples/plug_app/lib/plug_app/repo.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
defmodule PlugApp.Repo do
use Ecto.Repo, otp_app: :plug_app, adapter: Sqlite.Ecto2
end
end
1 change: 0 additions & 1 deletion examples/plug_app/lib/plug_app/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ defmodule PlugApp.Router do
match "/api/*_", to: PlugApp.Router.Api
match "/*_", to: PlugApp.Router.Html
end

28 changes: 16 additions & 12 deletions examples/plug_app/lib/plug_app/schemas.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ defmodule PlugApp.Schemas do
alias OpenApiSpex.Schema

defmodule User do
OpenApiSpex.schema %{
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_]+/},
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'}
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: %{
Expand All @@ -22,11 +26,11 @@ defmodule PlugApp.Schemas do
"inserted_at" => "2017-09-12T12:34:55Z",
"updated_at" => "2017-09-13T10:11:12Z"
}
}
})
end

defmodule UserRequest do
OpenApiSpex.schema %{
OpenApiSpex.schema(%{
title: "UserRequest",
description: "POST body for creating a user",
type: :object,
Expand All @@ -40,11 +44,11 @@ defmodule PlugApp.Schemas do
"email" => "[email protected]"
}
}
}
})
end

defmodule UserResponse do
OpenApiSpex.schema %{
OpenApiSpex.schema(%{
title: "UserResponse",
description: "Response schema for single user",
type: :object,
Expand All @@ -61,11 +65,11 @@ defmodule PlugApp.Schemas do
}
},
"x-struct": __MODULE__
}
})
end

defmodule UsersResponse do
OpenApiSpex.schema %{
OpenApiSpex.schema(%{
title: "UsersResponse",
description: "Response schema for multiple users",
type: :object,
Expand All @@ -86,6 +90,6 @@ defmodule PlugApp.Schemas do
}
]
}
}
})
end
end
end
9 changes: 7 additions & 2 deletions examples/plug_app/lib/plug_app/user_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ defmodule PlugApp.UserHandler do

def index(conn, _opts) do
users = Accounts.list_users()

conn
|> Plug.Conn.put_resp_header("Content-Type", "application/json")
|> Plug.Conn.send_resp(200, render(users))
Expand Down Expand Up @@ -69,7 +70,8 @@ defmodule PlugApp.UserHandler do
|> send_resp(404, ~s({"error": "User not found"}))
|> halt()

user -> assign(conn, :user, user)
user ->
assign(conn, :user, user)
end
end

Expand Down Expand Up @@ -100,7 +102,10 @@ defmodule PlugApp.UserHandler do
summary: "Create user",
description: "Create a user",
operationId: "UserHandler.Create",
requestBody: request_body("The user attributes", "application/json", Schemas.UserRequest, required: true),
requestBody:
request_body("The user attributes", "application/json", Schemas.UserRequest,
required: true
),
responses: %{
201 => response("User", "application/json", Schemas.UserResponse)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule PlugApp.Repo.Migrations.AddUsers do

def change do
create table(:users) do
add :name, :string, null: false
add :email, :string, null: false
add(:name, :string, null: false)
add(:email, :string, null: false)
timestamps()
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/open_api_spex/callback.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ defmodule OpenApiSpex.Callback do
that identifies a URL to use for the callback operation.
"""
@type t :: %{
String.t => PathItem.t
}
end
String.t() => PathItem.t()
}
end
Loading