Skip to content

Commit

Permalink
[OAPIF provider] addFeatures(): use the id returned by the server to …
Browse files Browse the repository at this point in the history
…fill the QGIS 'id' field, when it exists

Fixes #57486
  • Loading branch information
rouault authored and nyalldawson committed May 23, 2024
1 parent eaa0261 commit b2bfa91
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/providers/wfs/oapif/qgsoapifprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ bool QgsOapifProvider::addFeatures( QgsFeatureList &flist, Flags flags )
contentCrs = mShared->mSourceCrs.toOgcUri();
}
const bool hasAxisInverted = mShared->mSourceCrs.hasAxisInverted();
const int idFieldIdx = mShared->mFields.indexOf( "id" );
for ( QgsFeature &f : flist )
{
QgsOapifCreateFeatureRequest req( uri );
Expand All @@ -579,6 +580,15 @@ bool QgsOapifProvider::addFeatures( QgsFeatureList &flist, Flags flags )
return false;
}
jsonIds.append( id );

// If there's no feature["properties"]["id"] field in the JSON returned by the
// /items request, but there's a "id" field, it means that feature["id"]
// is non-numeric. Thus set the one returned by the createFeature() request
if ( !( flags & QgsFeatureSink::FastInsert ) &&
!mShared->mFoundIdInProperties && idFieldIdx >= 0 )
{
f.setAttribute( idFieldIdx, id );
}
}

QStringList::const_iterator idIt = jsonIds.constBegin();
Expand Down
2 changes: 2 additions & 0 deletions tests/src/python/test_provider_oapif.py
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,8 @@ def testFeatureInsertionDeletion(self):
self.assertTrue(ret)
self.assertEqual(fl[0].id(), 1)
self.assertEqual(fl[1].id(), 2)
self.assertEqual(fl[0]["id"], "new_id")
self.assertEqual(fl[1]["id"], "other_id")

# Failed attempt
self.assertFalse(vl.dataProvider().deleteFeatures([1]))
Expand Down

0 comments on commit b2bfa91

Please sign in to comment.