Page MenuHomePhorge

No OneTemporary

Size
23 KB
Referenced Files
None
Subscribers
None
diff --git a/examples/phoenix_app/.gitignore b/examples/phoenix_app/.gitignore
new file mode 100644
index 0000000..d5be28f
--- /dev/null
+++ b/examples/phoenix_app/.gitignore
@@ -0,0 +1,27 @@
+# App artifacts
+/_build
+/db
+/deps
+/*.ez
+
+# Generated on crash by the VM
+erl_crash.dump
+
+# Generated on crash by NPM
+npm-debug.log
+
+# Static artifacts
+/assets/node_modules
+
+# Since we are building assets from assets/,
+# we ignore priv/static. You may want to comment
+# this depending on your deployment strategy.
+/priv/static/
+
+# Files matching config/*.secret.exs pattern contain sensitive
+# data and you should not commit them into version control.
+#
+# Alternatively, you may comment the line below and commit the
+# secrets files as long as you replace their contents by environment
+# variables.
+/config/*.secret.exs
\ No newline at end of file
diff --git a/examples/phoenix_app/README.md b/examples/phoenix_app/README.md
new file mode 100644
index 0000000..b351da7
--- /dev/null
+++ b/examples/phoenix_app/README.md
@@ -0,0 +1,7 @@
+# PhoenixApp
+
+This simple Phoenix API Application demonstrates the usage of `open_api_spex`.
+
+ - [api_spec.ex](lib/phoenix_app_web/api_spec.ex) contains the outline of the api spec
+ - [schemas.ex](lib/phoenix_app_web/schemas.ex) conains the request/response schema modules
+ - [router.ex][lib/phoenix_app_web/router.ex]
\ No newline at end of file
diff --git a/examples/phoenix_app/config/config.exs b/examples/phoenix_app/config/config.exs
new file mode 100644
index 0000000..14ac247
--- /dev/null
+++ b/examples/phoenix_app/config/config.exs
@@ -0,0 +1,17 @@
+use Mix.Config
+
+config :phoenix_app, PhoenixAppWeb.Endpoint,
+ url: [host: "localhost"],
+ render_errors: [view: PhoenixAppWeb.ErrorView, accepts: ~w(json)]
+
+config :phoenix_app, ecto_repos: [PhoenixApp.Repo]
+
+config :phoenix_app, PhoenixApp.Repo,
+ adapter: Sqlite.Ecto2,
+ database: "priv/repo/phoenix_app.db"
+
+config :logger, :console,
+ format: "$time $metadata[$level] $message\n",
+ metadata: [:request_id]
+
+import_config "#{Mix.env}.exs"
diff --git a/examples/phoenix_app/config/dev.exs b/examples/phoenix_app/config/dev.exs
new file mode 100644
index 0000000..2d01f9b
--- /dev/null
+++ b/examples/phoenix_app/config/dev.exs
@@ -0,0 +1,10 @@
+use Mix.Config
+
+config :phoenix_app, PhoenixAppWeb.Endpoint,
+ http: [port: 4000],
+ debug_errors: true,
+ code_reloader: true,
+ check_origin: false
+
+config :logger, :console, format: "[$level] $message\n"
+config :phoenix, :stacktrace_depth, 20
diff --git a/examples/phoenix_app/config/test.exs b/examples/phoenix_app/config/test.exs
new file mode 100644
index 0000000..7ded3bd
--- /dev/null
+++ b/examples/phoenix_app/config/test.exs
@@ -0,0 +1,11 @@
+use Mix.Config
+
+config :phoenix_app, PhoenixAppWeb.Endpoint,
+ http: [port: 4001],
+ server: false
+
+config :logger, level: :warn
+
+config :phoenix_app, PhoenixApp.Repo,
+ pool: Ecto.Adapters.SQL.Sandbox
+
diff --git a/examples/phoenix_app/lib/phoenix_app/accounts/accounts.ex b/examples/phoenix_app/lib/phoenix_app/accounts/accounts.ex
new file mode 100644
index 0000000..f86708d
--- /dev/null
+++ b/examples/phoenix_app/lib/phoenix_app/accounts/accounts.ex
@@ -0,0 +1,17 @@
+defmodule PhoenixApp.Accounts do
+ alias PhoenixApp.Accounts.User
+ alias PhoenixApp.Repo
+
+ def list_users() do
+ Repo.all(User)
+ end
+
+ def get_user!(id) do
+ Repo.get(User, id)
+ end
+
+ def create_user(%{name: name, email: email}) do
+ user = Repo.insert!(%User{name: name, email: email})
+ {:ok, user}
+ end
+end
diff --git a/examples/phoenix_app/lib/phoenix_app/accounts/user.ex b/examples/phoenix_app/lib/phoenix_app/accounts/user.ex
new file mode 100644
index 0000000..b87d41a
--- /dev/null
+++ b/examples/phoenix_app/lib/phoenix_app/accounts/user.ex
@@ -0,0 +1,9 @@
+defmodule PhoenixApp.Accounts.User do
+ use Ecto.Schema
+
+ schema "users" do
+ field :name, :string
+ field :email, :string
+ timestamps()
+ end
+end
diff --git a/examples/phoenix_app/lib/phoenix_app/application.ex b/examples/phoenix_app/lib/phoenix_app/application.ex
new file mode 100644
index 0000000..aedd7c4
--- /dev/null
+++ b/examples/phoenix_app/lib/phoenix_app/application.ex
@@ -0,0 +1,16 @@
+defmodule PhoenixApp.Application do
+ use Application
+
+ def start(_type, _args) do
+ children = [
+ PhoenixAppWeb.Endpoint,
+ PhoenixApp.Repo,
+ ]
+ Supervisor.start_link(children, strategy: :one_for_one, name: PhoenixApp.Supervisor)
+ end
+
+ def config_change(changed, _new, removed) do
+ PhoenixAppWeb.Endpoint.config_change(changed, removed)
+ :ok
+ end
+end
diff --git a/examples/phoenix_app/lib/phoenix_app/repo.ex b/examples/phoenix_app/lib/phoenix_app/repo.ex
new file mode 100644
index 0000000..e5f50ce
--- /dev/null
+++ b/examples/phoenix_app/lib/phoenix_app/repo.ex
@@ -0,0 +1,3 @@
+defmodule PhoenixApp.Repo do
+ use Ecto.Repo, otp_app: :phoenix_app, adapter: Sqlite.Ecto2
+end
\ No newline at end of file
diff --git a/examples/phoenix_app/lib/phoenix_app_web.ex b/examples/phoenix_app/lib/phoenix_app_web.ex
new file mode 100644
index 0000000..1b19ea6
--- /dev/null
+++ b/examples/phoenix_app/lib/phoenix_app_web.ex
@@ -0,0 +1,19 @@
+defmodule PhoenixAppWeb do
+ def controller do
+ quote do
+ use Phoenix.Controller, namespace: PhoenixAppWeb
+ import PhoenixAppWeb.Router.Helpers
+ end
+ end
+
+ def view do
+ quote do
+ use Phoenix.View, root: "lib/phoenix_app_web/templates", namespace: PhoenixAppWeb
+ import PhoenixAppWeb.Router.Helpers
+ end
+ end
+
+ defmacro __using__(which) when is_atom(which) do
+ apply(__MODULE__, which, [])
+ end
+end
diff --git a/examples/phoenix_app/lib/phoenix_app_web/api_spec.ex b/examples/phoenix_app/lib/phoenix_app_web/api_spec.ex
new file mode 100644
index 0000000..4ed0090
--- /dev/null
+++ b/examples/phoenix_app/lib/phoenix_app_web/api_spec.ex
@@ -0,0 +1,14 @@
+defmodule PhoenixAppWeb.ApiSpec do
+ alias OpenApiSpex.{Info, OpenApi, Paths}
+
+ def spec do
+ %OpenApi{
+ info: %Info{
+ title: "Phoenix App",
+ version: "1.0"
+ },
+ paths: Paths.from_router(PhoenixAppWeb.Router)
+ }
+ |> OpenApiSpex.resolve_schema_modules()
+ end
+end
\ No newline at end of file
diff --git a/examples/phoenix_app/lib/phoenix_app_web/controllers/user_controller.ex b/examples/phoenix_app/lib/phoenix_app_web/controllers/user_controller.ex
new file mode 100644
index 0000000..e9e4df4
--- /dev/null
+++ b/examples/phoenix_app/lib/phoenix_app_web/controllers/user_controller.ex
@@ -0,0 +1,73 @@
+defmodule PhoenixAppWeb.UserController do
+ use PhoenixAppWeb, :controller
+ import OpenApiSpex.Operation, only: [parameter: 5, request_body: 4, response: 3]
+ alias OpenApiSpex.Operation
+ alias PhoenixApp.{Accounts, Accounts.User}
+ alias PhoenixAppWeb.Schemas
+
+ plug OpenApiSpex.Plug.Cast
+ plug OpenApiSpex.Plug.Validate
+
+ def open_api_operation(action) do
+ apply(__MODULE__, :"#{action}_operation", [])
+ end
+
+ def index_operation() do
+ %Operation{
+ tags: ["users"],
+ summary: "List users",
+ description: "List all useres",
+ operationId: "UserController.index",
+ responses: %{
+ 200 => response("User List Response", "application/json", Schemas.UsersResponse)
+ }
+ }
+ end
+ def index(conn, _params) do
+ users = Accounts.list_users()
+ render(conn, "index.json", users: users)
+ end
+
+ def create_operation() do
+ %Operation{
+ tags: ["users"],
+ summary: "Create user",
+ description: "Create a user",
+ operationId: "UserController.create",
+ requestBody: request_body("The user attributes", "application/json", Schemas.UserRequest, required: true),
+ responses: %{
+ 201 => response("User", "application/json", Schemas.UserResponse)
+ }
+ }
+ end
+ def create(conn, %Schemas.UserRequest{user: user_params}) do
+ with {:ok, %User{} = user} <- Accounts.create_user(user_params) do
+ conn
+ |> put_status(:created)
+ |> put_resp_header("location", user_path(conn, :show, user))
+ |> render("show.json", user: user)
+ end
+ end
+
+ @doc """
+ API Spec for :show action
+ """
+ def show_operation() do
+ %Operation{
+ tags: ["users"],
+ summary: "Show user",
+ description: "Show a user by ID",
+ operationId: "UserController.show",
+ parameters: [
+ parameter(:id, :path, :integer, "User ID", example: 123, minimum: 1)
+ ],
+ responses: %{
+ 200 => response("User", "application/json", Schemas.UserResponse)
+ }
+ }
+ end
+ def show(conn, %{id: id}) do
+ user = Accounts.get_user!(id)
+ render(conn, "show.json", user: user)
+ end
+end
diff --git a/examples/phoenix_app/lib/phoenix_app_web/endpoint.ex b/examples/phoenix_app/lib/phoenix_app_web/endpoint.ex
new file mode 100644
index 0000000..9848732
--- /dev/null
+++ b/examples/phoenix_app/lib/phoenix_app_web/endpoint.ex
@@ -0,0 +1,8 @@
+defmodule PhoenixAppWeb.Endpoint do
+ use Phoenix.Endpoint, otp_app: :phoenix_app
+
+ plug Plug.RequestId
+ plug Plug.Logger
+ plug Plug.Parsers, parsers: [:json], pass: ["*/*"], json_decoder: Poison
+ plug PhoenixAppWeb.Router
+end
diff --git a/examples/phoenix_app/lib/phoenix_app_web/router.ex b/examples/phoenix_app/lib/phoenix_app_web/router.ex
new file mode 100644
index 0000000..04bb39c
--- /dev/null
+++ b/examples/phoenix_app/lib/phoenix_app_web/router.ex
@@ -0,0 +1,23 @@
+defmodule PhoenixAppWeb.Router do
+ use Phoenix.Router
+
+ pipeline :browser do
+ plug :accepts, ["html"]
+ end
+
+ pipeline :api do
+ plug :accepts, ["json"]
+ plug OpenApiSpex.Plug.PutApiSpec, module: PhoenixAppWeb.ApiSpec
+ end
+
+ scope "/" do
+ pipe_through :browser
+ get "/swaggerui", OpenApiSpex.Plug.SwaggerUI, path: "/api/openapi"
+ end
+
+ scope "/api" do
+ pipe_through :api
+ resources "/users", PhoenixAppWeb.UserController, only: [:index, :create, :show]
+ get "/openapi", OpenApiSpex.Plug.RenderSpec, :show
+ end
+end
diff --git a/examples/phoenix_app/lib/phoenix_app_web/schemas.ex b/examples/phoenix_app/lib/phoenix_app_web/schemas.ex
new file mode 100644
index 0000000..d622530
--- /dev/null
+++ b/examples/phoenix_app/lib/phoenix_app_web/schemas.ex
@@ -0,0 +1,91 @@
+defmodule PhoenixAppWeb.Schemas do
+ require OpenApiSpex
+ alias OpenApiSpex.Schema
+
+ 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
+
+ defmodule UserRequest do
+ OpenApiSpex.schema %{
+ title: "UserRequest",
+ description: "POST body for creating a user",
+ type: :object,
+ properties: %{
+ user: User
+ },
+ required: [:user],
+ example: %{
+ "user" => %{
+ "name" => "Joe User",
+ "email" => "joe@gmail.com"
+ }
+ }
+ }
+ end
+
+ defmodule UserResponse do
+ OpenApiSpex.schema %{
+ title: "UserResponse",
+ description: "Response schema for single user",
+ type: :object,
+ properties: %{
+ data: User
+ },
+ example: %{
+ "data" => %{
+ "id" => 123,
+ "name" => "Joe User",
+ "email" => "joe@gmail.com",
+ "inserted_at" => "2017-09-12T12:34:55Z",
+ "updated_at" => "2017-09-13T10:11:12Z"
+ }
+ },
+ "x-struct": __MODULE__
+ }
+ end
+
+ defmodule UsersResponse do
+ OpenApiSpex.schema %{
+ title: "UsersResponse",
+ description: "Response schema for multiple users",
+ type: :object,
+ properties: %{
+ data: %Schema{description: "The users details", type: :array, items: User}
+ },
+ example: %{
+ "data" => [
+ %{
+ "id" => 123,
+ "name" => "Joe User",
+ "email" => "joe@gmail.com"
+ },
+ %{
+ "id" => 456,
+ "name" => "Jay Consumer",
+ "email" => "jay@yahoo.com"
+ }
+ ]
+ }
+ }
+ end
+end
\ No newline at end of file
diff --git a/examples/phoenix_app/lib/phoenix_app_web/views/error_view.ex b/examples/phoenix_app/lib/phoenix_app_web/views/error_view.ex
new file mode 100644
index 0000000..667b7b6
--- /dev/null
+++ b/examples/phoenix_app/lib/phoenix_app_web/views/error_view.ex
@@ -0,0 +1,17 @@
+defmodule PhoenixAppWeb.ErrorView do
+ use PhoenixAppWeb, :view
+
+ def render("404.html", _assigns) do
+ "Page not found"
+ end
+
+ def render("500.html", _assigns) do
+ "Internal server error"
+ end
+
+ # In case no render clause matches or no
+ # template is found, let's render it as 500
+ def template_not_found(_template, assigns) do
+ render "500.html", assigns
+ end
+end
diff --git a/examples/phoenix_app/lib/phoenix_app_web/views/user_view.ex b/examples/phoenix_app/lib/phoenix_app_web/views/user_view.ex
new file mode 100644
index 0000000..aee0bd0
--- /dev/null
+++ b/examples/phoenix_app/lib/phoenix_app_web/views/user_view.ex
@@ -0,0 +1,24 @@
+defmodule PhoenixAppWeb.UserView do
+ use PhoenixAppWeb, :view
+ alias PhoenixAppWeb.UserView
+
+ def render("index.json", %{users: users}) do
+ %{
+ data: render_many(users, UserView, "user.json")
+ }
+ end
+
+ def render("show.json", %{user: user}) do
+ %{
+ data: render_one(user, UserView, "user.json")
+ }
+ end
+
+ def render("user.json", %{user: user}) do
+ %{
+ id: user.id,
+ name: user.name,
+ email: user.email
+ }
+ end
+end
diff --git a/examples/phoenix_app/mix.exs b/examples/phoenix_app/mix.exs
new file mode 100644
index 0000000..0018a20
--- /dev/null
+++ b/examples/phoenix_app/mix.exs
@@ -0,0 +1,47 @@
+defmodule PhoenixApp.Mixfile do
+ use Mix.Project
+
+ def project do
+ [
+ app: :phoenix_app,
+ version: "0.0.1",
+ elixir: "~> 1.5",
+ elixirc_paths: elixirc_paths(Mix.env),
+ compilers: [:phoenix] ++ Mix.compilers,
+ start_permanent: Mix.env == :prod,
+ aliases: aliases(),
+ deps: deps()
+ ]
+ end
+
+ # Configuration for the OTP application.
+ #
+ # Type `mix help compile.app` for more information.
+ def application do
+ [
+ mod: {PhoenixApp.Application, []},
+ extra_applications: [:logger, :runtime_tools]
+ ]
+ end
+
+ # Specifies which paths to compile per environment.
+ defp elixirc_paths(:test), do: ["lib", "test/support"]
+ defp elixirc_paths(_), do: ["lib"]
+
+ defp aliases() do
+ [test: ["ecto.create --quiet", "ecto.migrate", "test"]]
+ end
+
+ # Specifies your project dependencies.
+ #
+ # Type `mix help deps` for examples and options.
+ defp deps do
+ [
+ {:open_api_spex, path: "../../"},
+ {:ecto, "~> 2.2"},
+ {:sqlite_ecto2, "~> 2.2"},
+ {:phoenix, "~> 1.3.0"},
+ {:cowboy, "~> 1.0"}
+ ]
+ end
+end
diff --git a/examples/phoenix_app/mix.lock b/examples/phoenix_app/mix.lock
new file mode 100644
index 0000000..6e28540
--- /dev/null
+++ b/examples/phoenix_app/mix.lock
@@ -0,0 +1,17 @@
+%{"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"},
+ "cowboy": {:hex, :cowboy, "1.1.2", "61ac29ea970389a88eca5a65601460162d370a70018afe6f949a29dca91f3bb0", [:rebar3], [{:cowlib, "~> 1.0.2", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3.2", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
+ "cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], [], "hexpm"},
+ "db_connection": {:hex, :db_connection, "1.1.2", "2865c2a4bae0714e2213a0ce60a1b12d76a6efba0c51fbda59c9ab8d1accc7a8", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
+ "decimal": {:hex, :decimal, "1.4.1", "ad9e501edf7322f122f7fc151cce7c2a0c9ada96f2b0155b8a09a795c2029770", [:mix], [], "hexpm"},
+ "ecto": {:hex, :ecto, "2.2.6", "3fd1067661d6d64851a0d4db9acd9e884c00d2d1aa41cc09da687226cf894661", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
+ "esqlite": {:hex, :esqlite, "0.2.3", "1a8b60877fdd3d50a8a84b342db04032c0231cc27ecff4ddd0d934485d4c0cd5", [:rebar3], [], "hexpm"},
+ "mime": {:hex, :mime, "1.1.0", "01c1d6f4083d8aa5c7b8c246ade95139620ef8effb009edde934e0ec3b28090a", [:mix], [], "hexpm"},
+ "phoenix": {:hex, :phoenix, "1.3.0", "1c01124caa1b4a7af46f2050ff11b267baa3edb441b45dbf243e979cd4c5891b", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: false]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
+ "phoenix_pubsub": {:hex, :phoenix_pubsub, "1.0.2", "bfa7fd52788b5eaa09cb51ff9fcad1d9edfeb68251add458523f839392f034c1", [:mix], [], "hexpm"},
+ "plug": {:hex, :plug, "1.4.3", "236d77ce7bf3e3a2668dc0d32a9b6f1f9b1f05361019946aae49874904be4aed", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1", [hex: :cowboy, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm"},
+ "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
+ "poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], [], "hexpm"},
+ "ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], [], "hexpm"},
+ "sbroker": {:hex, :sbroker, "1.0.0", "28ff1b5e58887c5098539f236307b36fe1d3edaa2acff9d6a3d17c2dcafebbd0", [:rebar3], [], "hexpm"},
+ "sqlite_ecto2": {:hex, :sqlite_ecto2, "2.2.2", "7a3e5c0521e1cb6e30a4907ba4d952b97db9b2ab5d1a4806ceeb66a10b23ba65", [:mix], [{:connection, "~> 1.0.3", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 1.1.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.2", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 2.2.2", [hex: :ecto, repo: "hexpm", optional: false]}, {:esqlite, "~> 0.2.3", [hex: :esqlite, repo: "hexpm", optional: false]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: false]}, {:sqlitex, "~> 1.3.2 or ~> 1.4", [hex: :sqlitex, repo: "hexpm", optional: false]}], "hexpm"},
+ "sqlitex": {:hex, :sqlitex, "1.3.3", "3aac5fd702be346f71d9de6e01702c9954484cd0971aa443490bb3bde045d919", [:mix], [{:decimal, "~> 1.1", [hex: :decimal, repo: "hexpm", optional: false]}, {:esqlite, "~> 0.2.3", [hex: :esqlite, repo: "hexpm", optional: false]}], "hexpm"}}
diff --git a/examples/phoenix_app/priv/repo/migrations/20171015110840_add_users.exs b/examples/phoenix_app/priv/repo/migrations/20171015110840_add_users.exs
new file mode 100644
index 0000000..2ab00ba
--- /dev/null
+++ b/examples/phoenix_app/priv/repo/migrations/20171015110840_add_users.exs
@@ -0,0 +1,11 @@
+defmodule PhoenixApp.Repo.Migrations.AddUsers do
+ use Ecto.Migration
+
+ def change do
+ create table(:users) do
+ add :name, :string, null: false
+ add :email, :string, null: false
+ timestamps()
+ end
+ end
+end
diff --git a/examples/phoenix_app/test/phoenix_app_web/controllers/user_controller_test.exs b/examples/phoenix_app/test/phoenix_app_web/controllers/user_controller_test.exs
new file mode 100644
index 0000000..158406a
--- /dev/null
+++ b/examples/phoenix_app/test/phoenix_app_web/controllers/user_controller_test.exs
@@ -0,0 +1,57 @@
+defmodule PhoenixAppWeb.UserControllerTest do
+ use PhoenixAppWeb.ConnCase, async: true
+ import OpenApiSpex.Test.Assertions
+
+ setup do
+ %{spec: PhoenixAppWeb.ApiSpec.spec()}
+ end
+
+ test "create user", %{conn: conn, spec: spec} do
+ conn
+ |> Plug.Conn.put_req_header("content-type", "application/json")
+ |> post(user_path(conn, :create, %{"user" => %{"name" => "Joe", "email" => "joe@gmail.com"}}))
+ |> 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: "Carl@yahoo.com"})
+
+ conn
+ |> Plug.Conn.put_req_header("accept", "application/json")
+ |> get(user_path(conn, :show, id))
+ |> json_response(200)
+ |> assert_schema("UserResponse", spec)
+ end
+
+ test "list users", %{conn: conn, spec: spec} do
+ %{id: id1} = PhoenixApp.Repo.insert!(%PhoenixApp.Accounts.User{name: "Aaron", email: "aaron@hotmail.com"})
+ %{id: id2} = PhoenixApp.Repo.insert!(%PhoenixApp.Accounts.User{name: "Benjamin", email: "ben@lycos.com"})
+ %{id: id3} = PhoenixApp.Repo.insert!(%PhoenixApp.Accounts.User{name: "Chuck", email: "chuck@aol.com"})
+
+ response =
+ conn
+ |> Plug.Conn.put_req_header("accept", "application/json")
+ |> get(user_path(conn, :index))
+ |> json_response(200)
+ |> assert_schema("UsersResponse", spec)
+
+ assert [%{id: ^id1}, %{id: ^id2}, %{id: ^id3}] = response.data
+ end
+
+ test "User schema example", %{spec: spec} do
+ assert_schema(PhoenixAppWeb.Schemas.User.schema().example, "User", spec)
+ end
+
+ test "UserRequest schema example", %{spec: spec} do
+ assert_schema(PhoenixAppWeb.Schemas.UserRequest.schema().example, "UserRequest", spec)
+ end
+
+ test "UserResponse schema example", %{spec: spec} do
+ assert_schema(PhoenixAppWeb.Schemas.UserResponse.schema().example, "UserResponse", spec)
+ end
+
+ test "UsersResponse schema example", %{spec: spec} do
+ assert_schema(PhoenixAppWeb.Schemas.UsersResponse.schema().example, "UsersResponse", spec)
+ end
+end
\ No newline at end of file
diff --git a/examples/phoenix_app/test/support/conn_case.ex b/examples/phoenix_app/test/support/conn_case.ex
new file mode 100644
index 0000000..c9f451f
--- /dev/null
+++ b/examples/phoenix_app/test/support/conn_case.ex
@@ -0,0 +1,19 @@
+defmodule PhoenixAppWeb.ConnCase do
+ use ExUnit.CaseTemplate
+
+ using do
+ quote do
+ use Phoenix.ConnTest
+ import PhoenixAppWeb.Router.Helpers
+ @endpoint PhoenixAppWeb.Endpoint
+ end
+ end
+
+ 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
diff --git a/examples/phoenix_app/test/test_helper.exs b/examples/phoenix_app/test/test_helper.exs
new file mode 100644
index 0000000..3c9a83e
--- /dev/null
+++ b/examples/phoenix_app/test/test_helper.exs
@@ -0,0 +1,3 @@
+ExUnit.start()
+
+Ecto.Adapters.SQL.Sandbox.mode(PhoenixApp.Repo, :manual)

File Metadata

Mime Type
text/x-diff
Expires
Sun, Nov 24, 2:26 AM (19 h, 28 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39265
Default Alt Text
(23 KB)

Event Timeline