Skip to content

Commit a02a4f2

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 cc24e61 commit a02a4f2

4 files changed

Lines changed: 57 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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,32 @@
1616
expect(document["paths"]).to include("/pages", "/pages/{path}", "/nodes")
1717
end
1818
end
19+
20+
describe "GET /alchemy/json_api/docs" do
21+
context "in development environment" do
22+
before do
23+
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new("development"))
24+
Rails.application.reload_routes!
25+
end
26+
27+
it "returns an HTML page with Swagger UI in development", :aggregate_failures do
28+
get alchemy_json_api.docs_path
29+
30+
expect(response).to have_http_status(:ok)
31+
expect(response.content_type).to include("text/html")
32+
expect(response.body).to include("swagger-ui")
33+
expect(response.body).to include(alchemy_json_api.openapi_path)
34+
end
35+
36+
after do
37+
allow(Rails).to receive(:env).and_call_original
38+
Rails.application.reload_routes!
39+
end
40+
end
41+
42+
it "is not routable in non-development environments" do
43+
get alchemy_json_api.docs_path
44+
expect(response).to have_http_status(:not_found)
45+
end
46+
end
1947
end

0 commit comments

Comments
 (0)