From da0143cb47c60d6ba25d9eadced9353802b8e59a Mon Sep 17 00:00:00 2001 From: Brook <71849503+Primebrook@users.noreply.github.com> Date: Tue, 27 Jan 2026 15:28:48 +0000 Subject: [PATCH] json only format for now (not YAML) --- lib/exunit_openapi.ex | 30 ++++++++++-------------- lib/exunit_openapi/config.ex | 9 ------- lib/mix/tasks/openapi.generate.ex | 7 ------ test/exunit_openapi/config_test.exs | 8 ------- test/mix/tasks/openapi_generate_test.exs | 28 ++++------------------ 5 files changed, 17 insertions(+), 65 deletions(-) diff --git a/lib/exunit_openapi.ex b/lib/exunit_openapi.ex index e88bf2f..97261d1 100644 --- a/lib/exunit_openapi.ex +++ b/lib/exunit_openapi.ex @@ -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 diff --git a/lib/exunit_openapi/config.ex b/lib/exunit_openapi/config.ex index 822c657..4fd07a1 100644 --- a/lib/exunit_openapi/config.ex +++ b/lib/exunit_openapi/config.ex @@ -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", @@ -23,7 +22,6 @@ defmodule ExUnitOpenAPI.Config do @default_config %{ router: nil, output: "openapi.json", - format: :json, info: %{ title: "API", version: "1.0.0" @@ -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(), @@ -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. """ diff --git a/lib/mix/tasks/openapi.generate.ex b/lib/mix/tasks/openapi.generate.ex index d75ffd5..72c9ce1 100644 --- a/lib/mix/tasks/openapi.generate.ex +++ b/lib/mix/tasks/openapi.generate.ex @@ -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 @@ -41,7 +40,6 @@ defmodule Mix.Tasks.Openapi.Generate do OptionParser.parse(args, switches: [ output: :string, - format: :string, only: :keep, exclude: :keep ] @@ -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 diff --git a/test/exunit_openapi/config_test.exs b/test/exunit_openapi/config_test.exs index 1dcfbdc..6d71dca 100644 --- a/test/exunit_openapi/config_test.exs +++ b/test/exunit_openapi/config_test.exs @@ -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 == %{} @@ -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 @@ -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 diff --git a/test/mix/tasks/openapi_generate_test.exs b/test/mix/tasks/openapi_generate_test.exs index fe3269b..ad6fa6b 100644 --- a/test/mix/tasks/openapi_generate_test.exs +++ b/test/mix/tasks/openapi_generate_test.exs @@ -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 @@ -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 ] @@ -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 ] @@ -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 @@ -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 @@ -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