From e8fdb20b98b88ec330ef41ee5b7454e68bad5f25 Mon Sep 17 00:00:00 2001 From: Nassim Tabchiche Date: Thu, 12 Dec 2024 15:36:43 +0100 Subject: [PATCH] Check that parent assets do not cause a cycle in the assets graph --- backend/core/serializers.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/backend/core/serializers.py b/backend/core/serializers.py index 0e3c95ae7..c741ef570 100644 --- a/backend/core/serializers.py +++ b/backend/core/serializers.py @@ -220,6 +220,20 @@ class Meta: model = Asset fields = "__all__" + def validate_parent_assets(self, parent_assets): + """ + Check that the assets graph will not contain cycles + """ + if not self.instance: + return parent_assets + if parent_assets: + for asset in parent_assets: + if self.instance in asset.ancestors_plus_self(): + raise serializers.ValidationError( + "errorAssetGraphMustNotContainCycles" + ) + return parent_assets + class AssetReadSerializer(AssetWriteSerializer): folder = FieldsRelatedField()