Skip to content

Commit 9f00c5f

Browse files
authored
Merge pull request #632 from geoadmin/fix-PB-2201-collection-patch
Fix PATCH collections
2 parents ff1535d + 0206358 commit 9f00c5f

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

app/stac_api/serializers/collection.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -360,16 +360,20 @@ def create(self, validated_data):
360360
def update(self, instance, validated_data):
361361
"""
362362
Update and return an existing `Collection` instance, given the validated data.
363+
In case of partial update, only update providers and links if they are present
364+
in the payload.
363365
"""
364-
providers_data = validated_data.pop('providers', [])
365-
links_data = validated_data.pop('links', [])
366-
self._update_or_create_providers(collection=instance, providers_data=providers_data)
367-
update_or_create_links(
368-
instance_type="collection",
369-
model=CollectionLink,
370-
instance=instance,
371-
links_data=links_data
372-
)
366+
if not self.partial or validated_data.get('providers') is not None:
367+
providers_data = validated_data.pop('providers', [])
368+
self._update_or_create_providers(collection=instance, providers_data=providers_data)
369+
if not self.partial or validated_data.get('links') is not None:
370+
links_data = validated_data.pop('links', [])
371+
update_or_create_links(
372+
instance_type="collection",
373+
model=CollectionLink,
374+
instance=instance,
375+
links_data=links_data
376+
)
373377
return super().update(instance, validated_data)
374378

375379
def update_or_create(self, look_up, validated_data):

app/tests/tests_10/test_collections_endpoint.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ def test_collection_patch(self):
402402
self.assertNotEqual(self.collection["license"], payload_json['license'])
403403
# for start the payload has no description
404404
self.assertNotIn('title', payload_json.keys())
405+
providers_before_patch = self.collection['providers']
406+
links_before_patch = self.collection['links']
405407

406408
response = self.client.patch(
407409
f"/{STAC_BASE_V}/collections/{collection_name}",
@@ -413,8 +415,13 @@ def test_collection_patch(self):
413415
# licence affected by patch
414416
self.assertEqual(payload_json['license'], response_json['license'])
415417

418+
db_collection = Collection.objects.get(name=collection_name)
416419
# description not affected by patch
417420
self.assertEqual(self.collection["description"], response_json['description'])
421+
# providers not affected by patch
422+
self.assertEqual(db_collection.providers.count(), len(providers_before_patch))
423+
# links not affected by patch
424+
self.assertEqual(db_collection.links.count(), len(links_before_patch))
418425

419426
def test_collection_patch_extra_payload(self):
420427
collection_name = self.collection['name'] # get a name that is registered in the service

0 commit comments

Comments
 (0)