Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Fixed

- [OpenAPI] Avoid caching a request-specific spec URL in the HTML docs page.
- [Router] Compose and restore `SCRIPT_NAME` correctly for mounted Rack apps.
- [OpenAPI] Fix schema registry key collisions for Blueprinter classes referenced under different views.

Expand Down
2 changes: 1 addition & 1 deletion lib/rage/openapi/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: '<%= spec_url %>',
url: window.location.pathname.replace(/\/?$/, "/json"),
dom_id: '#swagger-ui',
});
};
Expand Down
6 changes: 2 additions & 4 deletions lib/rage/openapi/openapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ module Rage::OpenAPI
# run Rage.openapi.application(namespace: "Api::V2")
# end
def self.application(namespace: nil)
html_app = ->(env) do
html_app = ->(_) do
__data_cache[[:page, namespace]] ||= begin
scheme, host, path = env["rack.url_scheme"], env["HTTP_HOST"], env["SCRIPT_NAME"]
spec_url = "#{scheme}://#{host}#{path}/json"
page = ERB.new(File.read("#{__dir__}/index.html.erb")).result(binding)
page = ERB.new(File.read("#{__dir__}/index.html.erb")).result

[200, { "content-type" => "text/html; charset=UTF-8" }, [page]]
end
Expand Down
36 changes: 36 additions & 0 deletions spec/openapi/openapi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,42 @@
require "prism"

RSpec.describe Rage::OpenAPI do
describe ".application" do
subject(:app) { described_class.application }

let(:first_env) do
{
"PATH_INFO" => "/",
"SCRIPT_NAME" => "/docs-a",
"HTTP_HOST" => "first.test:3000",
"rack.url_scheme" => "http"
}
end

let(:second_env) do
{
"PATH_INFO" => "/",
"SCRIPT_NAME" => "/docs-b",
"HTTP_HOST" => "second.test:4000",
"rack.url_scheme" => "https"
}
end

before do
allow(Rage.config).to receive(:middleware).and_return([])
end

it "renders a request-independent html page" do
_, _, first_body = app.call(first_env)
_, _, second_body = app.call(second_env)

expect(first_body.join).to eq(second_body.join)
expect(first_body.join).to include('url: window.location.pathname.replace(/\/?$/, "/json"),')
expect(first_body.join).not_to include("first.test:3000")
expect(first_body.join).not_to include("/docs-a/json")
end
end

describe ".__try_parse_collection" do
subject { described_class.__try_parse_collection(input) }

Expand Down