From bed10a0e2bf8f29e2568cdbe1af0839890dfa58d Mon Sep 17 00:00:00 2001 From: Diego Medina Date: Fri, 27 Jan 2023 13:50:19 -0300 Subject: [PATCH] chore: deprecate /superset/slice_json/ and /superset/annotation_json/ (#22496) --- .../src/components/Chart/chartAction.js | 36 +++++++------ .../controls/AnnotationLayerControl/index.jsx | 2 +- .../exploreUtils/getAnnotationJsonUrl.test.ts | 51 ------------------- .../src/explore/exploreUtils/index.js | 10 ++-- superset/common/query_context_processor.py | 1 + superset/views/core.py | 2 + 6 files changed, 29 insertions(+), 73 deletions(-) delete mode 100644 superset-frontend/src/explore/exploreUtils/getAnnotationJsonUrl.test.ts diff --git a/superset-frontend/src/components/Chart/chartAction.js b/superset-frontend/src/components/Chart/chartAction.js index f59c2da7199f2..7418a8f8b72c9 100644 --- a/superset-frontend/src/components/Chart/chartAction.js +++ b/superset-frontend/src/components/Chart/chartAction.js @@ -30,10 +30,7 @@ import { shouldUseLegacyApi, getChartDataUri, } from 'src/explore/exploreUtils'; -import { - requiresQuery, - ANNOTATION_SOURCE_TYPES, -} from 'src/modules/AnnotationTypes'; +import { requiresQuery } from 'src/modules/AnnotationTypes'; import { addDangerToast } from 'src/components/MessageToasts/actions'; import { logEvent } from 'src/logger/actions'; @@ -290,26 +287,35 @@ export function runAnnotationQuery({ : undefined; } - const isNative = annotation.sourceType === ANNOTATION_SOURCE_TYPES.NATIVE; - const url = getAnnotationJsonUrl( - annotation.value, - sliceFormData, - isNative, - force, - ); + const url = getAnnotationJsonUrl(annotation.value, force); const controller = new AbortController(); const { signal } = controller; dispatch(annotationQueryStarted(annotation, controller, sliceKey)); - return SupersetClient.get({ + const annotationIndex = fd?.annotation_layers?.findIndex( + it => it.name === annotation.name, + ); + if (annotationIndex >= 0) { + fd.annotation_layers[annotationIndex].overrides = sliceFormData; + } + + return SupersetClient.post({ url, signal, timeout: timeout * 1000, + headers: { 'Content-Type': 'application/json' }, + jsonPayload: buildV1ChartDataPayload({ + formData: fd, + force, + resultFormat: 'json', + resultType: 'full', + }), }) - .then(({ json }) => - dispatch(annotationQuerySuccess(annotation, json, sliceKey)), - ) + .then(({ json }) => { + const data = json?.result?.[0]?.annotation_data?.[annotation.name]; + return dispatch(annotationQuerySuccess(annotation, { data }, sliceKey)); + }) .catch(response => getClientErrorObject(response).then(err => { if (err.statusText === 'timeout') { diff --git a/superset-frontend/src/explore/components/controls/AnnotationLayerControl/index.jsx b/superset-frontend/src/explore/components/controls/AnnotationLayerControl/index.jsx index db3bbca272319..f70557170c0d8 100644 --- a/superset-frontend/src/explore/components/controls/AnnotationLayerControl/index.jsx +++ b/superset-frontend/src/explore/components/controls/AnnotationLayerControl/index.jsx @@ -71,7 +71,7 @@ class AnnotationLayerControl extends React.PureComponent { } componentDidMount() { - // preload the AnotationLayer component and dependent libraries i.e. mathjs + // preload the AnnotationLayer component and dependent libraries i.e. mathjs AnnotationLayer.preload(); } diff --git a/superset-frontend/src/explore/exploreUtils/getAnnotationJsonUrl.test.ts b/superset-frontend/src/explore/exploreUtils/getAnnotationJsonUrl.test.ts deleted file mode 100644 index 388c3b9a03093..0000000000000 --- a/superset-frontend/src/explore/exploreUtils/getAnnotationJsonUrl.test.ts +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -import { getAnnotationJsonUrl } from '.'; - -let windowLocation: any; - -beforeAll(() => { - windowLocation = window.location; - // @ts-expect-error - delete window.location; -}); - -beforeEach(() => { - window.location = { - search: '?testA=0&testB=1', - } as any; -}); - -afterAll(() => { - window.location = windowLocation; -}); - -test('get correct annotation when isNative:true', () => { - const response = getAnnotationJsonUrl('slice_id', 'form_data', true); - expect(response).toBe( - '/superset/annotation_json/slice_id?form_data=%22form_data%22', - ); -}); - -test('get correct annotation when isNative:false', () => { - const response = getAnnotationJsonUrl('slice_id', { json: 'my-data' }, false); - expect(response).toBe( - '/superset/slice_json/slice_id?form_data=%7B%22json%22%3A%22my-data%22%7D', - ); -}); diff --git a/superset-frontend/src/explore/exploreUtils/index.js b/superset-frontend/src/explore/exploreUtils/index.js index 0e6a79ab91c9d..1d678427ff3cf 100644 --- a/superset-frontend/src/explore/exploreUtils/index.js +++ b/superset-frontend/src/explore/exploreUtils/index.js @@ -61,18 +61,16 @@ export function getHostName(allowDomainSharding = false) { return availableDomains[currentIndex]; } -export function getAnnotationJsonUrl(slice_id, form_data, isNative, force) { +export function getAnnotationJsonUrl(slice_id, force) { if (slice_id === null || slice_id === undefined) { return null; } + const uri = URI(window.location.search); - const endpoint = isNative ? 'annotation_json' : 'slice_json'; return uri - .pathname(`/superset/${endpoint}/${slice_id}`) + .pathname('/api/v1/chart/data') .search({ - form_data: safeStringify(form_data, (key, value) => - value === null ? undefined : value, - ), + form_data: safeStringify({ slice_id }), force, }) .toString(); diff --git a/superset/common/query_context_processor.py b/superset/common/query_context_processor.py index 17cc5fd4119d9..e6fa964e4d7bd 100644 --- a/superset/common/query_context_processor.py +++ b/superset/common/query_context_processor.py @@ -576,6 +576,7 @@ def get_viz_annotation_data( if not chart.datasource: raise QueryObjectValidationError(_("The chart datasource does not exist")) form_data = chart.form_data.copy() + form_data.update(annotation_layer.get("overrides", {})) try: viz_obj = get_viz( datasource_type=chart.datasource.type, diff --git a/superset/views/core.py b/superset/views/core.py index d65023d600ba0..9733ea2910a09 100755 --- a/superset/views/core.py +++ b/superset/views/core.py @@ -505,6 +505,7 @@ def generate_json( @expose("/slice_json/") @etag_cache() @check_resource_permissions(check_slice_perms) + @deprecated() def slice_json(self, slice_id: int) -> FlaskResponse: form_data, slc = get_form_data(slice_id, use_slice_data=True) if not slc: @@ -528,6 +529,7 @@ def slice_json(self, slice_id: int) -> FlaskResponse: @has_access_api @event_logger.log_this @expose("/annotation_json/") + @deprecated() def annotation_json( # pylint: disable=no-self-use self, layer_id: int ) -> FlaskResponse: