Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions api/src/org/labkey/api/exp/api/ExperimentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,23 @@ static void validateParentAlias(Map<String, String> aliasMap, Set<String> reserv
throw new IllegalArgumentException(String.format("Parent alias header is reserved: %1$s", trimmedKey));
}

if (updatedDomainDesign != null && !existingAliases.contains(trimmedKey) && updatedDomainDesign.getFieldByName(trimmedKey) != null)
if (updatedDomainDesign != null)
{
throw new IllegalArgumentException(String.format("An existing " + dataTypeNoun + " property conflicts with parent alias header: %1$s", trimmedKey));
if (!existingAliases.contains(trimmedKey))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cnathe or @XingY. Do you know if this check is still valid? What is the use case where we want to allow retention of an existing alias that conflicts with a field name? Was this to protect against cases where we add (system) domain fields that conflict with already-existing import aliases? Still seems problematic to produce conflicts that we won't be able to resolve well during import.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check was added in #894. I guess the intention was to not break existing designer update that was already problematic. I agree in this case we should opt for rejecting ambiguity, even if the designer was not actually changed.

{
var field = updatedDomainDesign.getFieldByName(trimmedKey);
if (field != null)
{
throw new IllegalArgumentException(String.format("An existing %1$s property conflicts with parent alias header: %2$s", dataTypeNoun, trimmedKey));
}
}
// GH Issue 1257: If there are conflicts with import aliases, this should be an error since it produces ambiguity during import
var field = updatedDomainDesign.getFieldByImportAlias(trimmedKey);
if (field != null)
{
throw new IllegalArgumentException(String.format("Field %1$s has an import alias %2$s that conflicts with a parent alias header.", field.getName(), trimmedKey));
}

}

if (!dupes.add(trimmedKey))
Expand Down
32 changes: 32 additions & 0 deletions api/src/org/labkey/api/exp/property/DomainUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.labkey.api.assay.AbstractAssayProvider;
import org.labkey.api.collections.CaseInsensitiveHashMap;
import org.labkey.api.collections.CaseInsensitiveHashSet;
import org.labkey.api.collections.CaseInsensitiveLinkedHashMap;
import org.labkey.api.collections.IntHashMap;
import org.labkey.api.collections.LongHashMap;
import org.labkey.api.data.ColumnInfo;
Expand Down Expand Up @@ -1563,6 +1564,9 @@ public static ValidationException validateProperties(@Nullable Domain domain, @N
Set<String> reservedPrefixes = (null != domain && null != domainKind) ? domainKind.getReservedPropertyNamePrefixes() : updates.getReservedFieldNamePrefixes();
Map<String, Integer> namePropertyIdMap = new CaseInsensitiveHashMap<>();
Map<String, String> altNameMap = new CaseInsensitiveHashMap<>();
// GH Issue 1257: import alias -> the fields declaring it.
Map<String, List<GWTPropertyDescriptor>> importAliasMap = new CaseInsensitiveLinkedHashMap<>();
Set<String> fieldNames = new CaseInsensitiveHashSet();
ValidationException exception = new ValidationException();
Map<Integer, String> propertyIdNameMap = getOriginalFieldPropertyIdNameMap(orig);//key: orig property id, value : orig field name

Expand All @@ -1577,6 +1581,15 @@ public static ValidationException validateProperties(@Nullable Domain domain, @N
continue;
}

fieldNames.add(name);

// GH Issue 1257: Collect alias to field list mapping
for (String alias : new CaseInsensitiveHashSet(ColumnRenderPropertiesImpl.convertToSet(field.getImportAliases())))
{
if (!alias.equalsIgnoreCase(name)) // an alias that repeats the field's name is redundant but not ambiguous, so skip it
importAliasMap.computeIfAbsent(alias, k -> new ArrayList<>()).add(field);
}

if (ILLEGAL_PROPERTY_NAMES.contains(name.trim()))
{
exception.addError(new SimpleValidationError(getDomainErrorMessage(updates, "The field name '" + name + "' is not allowed.")));
Expand Down Expand Up @@ -1674,9 +1687,28 @@ public static ValidationException validateProperties(@Nullable Domain domain, @N
}
}

// GH Issue 1257: import aliases must be unique across the domain and must not collide with a field name, since
// ImportAliasable.Helper.createImportMap() resolves names, labels, and aliases into one case-insensitive map.
importAliasMap.forEach((alias, fields) -> {
String names = fields.stream().map(GWTPropertyDescriptor::getName).sorted().collect(Collectors.joining(", "));

if (fields.size() > 1)
addImportAliasErrors(exception, updates, fields, "Duplicate import alias " + alias + " for fields " + names + ".");

if (fieldNames.contains(alias))
addImportAliasErrors(exception, updates, fields, "Import alias " + alias + " on field" + (fields.size() == 1 ? " " : "s ") + names + " conflicts with a field name.");
});

return exception;
}

/** Anchor an import alias error on every field that declares the offending alias so the designer can highlight them. */
private static void addImportAliasErrors(ValidationException exception, GWTDomain<?> updates, List<GWTPropertyDescriptor> fields, String message)
{
for (GWTPropertyDescriptor field : fields)
exception.addError(new PropertyValidationError(getDomainErrorMessage(updates, message), field.getName(), field.getPropertyId()));
}

@Nullable
private static Map<Integer, String> getOriginalFieldPropertyIdNameMap(@Nullable GWTDomain<?> orig)
{
Expand Down
13 changes: 13 additions & 0 deletions api/src/org/labkey/api/gwt/client/model/GWTDomain.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import org.labkey.api.collections.CaseInsensitiveHashSet;
import org.labkey.api.data.ColumnRenderPropertiesImpl;
import org.labkey.api.gwt.client.DefaultValueType;

import java.util.ArrayList;
Expand Down Expand Up @@ -197,6 +199,17 @@ public FieldType getFieldByName(String name)
return null;
}

public FieldType getFieldByImportAlias(String alias)
{
for (FieldType field : getFields(true))
{
Set<String> importAliases = ColumnRenderPropertiesImpl.convertToSet(field.getImportAliases());
if (new CaseInsensitiveHashSet(importAliases).contains(alias))
return field;
}
return null;
}

/**
* @return Indicates that the property can't be removed from the domain. The property may or may not be nullable.
*/
Expand Down