Skip to content

Commit

Permalink
Fix saving " and \ to ArcGIS REST sources
Browse files Browse the repository at this point in the history
Seems these characters must be escaped before encoding

Fixes qgis#55946
  • Loading branch information
nyalldawson committed Feb 23, 2024
1 parent 2ec7009 commit b550b36
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/core/providers/arcgis/qgsarcgisrestutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,10 @@ QVariant QgsArcGisRestUtils::variantToAttributeValue( const QVariant &variant, Q
switch ( expectedType )
{
case QVariant::String:
return QString( QUrl::toPercentEncoding( variant.toString() ) );
{
const QString escaped = variant.toString().replace( '\\', QStringLiteral( "\\\\" ) ).replace( '"', QStringLiteral( "\\\"" ) );
return QString( QUrl::toPercentEncoding( escaped, "'" ) );
}

case QVariant::DateTime:
case QVariant::Date:
Expand Down
4 changes: 2 additions & 2 deletions tests/src/python/test_qgsarcgisrestutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,15 @@ def test_feature_to_json(self):

# with special characters

attributes[0] = 'aaa" , . - ; : ä ö ü è é à ? + &'
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_string_field': """aaa%5C%22%20'%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%20%5C%5C%20%2F""",
'a_null_value': None}})

def test_field_to_json(self):
Expand Down

0 comments on commit b550b36

Please sign in to comment.