Skip to content
Snippets Groups Projects
Unverified Commit b3c69861 authored by Bryan Maass's avatar Bryan Maass Committed by GitHub
Browse files

add implementations of write-body-to-stream for numbers / booleans (#21828)

* add implementations of write-body-to-stream for numbers / booleans
* add tests for getting non-string settings over the api
* remove blank line from end of api/segment.clj
parent 3675e8b4
No related branches found
No related tags found
No related merge requests found
......@@ -279,9 +279,4 @@
(wrap-response-if-needed [m]
(if (and (:status m) (contains? m :body))
m
{:status 200, :body m}))
;; Not sure why this is but the JSON serialization middleware barfs if response is just a plain boolean
Boolean
(wrap-response-if-needed [_]
(throw (Exception. (tru "Attempted to return a boolean as an API response. This is not allowed!")))))
{:status 200, :body m})))
......@@ -132,5 +132,4 @@
[id]
(-> id Segment api/read-check related/related))
(api/define-routes)
......@@ -188,7 +188,21 @@
ManyToManyChannel
(write-body-to-stream [chan _ ^OutputStream output-stream]
(log/debug (u/format-color 'green (trs "starting streaming response")))
(write-chan-vals-to-writer! (async-keepalive-channel chan) (io/writer output-stream))))
(write-chan-vals-to-writer! (async-keepalive-channel chan) (io/writer output-stream)))
;; java.lang.Double, java.lang.Long, and java.lang.Boolean will be given a Content-Type of "application/json; charset=utf-8"
;; so they should be strings, and will be parsed into their respective values.
java.lang.Double
(write-body-to-stream [num response output-stream]
(ring.protocols/write-body-to-stream (str num) response output-stream))
java.lang.Long
(write-body-to-stream [num response output-stream]
(ring.protocols/write-body-to-stream (str num) response output-stream))
java.lang.Boolean
(write-body-to-stream [bool response output-stream]
(ring.protocols/write-body-to-stream (str bool) response output-stream)))
;; `defendpoint-async` responses
(extend-protocol Sendable
......
(ns metabase.api.setting-test
(:require [clojure.test :refer :all]
[metabase.models.setting-test :refer [test-sensitive-setting test-setting-1 test-setting-2 test-setting-3
test-user-local-allowed-setting test-user-local-only-setting]]
[metabase.models.setting :as setting :refer [defsetting]]
[metabase.models.setting-test
:refer
[test-sensitive-setting
test-setting-1
test-setting-2
test-setting-3
test-user-local-allowed-setting
test-user-local-only-setting]]
[metabase.public-settings.premium-features-test :as premium-features-test]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
[metabase.util.i18n :refer [deferred-tru]]
[schema.core :as s]))
(use-fixtures :once (fixtures/initialize :db))
(defsetting test-api-setting-boolean
(deferred-tru "Test setting - this only shows up in dev (3)")
:visibility :public
:type :boolean)
(defsetting test-api-setting-double
(deferred-tru "Test setting - this only shows up in dev (3)")
:visibility :public
:type :double)
(defsetting test-api-setting-integer
(deferred-tru "Test setting - this only shows up in dev (3)")
:visibility :public
:type :integer)
;; ## Helper Fns
(defn- fetch-test-settings
"Fetch all test settings."
......@@ -17,6 +40,7 @@
:when (re-find #"^test-setting-\d$" (name (:key setting)))]
setting))
(defn- fetch-setting
"Fetch a single setting."
([setting-name status]
......@@ -57,7 +81,19 @@
(testing "Check that non-superusers cannot fetch a single setting if it is not user-local"
(is (= "You don't have permissions to do that."
(fetch-setting :rasta :test-setting-2 403))))))
(fetch-setting :rasta :test-setting-2 403))))
(testing "non-string values work over the api (#20735)"
;; n.b. the api will return nil if a setting is its default value.
(test-api-setting-double 3.14)
(is (= 3.14 (fetch-setting :test-api-setting-double 200)))
(test-api-setting-boolean true)
(is (= true (fetch-setting :test-api-setting-boolean 200)))
(test-api-setting-integer 42)
(is (= 42 (fetch-setting :test-api-setting-integer 200))))))
(test-api-setting-integer)
(deftest fetch-calculated-settings-test
(testing "GET /api/setting"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment