-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable openapi schema versioning (#1488)
* Convert the open api plug into a versinable macro * Move V1 schemas to its own folder * Update controllers to used versioned openapi * Update router to pipethrough specific open api version * Disable open api cache on dev * Update controller tests to use new openapi versions * Temporarily set openapi V1 version in CI docs generation * Test paths generation in open api spec
- Loading branch information
Showing
43 changed files
with
216 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,93 @@ | ||
defmodule TrentoWeb.OpenApi.ApiSpec do | ||
@moduledoc """ | ||
OpenApi specification entry point | ||
`api_version` must be provided to specify the version of this openapi specification | ||
Example: | ||
use TrentoWeb.OpenApi.ApiSpec, | ||
api_version: "v1" | ||
""" | ||
|
||
alias OpenApiSpex.{ | ||
Components, | ||
Info, | ||
OpenApi, | ||
Paths, | ||
SecurityScheme, | ||
Server, | ||
Tag | ||
} | ||
defmacro __using__(opts) do | ||
api_version = | ||
Keyword.get(opts, :api_version) || raise ArgumentError, "expected :api_version option" | ||
|
||
alias TrentoWeb.{Endpoint, Router} | ||
@behaviour OpenApi | ||
quote do | ||
alias OpenApiSpex.{ | ||
Components, | ||
Info, | ||
OpenApi, | ||
Paths, | ||
SecurityScheme, | ||
Server, | ||
Tag | ||
} | ||
|
||
@impl OpenApi | ||
def spec do | ||
OpenApiSpex.resolve_schema_modules(%OpenApi{ | ||
servers: [ | ||
endpoint() | ||
], | ||
info: %Info{ | ||
title: "Trento", | ||
description: to_string(Application.spec(:trento, :description)), | ||
version: to_string(Application.spec(:trento, :vsn)) | ||
}, | ||
components: %Components{ | ||
securitySchemes: %{"authorization" => %SecurityScheme{type: "http", scheme: "bearer"}} | ||
}, | ||
security: [%{"authorization" => []}], | ||
# Populate the paths from a phoenix router | ||
paths: Paths.from_router(Router), | ||
tags: [ | ||
%Tag{ | ||
name: "Target Infrastructure", | ||
description: "Providing access to the discovered target infrastructure" | ||
}, | ||
%Tag{ | ||
name: "Checks", | ||
description: "Providing Checks related feature" | ||
}, | ||
%Tag{ | ||
name: "Platform", | ||
description: "Providing access to Trento Platform features" | ||
} | ||
] | ||
}) | ||
end | ||
alias TrentoWeb.{Endpoint, Router} | ||
@behaviour OpenApi | ||
|
||
@impl OpenApi | ||
def spec(router \\ Router) do | ||
OpenApiSpex.resolve_schema_modules(%OpenApi{ | ||
servers: [ | ||
endpoint() | ||
], | ||
info: %Info{ | ||
title: "Trento", | ||
description: to_string(Application.spec(:trento, :description)), | ||
version: to_string(Application.spec(:trento, :vsn)) | ||
}, | ||
components: %Components{ | ||
securitySchemes: %{"authorization" => %SecurityScheme{type: "http", scheme: "bearer"}} | ||
}, | ||
security: [%{"authorization" => []}], | ||
paths: build_paths_for_version(unquote(api_version), router), | ||
tags: [ | ||
%Tag{ | ||
name: "Target Infrastructure", | ||
description: "Providing access to the discovered target infrastructure" | ||
}, | ||
%Tag{ | ||
name: "Checks", | ||
description: "Providing Checks related feature" | ||
}, | ||
%Tag{ | ||
name: "Platform", | ||
description: "Providing access to Trento Platform features" | ||
} | ||
] | ||
}) | ||
end | ||
|
||
defp endpoint do | ||
if Process.whereis(Endpoint) do | ||
# Populate the Server info from a phoenix endpoint | ||
Server.from_endpoint(Endpoint) | ||
else | ||
# If the endpoint is not running, use a placeholder | ||
# this happens when generarting openapi.json with --start-app=false | ||
# e.g. mix openapi.spec.json --start-app=false --spec WandaWeb.ApiSpec | ||
%OpenApiSpex.Server{url: "https://demo.trento-project.io"} | ||
end | ||
end | ||
|
||
defp build_paths_for_version(version, router) do | ||
excluded_versions = List.delete(router.available_api_versions(), version) | ||
|
||
router | ||
|> Paths.from_router() | ||
|> Enum.reject(fn {path, _info} -> | ||
current_version = | ||
path | ||
|> String.trim("/") | ||
|> String.split("/") | ||
|> Enum.at(1) | ||
|
||
defp endpoint do | ||
if Process.whereis(Endpoint) do | ||
# Populate the Server info from a phoenix endpoint | ||
Server.from_endpoint(Endpoint) | ||
else | ||
# If the endpoint is not running, use a placeholder | ||
# this happens when generarting openapi.json with --start-app=false | ||
# e.g. mix openapi.spec.json --start-app=false --spec WandaWeb.ApiSpec | ||
%OpenApiSpex.Server{url: "https://demo.trento-project.io"} | ||
Enum.member?(excluded_versions, current_version) | ||
end) | ||
|> Map.new() | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
defmodule TrentoWeb.OpenApi.V1.ApiSpec do | ||
@moduledoc """ | ||
OpenApi specification entry point for V1 version | ||
""" | ||
|
||
use TrentoWeb.OpenApi.ApiSpec, | ||
api_version: "v1" | ||
end |
2 changes: 1 addition & 1 deletion
2
lib/trento_web/openapi/schema/bad_request.ex → ...ento_web/openapi/v1/schema/bad_request.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/trento_web/openapi/schema/checks.ex → lib/trento_web/openapi/v1/schema/checks.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ento_web/openapi/schema/checks_catalog.ex → ...o_web/openapi/v1/schema/checks_catalog.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
lib/trento_web/openapi/schema/cluster.ex → lib/trento_web/openapi/v1/schema/cluster.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
lib/trento_web/openapi/schema/database.ex → lib/trento_web/openapi/v1/schema/database.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...nto_web/openapi/schema/discovery_event.ex → ..._web/openapi/v1/schema/discovery_event.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/trento_web/openapi/schema/health.ex → lib/trento_web/openapi/v1/schema/health.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
lib/trento_web/openapi/schema/host.ex → lib/trento_web/openapi/v1/schema/host.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/trento_web/openapi/schema/http_std.ex → lib/trento_web/openapi/v1/schema/http_std.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.