From cc0ae0a71468dd4c9bdc0650c94f04d7c6e98736 Mon Sep 17 00:00:00 2001 From: Kyle Hensel Date: Tue, 23 Nov 2021 20:22:15 +1300 Subject: [PATCH 1/4] fix bug with numeric properties in custom map data --- modules/ui/sections/raw_tag_editor.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/modules/ui/sections/raw_tag_editor.js b/modules/ui/sections/raw_tag_editor.js index 055b58672..2d39703ab 100644 --- a/modules/ui/sections/raw_tag_editor.js +++ b/modules/ui/sections/raw_tag_editor.js @@ -11,6 +11,21 @@ import { t } from '../../core/localizer'; import { utilArrayDifference, utilArrayIdentical } from '../../util/array'; import { utilGetSetValue, utilNoAuto, utilRebind, utilTagDiff } from '../../util'; +/** + * This component is also used for custom map data, + * and geojson can contain numbers as values. + * We convert numbers to strings to avoid unexpected bugs. + * @param {{ [key: string]: any }} tags + */ +function stringifyNumbers(tags) { + for (const key in tags) { + if (typeof tags[key] === 'number') { + tags[key] = tags[key].toString(); + } + } + return tags; +} + export function uiSectionRawTagEditor(id, context) { var section = uiSection(id, context) @@ -594,7 +609,7 @@ export function uiSectionRawTagEditor(id, context) { section.tags = function(val) { if (!arguments.length) return _tags; - _tags = val; + _tags = stringifyNumbers(val); return section; }; From 3373f1b53de443d5ff1b4ebe3348914a17287e71 Mon Sep 17 00:00:00 2001 From: Martin Raifer Date: Tue, 23 Nov 2021 12:53:10 +0100 Subject: [PATCH 2/4] enhance and move geojson property sanitation to data layer module --- modules/svg/data.js | 20 ++++++++++++++++++++ modules/ui/sections/raw_tag_editor.js | 16 +--------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/modules/svg/data.js b/modules/svg/data.js index 3ddea8c9e..a88e4b0ff 100644 --- a/modules/svg/data.js +++ b/modules/svg/data.js @@ -315,6 +315,21 @@ export function svgData(projection, context, dispatch) { } + function stringifyGeojsonProperties(feature) { + const properties = feature.properties; + for (const key in properties) { + const property = properties[key]; + if (typeof property === 'number' || typeof property === 'boolean' || Array.isArray(property)) { + properties[key] = property.toString(); + } else if (property === null) { + properties[key] = 'null'; + } else if (typeof property === 'object') { + properties[key] = JSON.stringify(property); + } + } + } + + drawData.setFile = function(extension, data) { _template = null; _fileList = null; @@ -332,6 +347,11 @@ export function svgData(projection, context, dispatch) { case '.geojson': case '.json': gj = JSON.parse(data); + if (gj.type === 'FeatureCollection') { + gj.features.forEach(stringifyGeojsonProperties); + } else if (gj.type === 'Feature') { + stringifyGeojsonProperties(gj); + } break; } diff --git a/modules/ui/sections/raw_tag_editor.js b/modules/ui/sections/raw_tag_editor.js index 2d39703ab..6f552317b 100644 --- a/modules/ui/sections/raw_tag_editor.js +++ b/modules/ui/sections/raw_tag_editor.js @@ -11,20 +11,6 @@ import { t } from '../../core/localizer'; import { utilArrayDifference, utilArrayIdentical } from '../../util/array'; import { utilGetSetValue, utilNoAuto, utilRebind, utilTagDiff } from '../../util'; -/** - * This component is also used for custom map data, - * and geojson can contain numbers as values. - * We convert numbers to strings to avoid unexpected bugs. - * @param {{ [key: string]: any }} tags - */ -function stringifyNumbers(tags) { - for (const key in tags) { - if (typeof tags[key] === 'number') { - tags[key] = tags[key].toString(); - } - } - return tags; -} export function uiSectionRawTagEditor(id, context) { @@ -609,7 +595,7 @@ export function uiSectionRawTagEditor(id, context) { section.tags = function(val) { if (!arguments.length) return _tags; - _tags = stringifyNumbers(val); + _tags = val; return section; }; From 6e6becb4e49f46ac7004a051f5f7992b9ddbc1dd Mon Sep 17 00:00:00 2001 From: Martin Raifer Date: Tue, 23 Nov 2021 13:35:05 +0100 Subject: [PATCH 3/4] add test for geojson sanitation of non-string properties --- test/spec/svg/data.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/spec/svg/data.js b/test/spec/svg/data.js index d064e1a77..6c2b70b2c 100644 --- a/test/spec/svg/data.js +++ b/test/spec/svg/data.js @@ -22,7 +22,11 @@ describe('iD.svgData', function () { ' "area": 19717.8,' + ' "name": "New Jersey",' + ' "name_en": "New Jersey",' + - ' "osm_id": 316973311' + + ' "osm_id": 316973311,' + + ' "flag": true,' + + ' "list": [1,2,3],' + + ' "null": null,' + + ' "object": {}' + ' },' + ' "id": 316973311' + ' }' + @@ -171,6 +175,11 @@ describe('iD.svgData', function () { path = surface.selectAll('path.stroke'); expect(path.nodes().length).to.eql(1); expect(path.attr('d')).to.match(/^M.*z$/); + expect(render.geojson().features[0].properties.osm_id).to.be.a('string'); + expect(render.geojson().features[0].properties.flag).to.be.a('string'); + expect(render.geojson().features[0].properties.list).to.be.a('string'); + expect(render.geojson().features[0].properties.null).to.be.a('string'); + expect(render.geojson().features[0].properties.object).to.be.a('string'); done(); }, 200); }); From 8ef66e7cfbcda3517275d02ca9617d541bb54a62 Mon Sep 17 00:00:00 2001 From: Martin Raifer Date: Tue, 23 Nov 2021 13:38:05 +0100 Subject: [PATCH 4/4] add to changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6b764ecc..7f25beaef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ _Breaking developer changes, which may affect downstream projects or sites that #### :bug: Bugfixes * Fix hidden tooltips on map control toolbar ([#8781]) * Fix glitching out turn restriction minimap on narrow sidebars ([#8792]) +* Fix non-string properties of GeoJSON custom map data not being displayed correctly ([#8825], thanks [@k-yle]) #### :earth_asia: Localization #### :hourglass: Performance #### :mortar_board: Walkthrough / Help @@ -61,6 +62,8 @@ _Breaking developer changes, which may affect downstream projects or sites that * Add colours for preset categories ([#8799]) #### :hammer: Development +[@k-yle]: https://github.com/k-yle + [#8771]: https://github.com/openstreetmap/iD/issues/8771 [#8781]: https://github.com/openstreetmap/iD/issues/8781 [#8792]: https://github.com/openstreetmap/iD/pull/8792 @@ -69,6 +72,7 @@ _Breaking developer changes, which may affect downstream projects or sites that [#8800]: https://github.com/openstreetmap/iD/pull/8800 [#8805]: https://github.com/openstreetmap/iD/issues/8805 [#8807]: https://github.com/openstreetmap/iD/issues/8807 +[#8825]: https://github.com/openstreetmap/iD/pull/8825 # 2.20.2 ##### 2021-Oct-28