diff --git a/api/src/org/labkey/api/data/NameGenerator.java b/api/src/org/labkey/api/data/NameGenerator.java index 517d7f3574e..dc2801d40bd 100644 --- a/api/src/org/labkey/api/data/NameGenerator.java +++ b/api/src/org/labkey/api/data/NameGenerator.java @@ -76,6 +76,7 @@ import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; import java.util.stream.Stream; import static org.labkey.api.exp.api.ExpMaterial.ALIQUOTED_FROM_INPUT; @@ -729,7 +730,7 @@ protected int write() .filter(s -> !s.isEmpty()); } - public static Stream parentNames(Object value, String parentColName, TSVWriter tsvWriter, @Nullable BatchValidationException errors) + public static @Nullable Stream parentNames(Object value, String parentColName, TSVWriter tsvWriter, @Nullable BatchValidationException errors) { if (value == null) return Stream.empty(); @@ -783,6 +784,21 @@ else if (value instanceof JSONArray jsonArray) throw new IllegalStateException("For parent values in naming pattern, expected string or collection for '" + parentColName + "': " + value); } + if (values != null) + { + List valueList = values.toList(); + Set valueSet = new HashSet<>(); + Set duplicates = valueList.stream().filter(s -> !valueSet.add(s)).collect(Collectors.toSet()); + if (!duplicates.isEmpty()) + { + if (errors != null) + errors.addRowError(new ValidationException("Duplicate parent names found: " + StringUtils.join(duplicates, ", "), parentColName)); + else + throw new IllegalStateException("Duplicate parent names found: " + StringUtils.join(duplicates, ", ")); + } + return valueList.stream(); + } + return values; }