Skip to content

Commit

Permalink
fix: Update Query Context on Explore loading (apache#15865)
Browse files Browse the repository at this point in the history
* create serialize json function

* saving for now

* saving for now

* lint

* cleanup

* fix network request

* update test

* Update tests/integration_tests/charts/api_tests.py

Co-authored-by: Beto Dealmeida <roberto@dealmeida.net>
  • Loading branch information
hughhhh and betodealmeida authored Jul 26, 2021
1 parent 2652f3a commit 762b5bb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import Button from 'src/components/Button';
import { OptionsType } from 'react-select/src/types';
import { AsyncSelect } from 'src/components/Select';
import rison from 'rison';
import { t, SupersetClient } from '@superset-ui/core';
import { t, SupersetClient, QueryFormData } from '@superset-ui/core';
import Chart, { Slice } from 'src/types/Chart';
import { Form, FormItem } from 'src/components/Form';
import { getClientErrorObject } from 'src/utils/getClientErrorObject';
import { buildV1ChartDataPayload } from '../../exploreUtils';

type PropertiesModalProps = {
slice: Slice;
Expand Down Expand Up @@ -81,6 +82,26 @@ export default function PropertiesModal({
label: `${owner.first_name} ${owner.last_name}`,
})),
);

if (chart.query_context === null) {
// set query_context if null
const queryContext = buildV1ChartDataPayload({
formData: slice.form_data as QueryFormData,
force: false,
resultFormat: 'json',
resultType: 'full',
setDataMask: null,
ownState: null,
});

await SupersetClient.put({
endpoint: `/api/v1/chart/${slice.slice_id}`,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query_context: JSON.stringify(queryContext),
}),
});
}
} catch (response) {
const clientError = await getClientErrorObject(response);
showError(clientError);
Expand Down
3 changes: 1 addition & 2 deletions superset/charts/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def ensure_thumbnails_enabled(self) -> Optional[Response]:
"params",
"slice_name",
"viz_type",
"query_context",
]
show_select_columns = show_columns + ["table.id"]
list_columns = [
Expand Down Expand Up @@ -338,15 +339,13 @@ def put(self, pk: int) -> Response:
500:
$ref: '#/components/responses/500'
"""

if not request.is_json:
return self.response_400(message="Request is not JSON")
try:
item = self.edit_model_schema.load(request.json)
# This validates custom Schema with custom validations
except ValidationError as error:
return self.response_400(message=error.messages)

try:
changed_model = UpdateChartCommand(g.user, pk, item).run()
response = self.response(200, id=changed_model.id, result=item)
Expand Down
1 change: 1 addition & 0 deletions tests/integration_tests/charts/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ def test_get_chart(self):
"params": None,
"slice_name": "title",
"viz_type": None,
"query_context": None,
}
data = json.loads(rv.data.decode("utf-8"))
self.assertEqual(data["result"], expected_result)
Expand Down

0 comments on commit 762b5bb

Please sign in to comment.