Skip to content
Draft
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
30 changes: 13 additions & 17 deletions lib/exunit_openapi.ex
Original file line number Diff line number Diff line change
Expand Up @@ -131,27 +131,23 @@ defmodule ExUnitOpenAPI do
end)
end

defp write_spec(spec, output_path, config) do
defp write_spec(spec, output_path, _config) do
dir = Path.dirname(output_path)
File.mkdir_p!(dir)

content =
case Config.format(config) do
:json -> Jason.encode!(spec, pretty: true)
:yaml -> encode_yaml(spec)
end

File.write!(output_path, content)
{:ok, output_path}
with :ok <- File.mkdir_p(dir),
{:ok, content} <- encode_json(spec),
:ok <- File.write(output_path, content) do
{:ok, output_path}
else
{:error, reason} ->
{:error, {:file_write_failed, output_path, reason}}
end
end

defp encode_yaml(spec) do
if Code.ensure_loaded?(YamlElixir) do
# YamlElixir doesn't have an encoder, so we'd need a different library
# For now, fall back to JSON
Jason.encode!(spec, pretty: true)
else
raise "yaml_elixir is required for YAML output. Add {:yaml_elixir, \"~> 2.9\"} to your deps."
defp encode_json(spec) do
case Jason.encode(spec, pretty: true) do
{:ok, json} -> {:ok, json}
{:error, reason} -> {:error, {:json_encode_failed, reason}}
end
end
end
9 changes: 0 additions & 9 deletions lib/exunit_openapi/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ defmodule ExUnitOpenAPI.Config do
config :exunit_openapi,
router: MyAppWeb.Router, # Required: Your Phoenix router module
output: "openapi.json", # Output file path (default: openapi.json)
format: :json, # Output format: :json or :yaml
info: [ # OpenAPI info object
title: "My API",
version: "1.0.0",
Expand All @@ -23,7 +22,6 @@ defmodule ExUnitOpenAPI.Config do
@default_config %{
router: nil,
output: "openapi.json",
format: :json,
info: %{
title: "API",
version: "1.0.0"
Expand All @@ -36,7 +34,6 @@ defmodule ExUnitOpenAPI.Config do
@type t :: %{
router: module() | nil,
output: String.t(),
format: :json | :yaml,
info: map(),
servers: list(map()),
security_schemes: map(),
Expand Down Expand Up @@ -64,12 +61,6 @@ defmodule ExUnitOpenAPI.Config do
@spec output_path(t()) :: String.t()
def output_path(%{output: output}), do: output

@doc """
Gets the output format from config.
"""
@spec format(t()) :: :json | :yaml
def format(%{format: format}), do: format

@doc """
Gets the router module from config.
"""
Expand Down
7 changes: 0 additions & 7 deletions lib/mix/tasks/openapi.generate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ defmodule Mix.Tasks.Openapi.Generate do
## Options

* `--output` - Output file path (default: from config or "openapi.json")
* `--format` - Output format: json or yaml (default: json)
* `--only` - Only run tests matching the given tag
* `--exclude` - Exclude tests matching the given tag

Expand All @@ -41,7 +40,6 @@ defmodule Mix.Tasks.Openapi.Generate do
OptionParser.parse(args,
switches: [
output: :string,
format: :string,
only: :keep,
exclude: :keep
]
Expand All @@ -64,11 +62,6 @@ defmodule Mix.Tasks.Openapi.Generate do
Application.put_env(:exunit_openapi, :output, opts[:output])
end

if opts[:format] do
format = String.to_atom(opts[:format])
Application.put_env(:exunit_openapi, :format, format)
end

# Run the test task
Mix.Task.run("test", test_args)
end
Expand Down
8 changes: 0 additions & 8 deletions test/exunit_openapi/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ defmodule ExUnitOpenAPI.ConfigTest do

assert config.router == nil
assert config.output == "openapi.json"
assert config.format == :json
assert config.info == %{title: "API", version: "1.0.0"}
assert config.servers == []
assert config.security_schemes == %{}
Expand All @@ -43,13 +42,11 @@ defmodule ExUnitOpenAPI.ConfigTest do
test "loads config from application env" do
Application.put_env(:exunit_openapi, :router, MyApp.Router)
Application.put_env(:exunit_openapi, :output, "priv/api.json")
Application.put_env(:exunit_openapi, :format, :yaml)

config = Config.load()

assert config.router == MyApp.Router
assert config.output == "priv/api.json"
assert config.format == :yaml
end

test "override options take precedence over app env" do
Expand Down Expand Up @@ -121,11 +118,6 @@ defmodule ExUnitOpenAPI.ConfigTest do
assert Config.output_path(config) == "custom/path.json"
end

test "format/1 returns format" do
config = Config.load(format: :yaml)
assert Config.format(config) == :yaml
end

test "router/1 returns router module" do
config = Config.load(router: MyApp.Router)
assert Config.router(config) == MyApp.Router
Expand Down
28 changes: 4 additions & 24 deletions test/mix/tasks/openapi_generate_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ defmodule Mix.Tasks.Openapi.GenerateTest do
# Clean up env before each test
System.delete_env("OPENAPI")
Application.delete_env(:exunit_openapi, :output)
Application.delete_env(:exunit_openapi, :format)

on_exit(fn ->
System.delete_env("OPENAPI")
Application.delete_env(:exunit_openapi, :output)
Application.delete_env(:exunit_openapi, :format)
end)

:ok
Expand All @@ -25,7 +23,6 @@ defmodule Mix.Tasks.Openapi.GenerateTest do
OptionParser.parse(["--output", "custom/path.json"],
switches: [
output: :string,
format: :string,
only: :keep,
exclude: :keep
]
Expand All @@ -34,26 +31,11 @@ defmodule Mix.Tasks.Openapi.GenerateTest do
assert opts[:output] == "custom/path.json"
end

test "parses --format option" do
{opts, _, _} =
OptionParser.parse(["--format", "yaml"],
switches: [
output: :string,
format: :string,
only: :keep,
exclude: :keep
]
)

assert opts[:format] == "yaml"
end

test "parses multiple --only options" do
{opts, _, _} =
OptionParser.parse(["--only", "integration", "--only", "api"],
switches: [
output: :string,
format: :string,
only: :keep,
exclude: :keep
]
Expand All @@ -66,17 +48,15 @@ defmodule Mix.Tasks.Openapi.GenerateTest do

test "parses mixed options" do
{opts, _, _} =
OptionParser.parse(["--output", "api.json", "--format", "json", "--only", "api", "--exclude", "slow"],
OptionParser.parse(["--output", "api.json", "--only", "api", "--exclude", "slow"],
switches: [
output: :string,
format: :string,
only: :keep,
exclude: :keep
]
)

assert opts[:output] == "api.json"
assert opts[:format] == "json"
assert opts[:only] == "api"
assert opts[:exclude] == "slow"
end
Expand All @@ -97,8 +77,8 @@ defmodule Mix.Tasks.Openapi.GenerateTest do
assert test_args == ["--only", "integration", "--exclude", "slow"]
end

test "ignores output and format when building test args" do
opts = [output: "api.json", format: "yaml", only: "api"]
test "ignores output when building test args" do
opts = [output: "api.json", only: "api"]

test_args =
opts
Expand All @@ -121,7 +101,7 @@ defmodule Mix.Tasks.Openapi.GenerateTest do
{:docs_v1, _, _, _, %{"en" => moduledoc}, _, _} = Code.fetch_docs(Generate)
assert moduledoc =~ "mix openapi.generate"
assert moduledoc =~ "--output"
assert moduledoc =~ "--format"
assert moduledoc =~ "--only"
end
end
end