The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
json from data-url (#65374)
Jeff Bruemmer9638db8b0269
diff --git a/resources/openapi/index_inline.html.mustache b/resources/openapi/index_external.html.mustachesimilarity index 95%rename from resources/openapi/index_inline.html.mustacherename to resources/openapi/index_external.html.mustacheindex 5c77821f1bd..9c271feb04e 100644--- a/resources/openapi/index_inline.html.mustache+++ b/resources/openapi/index_external.html.mustache@@ -72,11 +72,7 @@ redirect_from:}</style><body>- <script- id="api-reference"- type="application/json">-{{json}}- </script>+ <script id="api-reference" data-url="{{json_url}}"></script><script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script></body>diff --git a/src/metabase/cmd/endpoint_dox.clj b/src/metabase/cmd/endpoint_dox.cljindex 5213da8340b..2a2e3b26f60 100644--- a/src/metabase/cmd/endpoint_dox.clj+++ b/src/metabase/cmd/endpoint_dox.clj@@ -47,19 +47,24 @@scalar-config))(defn generate-dox!- "Generates OpenAPI/Scalar documentation and writes it to a file.+ "Generates OpenAPI/Scalar documentation and writes it to files.- With no arguments, writes to `docs/api.html`.- With an output-file argument, writes to the specified path."+ With no arguments, writes to `docs/api.html` and `docs/api.json`.+ With an output-file argument, writes to the specified path and generates+ a corresponding JSON file with the same basename."([](generate-dox! "docs/api.html"))([^String output-file](printf "Generating OpenAPI+Scalar documentation in %s\n" output-file)(let [openapi-json (json/encode (openapi-object) {:pretty true})+ json-file (str/replace output-file #"\.html$" ".json")content (fill-template-placeholder- "openapi/index_inline.html.mustache"- "{{json}}"- openapi-json)]+ "openapi/index_external.html.mustache"+ "{{json_url}}"+ "api.json")]+ (printf "Writing OpenAPI JSON to %s\n" json-file)+ (cmd.common/write-doc-file! json-file openapi-json)+ (printf "Writing HTML to %s\n" output-file)(cmd.common/write-doc-file! output-file content))(println "Done.")))diff --git a/test/metabase/cmd/endpoint_dox_test.clj b/test/metabase/cmd/endpoint_dox_test.cljindex 2c2ca4ff2ad..4826d7ed9d0 100644--- a/test/metabase/cmd/endpoint_dox_test.clj+++ b/test/metabase/cmd/endpoint_dox_test.clj@@ -1,15 +1,97 @@(ns metabase.cmd.endpoint-dox-test(:require[clojure.java.io :as io]+ [clojure.string :as str][clojure.test :refer :all][metabase.cmd.endpoint-dox :as cmd.endpoint-dox]- [metabase.test :as mt]))+ [metabase.test :as mt]+ [metabase.util.json :as json]))(set! *warn-on-reflection* true)(deftest generate-dox-test- (testing "Make sure we can run the command successfully"- (mt/with-temp-file [filename]- (cmd.endpoint-dox/generate-dox! filename)- (is (.exists (io/file filename)))- (io/delete-file filename))))+ (testing "Should generate both HTML and JSON files"+ (mt/with-temp-file [temp-file]+ (let [html-file (str temp-file ".html")+ json-file (str temp-file ".json")]+ (try+ (cmd.endpoint-dox/generate-dox! html-file)+ (testing "HTML file should be created"+ (is (.exists (io/file html-file))))+ (testing "JSON file should be created"+ (is (.exists (io/file json-file))))+ (finally+ (when (.exists (io/file json-file))+ (io/delete-file json-file))+ (when (.exists (io/file html-file))+ (io/delete-file html-file))))))))++(deftest generate-dox-html-uses-external-json-test+ (testing "HTML file should use data-url to load external JSON"+ (mt/with-temp-file [temp-file]+ (let [html-file (str temp-file ".html")+ json-file (str temp-file ".json")]+ (try+ (cmd.endpoint-dox/generate-dox! html-file)+ (let [html-content (slurp html-file)]+ (testing "Should use data-url attribute"+ (is (str/includes? html-content "data-url=\"api.json\"")))+ (testing "Should NOT contain inline JSON"+ (is (not (str/includes? html-content "type=\"application/json\""))))+ (testing "Should load Scalar script"+ (is (str/includes? html-content "cdn.jsdelivr.net/npm/@scalar/api-reference"))))+ (finally+ (when (.exists (io/file json-file))+ (io/delete-file json-file))+ (when (.exists (io/file html-file))+ (io/delete-file html-file))))))))++(deftest generate-dox-json-structure-test+ (testing "JSON file should contain valid OpenAPI spec with description"+ (mt/with-temp-file [temp-file]+ (let [html-file (str temp-file ".html")+ json-file (str temp-file ".json")]+ (try+ (cmd.endpoint-dox/generate-dox! html-file)+ (let [json-content (json/decode (slurp json-file) keyword)]+ (testing "Should have OpenAPI version"+ (is (= "3.1.0" (:openapi json-content))))+ (testing "Should have info section"+ (is (map? (:info json-content))))+ (testing "Should have title"+ (is (= "Metabase API documentation" (get-in json-content [:info :title]))))+ (testing "Should have description from api-intro.md"+ (let [description (get-in json-content [:info :description])]+ (is (string? description))+ (is (str/includes? description "The API is subject to change"))+ (is (str/includes? description "API tutorial"))))+ (testing "Should have servers configuration"+ (is (sequential? (:servers json-content))))+ (testing "Should have components section"+ (is (map? (:components json-content)))))+ (finally+ (when (.exists (io/file json-file))+ (io/delete-file json-file))+ (when (.exists (io/file html-file))+ (io/delete-file html-file))))))))++(deftest generate-dox-html-size-test+ (testing "HTML file should be small (no inline JSON)"+ (mt/with-temp-file [temp-file]+ (let [html-file (str temp-file ".html")+ json-file (str temp-file ".json")]+ (try+ (cmd.endpoint-dox/generate-dox! html-file)+ (let [html-size (.length (io/file html-file))+ json-size (.length (io/file json-file))]+ (testing "HTML should be much smaller than JSON"+ (is (< html-size 10000)+ "HTML file should be less than 10KB"))+ (testing "JSON should contain the bulk of the data"+ (is (> json-size html-size)+ "JSON file should be larger than HTML file")))+ (finally+ (when (.exists (io/file json-file))+ (io/delete-file json-file))+ (when (.exists (io/file html-file))+ (io/delete-file html-file))))))))