Skip to content

Commit 3845d79

Browse files
authoredMar 14, 2019
Merge pull request #9517 from qgis/backport-9417-on-release-3_6
Backport #9417 on release-3_6
2 parents 568eeb4 + 3011f22 commit 3845d79

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed
 

‎python/core/auto_generated/qgsvectorlayerutils.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ If the ``feature`` does not have a valid fields container set, then the feature'
231231
are simply truncated to match the number of fields present in ``fields`` (or if
232232
less attributes are present in ``feature`` than in ``fields``, the feature's attributes
233233
are padded with NULL values to match the required length).
234+
Finally, the feature's fields are set to ``fields``.
234235

235236
.. versionadded:: 3.4
236237
%End

‎src/core/qgsvectorlayerutils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ void QgsVectorLayerUtils::matchAttributesToFields( QgsFeature &feature, const Qg
673673
feature.setAttributes( attributes );
674674
}
675675
}
676+
feature.setFields( fields );
676677
}
677678

678679
QgsFeatureList QgsVectorLayerUtils::makeFeatureCompatible( const QgsFeature &feature, const QgsVectorLayer *layer )

‎src/core/qgsvectorlayerutils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ class CORE_EXPORT QgsVectorLayerUtils
237237
* are simply truncated to match the number of fields present in \a fields (or if
238238
* less attributes are present in \a feature than in \a fields, the feature's attributes
239239
* are padded with NULL values to match the required length).
240+
* Finally, the feature's fields are set to \a fields.
240241
*
241242
* \since QGIS 3.4
242243
*/

‎tests/src/python/test_qgsprocessinginplace.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,26 @@ def test_make_features_compatible_attributes(self):
412412
self.assertEqual(new_features[0].attributes()[0], 1)
413413
self.assertEqual(new_features[0].attributes()[1], 'foo')
414414

415+
def test_make_features_compatible_different_field_length(self):
416+
"""Test regression #21497"""
417+
fields = QgsFields()
418+
fields.append(QgsField('int_f1', QVariant.Int))
419+
f1 = QgsFeature(fields)
420+
f1.setAttributes([12345])
421+
f1.setGeometry(QgsGeometry.fromWkt('Point(9 45)'))
422+
423+
fields = QgsFields()
424+
fields.append(QgsField('int_f2', QVariant.Int))
425+
fields.append(QgsField('int_f1', QVariant.Int))
426+
vl2 = QgsMemoryProviderUtils.createMemoryLayer(
427+
'mymultiplayer', fields, QgsWkbTypes.Point, QgsCoordinateReferenceSystem(4326))
428+
new_features = QgsVectorLayerUtils.makeFeaturesCompatible([f1], vl2)
429+
self.assertEqual(new_features[0].attributes(), [None, 12345])
430+
431+
f1.setGeometry(QgsGeometry.fromWkt('MultiPoint((9 45))'))
432+
new_features = QgsVectorLayerUtils.makeFeaturesCompatible([f1], vl2)
433+
self.assertEqual(new_features[0].attributes(), [None, 12345])
434+
415435
def test_make_features_compatible_geometry(self):
416436
"""Test corner cases for geometries"""
417437

‎tests/src/python/test_qgsvectorlayerutils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,10 @@ def test_make_features_compatible_attributes(self):
552552
f1.setAttributes([1, 'foo', 'blah'])
553553
QgsVectorLayerUtils.matchAttributesToFields(f1, fields)
554554
self.assertEqual(len(f1.attributes()), 4)
555-
self.assertEqual(f1.attributes()[0], 'foo')
556-
self.assertEqual(f1.attributes()[1], 1)
557-
self.assertEqual(f1.attributes()[2], QVariant())
558-
self.assertEqual(f1.attributes()[3], 'blah')
555+
self.assertEqual(f1.attributes()[0], 1)
556+
self.assertEqual(f1.attributes()[1], 'foo')
557+
self.assertEqual(f1.attributes()[2], 'blah')
558+
self.assertEqual(f1.attributes()[3], QVariant())
559559

560560
# case insensitive
561561
fields2.append(QgsField('extra3', QVariant.String))

0 commit comments

Comments
 (0)
Please sign in to comment.