From 6d695161da2a0d4e08dc522b109ccfa4fc1af852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20F=C3=B6rster?= Date: Mon, 29 Jul 2024 17:40:32 +0200 Subject: [PATCH] [MS-432] Fix recursion detection of mutual parents Case: A has parents B and C, and B's parent is also C This is not a recursion but was mistakenly detected as one. --- CHANGELOG.md | 4 ++++ .../entity/DigitalObjectServiceImpl.java | 21 +++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2b200c90..d1de9e08a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ## Unreleased +### Fixed + +- Recursion detection of a mutual parent + ## [9.3.0](https://github.com/dbmdz/metadata-service/releases/tag/9.3.0) - 2024-07-09 ### Added diff --git a/metasvc-server/business/src/main/java/io/github/dbmdz/metadata/server/business/impl/service/identifiable/entity/DigitalObjectServiceImpl.java b/metasvc-server/business/src/main/java/io/github/dbmdz/metadata/server/business/impl/service/identifiable/entity/DigitalObjectServiceImpl.java index 2b87329ed..267194e28 100644 --- a/metasvc-server/business/src/main/java/io/github/dbmdz/metadata/server/business/impl/service/identifiable/entity/DigitalObjectServiceImpl.java +++ b/metasvc-server/business/src/main/java/io/github/dbmdz/metadata/server/business/impl/service/identifiable/entity/DigitalObjectServiceImpl.java @@ -46,6 +46,7 @@ import java.util.function.BiConsumer; import java.util.function.Function; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; @@ -242,22 +243,24 @@ private void preventStackOverflow( throw new IllegalArgumentException( "A function to get the parents must be provided (getParents)!"); if (testObjects == null || testObjects.isEmpty()) return; - List prohibitions = prohibitedChildUuids; + if (prohibitedChildUuids == null) { + prohibitedChildUuids = Collections.emptyList(); + } for (I t : testObjects) { - if (prohibitedChildUuids == null) { - // every "branch" gets a new list - prohibitions = new ArrayList<>(); - } - if (prohibitions.contains(t.getUuid())) + if (prohibitedChildUuids.contains(t.getUuid())) throw new ServiceException( "Endless recursion detected at object %s related with the objects %s" .formatted( t.getUuid(), - prohibitions.stream().map(UUID::toString).collect(Collectors.joining(", ")))); - prohibitions.add(t.getUuid()); + prohibitedChildUuids.stream() + .map(UUID::toString) + .collect(Collectors.joining(", ")))); List parents = getParents.apply(t); if (parents == null || parents.isEmpty()) continue; - preventStackOverflow(parents, prohibitions, getParents); + preventStackOverflow( + parents, + Stream.concat(prohibitedChildUuids.stream(), Stream.of(t.getUuid())).toList(), + getParents); } }