Skip to content

Commit

Permalink
[afs] Percent encode text values when creating/updating fields on
Browse files Browse the repository at this point in the history
ArcGIS feature services

Fixes loss of edits when special characters are used

Fixes #51509
  • Loading branch information
nyalldawson authored and github-actions[bot] committed Jan 31, 2023
1 parent a059f6b commit fca7227
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/core/providers/arcgis/qgsarcgisrestutils.cpp
Expand Up @@ -1576,6 +1576,9 @@ QVariant QgsArcGisRestUtils::variantToAttributeValue( const QVariant &variant, Q

switch ( expectedType )
{
case QVariant::String:
return QString( QUrl::toPercentEncoding( variant.toString() ) );

case QVariant::DateTime:
case QVariant::Date:
{
Expand Down
17 changes: 15 additions & 2 deletions tests/src/python/test_qgsarcgisrestutils.py
Expand Up @@ -306,7 +306,7 @@ def test_feature_to_json(self):
'a_date_field': 1646352000000,
'a_double_field': 5.5,
'a_int_field': 5,
'a_string_field': 'my string value',
'a_string_field': 'my%20string%20value',
'a_null_value': None},
'geometry': {'x': 1.0, 'y': 2.0}})
# without geometry
Expand All @@ -316,7 +316,7 @@ def test_feature_to_json(self):
'a_date_field': 1646352000000,
'a_double_field': 5.5,
'a_int_field': 5,
'a_string_field': 'my string value',
'a_string_field': 'my%20string%20value',
'a_null_value': None}})
# without attributes
context.setObjectIdFieldName('a_int_field')
Expand All @@ -325,6 +325,19 @@ def test_feature_to_json(self):
'a_int_field': 5},
'geometry': {'x': 1.0, 'y': 2.0}})

# with special characters

attributes[0] = 'aaa" , . - ; : ä ö ü è é à ? + &'
test_feature.setAttributes(attributes)
res = QgsArcGisRestUtils.featureToJson(test_feature, context, flags=QgsArcGisRestUtils.FeatureToJsonFlags(QgsArcGisRestUtils.FeatureToJsonFlag.IncludeNonObjectIdAttributes))
self.assertEqual(res, {'attributes': {'a_boolean_field': True,
'a_datetime_field': 1646395994000,
'a_date_field': 1646352000000,
'a_double_field': 5.5,
'a_int_field': 5,
'a_string_field': 'aaa%22%20%2C%20.%20-%20%3B%20%3A%20%C3%A4%20%C3%B6%20%C3%BC%20%C3%A8%20%C3%A9%20%C3%A0%20%3F%20%2B%20%26',
'a_null_value': None}})

def test_field_to_json(self):
field = QgsField('my name', QVariant.LongLong)
field.setAlias('my alias')
Expand Down

0 comments on commit fca7227

Please sign in to comment.