Skip to content
Permalink
Browse files

[memory] Fix provider cannot handle fields with complex characters in…

… name

Fixes #18885
  • Loading branch information
nyalldawson committed May 2, 2018
1 parent f4d38d4 commit 9809f9394b3b0e1d8392add4cc2a7c380dd33eca
@@ -108,7 +108,7 @@ QgsMemoryProvider::QgsMemoryProvider( const QString &uri )
QStringList fields = url.allQueryItemValues( QStringLiteral( "field" ) );
for ( int i = 0; i < fields.size(); i++ )
{
QString name = fields.at( i );
QString name = QUrl::fromPercentEncoding( fields.at( i ).toUtf8() );
QVariant::Type type = QVariant::String;
QVariant::Type subType = QVariant::Invalid;
QString typeName( QStringLiteral( "string" ) );
@@ -72,7 +72,7 @@ QgsVectorLayer *QgsMemoryProviderUtils::createMemoryLayer( const QString &name,
{
lengthPrecision = QStringLiteral( "(%1)" ).arg( field.length() );
}
parts << QStringLiteral( "field=%1:%2%3" ).arg( field.name(), memoryLayerFieldType( field.type() ), lengthPrecision );
parts << QStringLiteral( "field=%1:%2%3" ).arg( QString( QUrl::toPercentEncoding( field.name() ) ), memoryLayerFieldType( field.type() ), lengthPrecision );
}

QString uri = geomType + '?' + parts.join( '&' );
@@ -275,6 +275,16 @@ def testLengthPrecisionFromUri(self):
self.assertEqual(myMemoryLayer.fields().field('size').length(), 12)
self.assertEqual(myMemoryLayer.fields().field('size').precision(), 9)

def testFromUriWithEncodedField(self):
"""Test we can construct the mem provider from a uri when a field name is encoded"""
layer = QgsVectorLayer(
('Point?crs=epsg:4326&field=name:string(20)&'
'field=test%2Ffield:integer'),
'test',
'memory')
self.assertTrue(layer.isValid())
self.assertEqual([f.name() for f in layer.fields()], ['name', 'test/field'])

def testSaveFields(self):
# Create a new memory layer with no fields
myMemoryLayer = QgsVectorLayer(
@@ -398,6 +408,8 @@ def testCreateMemoryLayer(self):
fields.append(QgsField("date", QVariant.Date))
fields.append(QgsField("datetime", QVariant.DateTime))
fields.append(QgsField("time", QVariant.Time))
fields.append(QgsField("#complex_name", QVariant.String))
fields.append(QgsField("complex/name", QVariant.String))
layer = QgsMemoryProviderUtils.createMemoryLayer('my name', fields)
self.assertTrue(layer.isValid())
self.assertFalse(layer.fields().isEmpty())

0 comments on commit 9809f93

Please sign in to comment.
You can’t perform that action at this time.