Skip to content

Commit 7fd57cf

Browse files
committed
Add Swagger UI docs endpoint at /jsonapi/docs
Serves a lightweight HTML page that loads Swagger UI from the CDN and points it at the existing /jsonapi/openapi.json endpoint. No extra gem dependencies required. Signed-off-by: Thomas von Deyen <thomas@vondeyen.com>
1 parent f34743d commit 7fd57cf

4 files changed

Lines changed: 52 additions & 1 deletion

File tree

app/controllers/alchemy/json_api/openapi_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ def show
1010
render json: spec
1111
end
1212

13+
def docs
14+
@spec_url = alchemy_json_api.openapi_path(format: :json)
15+
render layout: false
16+
end
17+
1318
private
1419

1520
def spec
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Alchemy JSON:API Documentation</title>
6+
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css">
7+
<style>html { box-sizing: border-box; } *, *::before, *::after { box-sizing: inherit; }</style>
8+
</head>
9+
<body>
10+
<div id="swagger-ui"></div>
11+
<script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js"></script>
12+
<script>
13+
SwaggerUIBundle({
14+
url: <%== @spec_url.to_json %>,
15+
dom_id: '#swagger-ui',
16+
deepLinking: true,
17+
presets: [SwaggerUIBundle.presets.apis, SwaggerUIBundle.SwaggerUIStandalonePreset],
18+
layout: "BaseLayout"
19+
});
20+
</script>
21+
</body>
22+
</html>

config/routes.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# frozen_string_literal: true
22

33
Alchemy::JsonApi::Engine.routes.draw do
4-
get "openapi", to: "openapi#show", defaults: {format: :json}
4+
get "openapi", to: "openapi#show", defaults: {format: :json}, as: :openapi
5+
get "docs", to: "openapi#docs", constraints: ->(_r) { Rails.env.development? }
56

67
resources :pages, only: [:index]
78
get "pages/*path" => "pages#show", :as => :page

spec/requests/alchemy/json_api/openapi_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,27 @@
1616
expect(document["paths"]).to include("/pages", "/pages/{path}", "/nodes")
1717
end
1818
end
19+
20+
describe "GET /alchemy/json_api/docs" do
21+
it "returns an HTML page with Swagger UI in development" do
22+
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new("development"))
23+
Rails.application.reload_routes!
24+
25+
get alchemy_json_api.docs_path
26+
27+
expect(response).to have_http_status(:ok)
28+
expect(response.content_type).to include("text/html")
29+
expect(response.body).to include("swagger-ui")
30+
expect(response.body).to include("openapi.json")
31+
ensure
32+
allow(Rails).to receive(:env).and_call_original
33+
Rails.application.reload_routes!
34+
end
35+
36+
it "is not routable in non-development environments" do
37+
expect {
38+
alchemy_json_api.docs_path
39+
}.to raise_error(NoMethodError)
40+
end
41+
end
1942
end

0 commit comments

Comments
 (0)