Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add --check option in Mix tasks to compare the generated spec with a previously generated file #618

Merged
Merged
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
3 changes: 3 additions & 0 deletions lib/mix/tasks/openapi.spec.json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Mix.Tasks.Openapi.Spec.Json do

$ mix openapi.spec.json --spec PhoenixAppWeb.ApiSpec apispec.json
$ mix openapi.spec.json --spec PhoenixAppWeb.ApiSpec --pretty=true
$ mix openapi.spec.json --spec PhoenixAppWeb.ApiSpec --check=true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small stylistic note: Boolean arga can be set to true with just --arg, looks a bit nicer than --arg=true

$ mix openapi.spec.json --spec PhoenixAppWeb.ApiSpec --start-app=false
$ mix openapi.spec.json --spec PhoenixAppWeb.ApiSpec --vendor-extensions=false

Expand All @@ -16,6 +17,8 @@ defmodule Mix.Tasks.Openapi.Spec.Json do

* `--pretty` - Whether to prettify the generated JSON (defaults to false)

* `--check` - Whether to only compare the generated JSON with the spec file (defaults to false)

* `--start-app` - Whether need to start application before generate schema (defaults to true)

* `--vendor-extensions` - Whether to include open_api_spex OpenAPI vendor extensions
Expand Down
3 changes: 3 additions & 0 deletions lib/mix/tasks/openapi.spec.yaml.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ defmodule Mix.Tasks.Openapi.Spec.Yaml do
## Examples

$ mix openapi.spec.yaml --spec PhoenixAppWeb.ApiSpec apispec.yaml
$ mix openapi.spec.yaml --spec PhoenixAppWeb.ApiSpec --check=true
$ mix openapi.spec.yaml --spec PhoenixAppWeb.ApiSpec --start-app=false
$ mix openapi.spec.yaml --spec PhoenixAppWeb.ApiSpec --vendor-extensions=false

## Command line options

* `--spec` - The ApiSpec module from which to generate the OpenAPI YAML file

* `--check` - Whether to only compare the generated YAML with the spec file (defaults to false)

* `--start-app` - Whether to start the application before generating the schema (defaults to true)

* `--vendor-extensions` - Whether to include open_api_spex OpenAPI vendor extensions
Expand Down
29 changes: 24 additions & 5 deletions lib/open_api_spex/export_spec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,27 @@ defmodule OpenApiSpex.ExportSpec do
defmodule Options do
@moduledoc false

defstruct filename: nil, spec: nil, pretty: false, vendor_extensions: true, quiet: false
defstruct filename: nil,
spec: nil,
pretty: false,
check: false,
vendor_extensions: true,
quiet: false
end

def call(argv, encode_spec, default_filename) do
opts = parse_options(argv, default_filename)

opts
|> generate_spec()
|> encode_spec.(opts)
|> write_spec(opts)
encoded_spec =
opts
|> generate_spec()
|> encode_spec.(opts)

if opts.check do
check_spec(encoded_spec, opts)
else
write_spec(encoded_spec, opts)
end
end

defp generate_spec(%{spec: spec, vendor_extensions: vendor_extensions}) do
Expand Down Expand Up @@ -45,6 +56,7 @@ defmodule OpenApiSpex.ExportSpec do
spec: :string,
endpoint: :string,
pretty: :boolean,
check: :boolean,
vendor_extensions: :boolean,
quiet: :boolean
]
Expand All @@ -56,11 +68,18 @@ defmodule OpenApiSpex.ExportSpec do
filename: args |> List.first() || default_filename,
spec: find_spec(opts),
pretty: Keyword.get(opts, :pretty, false),
check: Keyword.get(opts, :check, false),
vendor_extensions: Keyword.get(opts, :vendor_extensions, true),
quiet: Keyword.get(opts, :quiet, false)
}
end

defp check_spec(content, opts) do
unless content == File.read!(opts.filename) do
Mix.raise("The OpenAPI spec file does not match the generated spec:\n\n#{content}")
end
end

defp write_spec(content, opts) do
case Path.dirname(opts.filename) do
"." -> true
Expand Down
Loading