diff --git a/superset-frontend/src/explore/components/PropertiesModal/index.tsx b/superset-frontend/src/explore/components/PropertiesModal/index.tsx index 0c46f6ba79..b1201e7b34 100644 --- a/superset-frontend/src/explore/components/PropertiesModal/index.tsx +++ b/superset-frontend/src/explore/components/PropertiesModal/index.tsx @@ -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; @@ -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); diff --git a/superset/charts/api.py b/superset/charts/api.py index 98dd509acc..3b3227afe5 100644 --- a/superset/charts/api.py +++ b/superset/charts/api.py @@ -130,6 +130,7 @@ class ChartRestApi(BaseSupersetModelRestApi): "params", "slice_name", "viz_type", + "query_context", ] show_select_columns = show_columns + ["table.id"] list_columns = [ @@ -338,7 +339,6 @@ class ChartRestApi(BaseSupersetModelRestApi): 500: $ref: '#/components/responses/500' """ - if not request.is_json: return self.response_400(message="Request is not JSON") try: @@ -346,7 +346,6 @@ class ChartRestApi(BaseSupersetModelRestApi): # 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) diff --git a/tests/integration_tests/charts/api_tests.py b/tests/integration_tests/charts/api_tests.py index 74d0fa200e..e74f50d881 100644 --- a/tests/integration_tests/charts/api_tests.py +++ b/tests/integration_tests/charts/api_tests.py @@ -722,6 +722,7 @@ class TestChartApi(SupersetTestCase, ApiOwnersTestCaseMixin, InsertChartMixin): "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)