From f769251b0fb949a37952eb6a805664e8001afcbc Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Tue, 3 Aug 2021 14:18:24 -0600 Subject: [PATCH] [maps] fix More than 2 maps embeddables with geo-shape layers results in empty layers for 3+ (#107442) * [maps] fix More than 2 maps embeddables with geo-shape layers results in empty layers for 3+ * comment * add unit test * eslint --- .../elasticsearch_geo_utils.test.js | 20 +++++++++++++++++++ .../elasticsearch_geo_utils.ts | 4 +++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/maps/common/elasticsearch_util/elasticsearch_geo_utils.test.js b/x-pack/plugins/maps/common/elasticsearch_util/elasticsearch_geo_utils.test.js index e6b27e78fd36b..d4fcab060b01c 100644 --- a/x-pack/plugins/maps/common/elasticsearch_util/elasticsearch_geo_utils.test.js +++ b/x-pack/plugins/maps/common/elasticsearch_util/elasticsearch_geo_utils.test.js @@ -269,6 +269,26 @@ describe('hitsToGeoJson', () => { type: 'Point', }); }); + + it('Should not modify results of flattenHit', () => { + const geoFieldName = 'location'; + const cachedProperities = { + [geoFieldName]: '20,100', + }; + const cachedFlattenHit = () => { + return cachedProperities; + }; + const hits = [ + { + _source: { + [geoFieldName]: '20,100', + }, + }, + ]; + const geojson = hitsToGeoJson(hits, cachedFlattenHit, geoFieldName, 'geo_point', []); + expect(cachedProperities.hasOwnProperty('location')).toBe(true); + expect(geojson.features[0].properties).toEqual({}); + }); }); }); diff --git a/x-pack/plugins/maps/common/elasticsearch_util/elasticsearch_geo_utils.ts b/x-pack/plugins/maps/common/elasticsearch_util/elasticsearch_geo_utils.ts index edadc20a595ce..94f3cbb0fed28 100644 --- a/x-pack/plugins/maps/common/elasticsearch_util/elasticsearch_geo_utils.ts +++ b/x-pack/plugins/maps/common/elasticsearch_util/elasticsearch_geo_utils.ts @@ -80,7 +80,9 @@ export function hitsToGeoJson( const tmpGeometriesAccumulator: Geometry[] = []; for (let i = 0; i < hits.length; i++) { - const properties = flattenHit(hits[i]); + // flattenHit returns value from cache. Create new object to avoid modifying flattenHit cache. + // not doing deep copy because copying coordinates can be very expensive for complex geometries. + const properties = { ...flattenHit(hits[i]) }; tmpGeometriesAccumulator.length = 0; // truncate accumulator