Skip to content

Commit

Permalink
Fix endpoint not loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
shikanime committed Jul 16, 2020
1 parent 223732d commit 439297c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 44 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ You can write the swagger file to disk using the following Mix task and optional
convenience, create a direct alias:

```shell
mix openapi.spec.json --endpoint MyAppWeb.Endpoint --spec MyAppWeb.ApiSpec
mix openapi.spec.json --spec MyAppWeb.ApiSpec
```

## Serve Swagger UI
Expand Down
5 changes: 4 additions & 1 deletion examples/phoenix_app/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ defmodule PhoenixApp.Mixfile do
defp elixirc_paths(_), do: ["lib"]

defp aliases() do
[test: ["ecto.create --quiet", "ecto.migrate", "test"]]
[
test: ["ecto.create --quiet", "ecto.migrate", "test"],
spec: ["openapi.spec.json --spec PhoenixAppWeb.ApiSpec \"priv/static/swagger.json\""]
]
end

# Specifies your project dependencies.
Expand Down
44 changes: 2 additions & 42 deletions lib/mix/tasks/openapi.spec.json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,17 @@ defmodule Mix.Tasks.Openapi.Spec.Json do
defmodule Options do
@moduledoc false

defstruct filename: nil, spec: nil, endpoint: nil, pretty: false
defstruct filename: nil, spec: nil, pretty: false
end

@impl true
def run(argv) do
Mix.Task.run("app.start")
opts = parse_options(argv)
maybe_start_endpoint(opts)
content = generate_spec(opts)
write_spec(content, opts.filename)
end

def maybe_start_endpoint(%{endpoint: nil}) do
:ok
end

def maybe_start_endpoint(%{endpoint: endpoint}) do
case Code.ensure_compiled(endpoint) do
{:module, _} ->
if function_exported?(endpoint, :start_link, 0) do
case endpoint.start_link() do
{:ok, _} ->
:ok

:ignore ->
:ok

{:error, error} ->
Mix.raise("could not start #{inspect(endpoint)}, error: #{inspect(error)}.")
end
else
Mix.raise(
"module #{inspect(endpoint)} is not a Phoenix.Endpoint. " <>
"Please pass a endpoint with the --endpoint option."
)
end

{:error, error} ->
Mix.raise(
"could not load #{inspect(endpoint)}, error: #{inspect(error)}. " <>
"Please pass a endpoint with the --endpoint option."
)
end
end

def generate_spec(%{spec: spec, pretty: pretty}) do
case Code.ensure_compiled(spec) do
{:module, _} ->
Expand Down Expand Up @@ -90,17 +57,10 @@ defmodule Mix.Tasks.Openapi.Spec.Json do
%Options{
filename: args |> List.first() || @default_filename,
spec: find_spec(opts),
endpoint: maybe_endpoint_as_atom(opts),
pretty: Keyword.get(opts, :pretty, false)
}
end

defp maybe_endpoint_as_atom(opts) do
if endpoint = Keyword.get(opts, :endpoint) do
Module.concat([endpoint])
end
end

defp find_spec(opts) do
if spec = Keyword.get(opts, :spec) do
Module.concat([spec])
Expand Down

0 comments on commit 439297c

Please sign in to comment.