Fixes #3794 Getting duplicate tags from backend (#3834)

This commit is contained in:
Suresh Srinivas 2022-04-04 15:30:55 -07:00 committed by GitHub
parent 7b3e459eb3
commit ee973e561b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 5 deletions

View File

@ -23,6 +23,7 @@ import static org.openmetadata.catalog.util.EntityUtil.entityReferenceMatch;
import static org.openmetadata.catalog.util.EntityUtil.nextMajorVersion; import static org.openmetadata.catalog.util.EntityUtil.nextMajorVersion;
import static org.openmetadata.catalog.util.EntityUtil.nextVersion; import static org.openmetadata.catalog.util.EntityUtil.nextVersion;
import static org.openmetadata.catalog.util.EntityUtil.objectMatch; import static org.openmetadata.catalog.util.EntityUtil.objectMatch;
import static org.openmetadata.catalog.util.EntityUtil.tagLabelMatch;
import static org.openmetadata.common.utils.CommonUtil.listOrEmpty; import static org.openmetadata.common.utils.CommonUtil.listOrEmpty;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
@ -566,11 +567,17 @@ public abstract class EntityRepository<T> {
return tagLabels; return tagLabels;
} }
List<TagLabel> updatedTagLabels = new ArrayList<>(tagLabels); List<TagLabel> updatedTagLabels = new ArrayList<>();
for (TagLabel tagLabel : tagLabels) { for (TagLabel tagLabel : tagLabels) {
TagLabel existingTag =
updatedTagLabels.stream().filter(c -> tagLabelMatch.test(c, tagLabel)).findAny().orElse(null);
if (existingTag != null) {
continue; // tag label is already seen. Don't add duplicate tags.
}
updatedTagLabels.add(tagLabel);
if (tagLabel.getSource() != Source.TAG) { if (tagLabel.getSource() != Source.TAG) {
// Related tags are not supported for Glossary yet continue; // Related tags are not supported for Glossary yet
continue;
} }
String json = daoCollection.tagDAO().findTag(tagLabel.getTagFQN()); String json = daoCollection.tagDAO().findTag(tagLabel.getTagFQN());
if (json == null) { if (json == null) {
@ -997,7 +1004,7 @@ public abstract class EntityRepository<T> {
List<TagLabel> addedTags = new ArrayList<>(); List<TagLabel> addedTags = new ArrayList<>();
List<TagLabel> deletedTags = new ArrayList<>(); List<TagLabel> deletedTags = new ArrayList<>();
recordListChange(fieldName, origTags, updatedTags, addedTags, deletedTags, EntityUtil.tagLabelMatch); recordListChange(fieldName, origTags, updatedTags, addedTags, deletedTags, tagLabelMatch);
updatedTags.sort(compareTagLabel); updatedTags.sort(compareTagLabel);
applyTags(updatedTags, fqn); applyTags(updatedTags, fqn);
} }

View File

@ -1208,8 +1208,12 @@ public abstract class EntityResourceTest<T, K> extends CatalogApplicationTest {
if (supportsTags) { if (supportsTags) {
entityInterface.setTags(new ArrayList<>()); entityInterface.setTags(new ArrayList<>());
entityInterface.getTags().add(USER_ADDRESS_TAG_LABEL); entityInterface.getTags().add(USER_ADDRESS_TAG_LABEL);
entityInterface.getTags().add(USER_ADDRESS_TAG_LABEL); // Add duplicated tags and make sure only one tag is added
entityInterface.getTags().add(GLOSSARY2_TERM1_LABEL); entityInterface.getTags().add(GLOSSARY2_TERM1_LABEL);
change.getFieldsAdded().add(new FieldChange().withName("tags").withNewValue(entityInterface.getTags())); entityInterface.getTags().add(GLOSSARY2_TERM1_LABEL); // Add duplicated tags and make sure only one tag is added
change
.getFieldsAdded()
.add(new FieldChange().withName("tags").withNewValue(List.of(USER_ADDRESS_TAG_LABEL, GLOSSARY2_TERM1_LABEL)));
} }
change change
.getFieldsAdded() .getFieldsAdded()

View File

@ -570,6 +570,7 @@ public class TableResourceTest extends EntityResourceTest<Table, CreateTable> {
// //
List<TagLabel> tags = new ArrayList<>(); List<TagLabel> tags = new ArrayList<>();
tags.add(USER_ADDRESS_TAG_LABEL); tags.add(USER_ADDRESS_TAG_LABEL);
tags.add(USER_ADDRESS_TAG_LABEL); // Duplicated tags should be handled
List<Column> columns = new ArrayList<>(); List<Column> columns = new ArrayList<>();
columns.add(getColumn("c1", BIGINT, null).withTags(tags)); columns.add(getColumn("c1", BIGINT, null).withTags(tags));
@ -588,6 +589,7 @@ public class TableResourceTest extends EntityResourceTest<Table, CreateTable> {
// Ensure description and previous tag is carried forward during update // Ensure description and previous tag is carried forward during update
// //
tags.add(GLOSSARY1_TERM1_LABEL); tags.add(GLOSSARY1_TERM1_LABEL);
tags.add(GLOSSARY1_TERM1_LABEL); // Duplicated tags should be handled
List<Column> updatedColumns = new ArrayList<>(); List<Column> updatedColumns = new ArrayList<>();
updatedColumns.add(getColumn("c1", BIGINT, null).withTags(tags)); updatedColumns.add(getColumn("c1", BIGINT, null).withTags(tags));
ChangeDescription change = getChangeDescription(table.getVersion()); ChangeDescription change = getChangeDescription(table.getVersion());