Skip to content

A platform for community discussion. Free, open, simple.

DEV: Allow query parameters to be sent without JSON-encoding them. (#35314)

Gary Pendergast68ce9699c43a
diff --git a/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rb b/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rb
index b747ef6b96d..16249db2988 100644
--- a/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rb
+++ b/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rb
@@ -152,7 +152,10 @@ module DiscourseDataExplorer
response.sending_file = true if params[:download]
query_params = {}
- query_params = MultiJson.load(params[:params]) if params[:params]
+ if params[:params]
+ query_params =
+ params[:params].is_a?(String) ? MultiJson.load(params[:params]) : params[:params]
+ end
opts = { current_user: current_user&.username }
opts[:explain] = true if params[:explain] == "true"
diff --git a/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb b/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb
index 265f4754d31..76f5ea517ff 100644
--- a/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb
+++ b/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb
@@ -184,6 +184,38 @@ describe DiscourseDataExplorer::QueryController do
expect(response_json["errors"].first).to match(/ValidationError/)
end
+ it "can accept parameters as a hash instead of JSON string" do
+ query = make_query <<~SQL
+ -- [params]
+ -- int :foo = 34
+ -- string :bar = 'default'
+ SELECT :foo as my_value, :bar as text_value
+ SQL
+
+ # Test with hash format
+ post "/admin/plugins/explorer/queries/#{query.id}/run.json",
+ params: {
+ params: {
+ foo: 42,
+ bar: "test",
+ },
+ }
+ expect(response.status).to eq(200)
+ expect(response_json["success"]).to eq(true)
+ expect(response_json["columns"]).to eq(%w[my_value text_value])
+ expect(response_json["rows"]).to eq([[42, "test"]])
+ end
+
+ it "can accept empty hash for parameters" do
+ query = make_query "SELECT 23 as my_value"
+
+ post "/admin/plugins/explorer/queries/#{query.id}/run.json", params: { params: {} }
+ expect(response.status).to eq(200)
+ expect(response_json["success"]).to eq(true)
+ expect(response_json["columns"]).to eq(["my_value"])
+ expect(response_json["rows"]).to eq([[23]])
+ end
+
context "when rate limited" do
def unlimited_request(query_id, headers = {})
post "/admin/plugins/explorer/queries/#{query_id}/run.json",
@@ -524,6 +556,21 @@ describe DiscourseDataExplorer::QueryController do
expect(response.parsed_body["rows"]).to eq([[1828]])
end
+ it "can accept parameters as a hash" do
+ query_string = <<~SQL
+ -- [params]
+ -- int :num = 100
+ SELECT :num as value
+ SQL
+ query = make_query(query_string, { name: "Parameterized Query" }, ["#{group.id}"])
+
+ post "/g/#{group.name}/reports/#{query.id}/run.json", params: { params: { num: 999 } }
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["success"]).to eq(true)
+ expect(response.parsed_body["columns"]).to eq(["value"])
+ expect(response.parsed_body["rows"]).to eq([[999]])
+ end
+
it "returns a 404 when the user should not have access to the query " do
group.add(user)
query = make_query("SELECT 1 as value", {}, [])