Fixes #9783 Custom attributes descriptions are not updated (#9828)

This commit is contained in:
Suresh Srinivas 2023-01-20 12:16:11 -08:00 committed by GitHub
parent 16a1b2c8be
commit ca5e35eda2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 48 deletions

View File

@ -33,6 +33,7 @@ import org.apache.commons.lang3.tuple.Triple;
import org.openmetadata.schema.entity.Type; import org.openmetadata.schema.entity.Type;
import org.openmetadata.schema.entity.type.Category; import org.openmetadata.schema.entity.type.Category;
import org.openmetadata.schema.entity.type.CustomProperty; import org.openmetadata.schema.entity.type.CustomProperty;
import org.openmetadata.schema.type.EntityReference;
import org.openmetadata.schema.type.Include; import org.openmetadata.schema.type.Include;
import org.openmetadata.schema.type.Relationship; import org.openmetadata.schema.type.Relationship;
import org.openmetadata.service.Entity; import org.openmetadata.service.Entity;
@ -123,7 +124,7 @@ public class TypeRepository extends EntityRepository<Type> {
private List<CustomProperty> getCustomProperties(Type type) throws IOException { private List<CustomProperty> getCustomProperties(Type type) throws IOException {
if (type.getCategory().equals(Category.Field)) { if (type.getCategory().equals(Category.Field)) {
return null; // Property types don't support custom properties return null; // Property type fields don't support custom properties
} }
List<CustomProperty> customProperties = new ArrayList<>(); List<CustomProperty> customProperties = new ArrayList<>();
List<Triple<String, String, String>> results = List<Triple<String, String, String>> results =
@ -152,14 +153,35 @@ public class TypeRepository extends EntityRepository<Type> {
} }
private void updateCustomProperties() throws JsonProcessingException { private void updateCustomProperties() throws JsonProcessingException {
List<CustomProperty> updatedFields = listOrEmpty(updated.getCustomProperties()); List<CustomProperty> updatedProperties = listOrEmpty(updated.getCustomProperties());
List<CustomProperty> origFields = listOrEmpty(original.getCustomProperties()); List<CustomProperty> origProperties = listOrEmpty(original.getCustomProperties());
List<CustomProperty> added = new ArrayList<>(); List<CustomProperty> added = new ArrayList<>();
List<CustomProperty> deleted = new ArrayList<>(); List<CustomProperty> deleted = new ArrayList<>();
recordListChange("customProperties", origFields, updatedFields, added, deleted, customFieldMatch); recordListChange("customProperties", origProperties, updatedProperties, added, deleted, customFieldMatch);
for (CustomProperty property : added) { for (CustomProperty property : added) {
storeCustomProperty(property);
}
for (CustomProperty property : deleted) {
deleteCustomProperty(property);
}
// Record changes to updated custom properties (only description can be updated)
for (CustomProperty updateProperty : updatedProperties) {
// Find property that matches name and type
CustomProperty storedProperty =
origProperties.stream().filter(c -> customFieldMatch.test(c, updateProperty)).findAny().orElse(null);
if (storedProperty == null) { // New property added, which is already handled
continue;
}
updateCustomPropertyDescription(updated, storedProperty, updateProperty);
}
}
private void storeCustomProperty(CustomProperty property) throws JsonProcessingException {
String customPropertyFQN = getCustomPropertyFQN(updated.getName(), property.getName()); String customPropertyFQN = getCustomPropertyFQN(updated.getName(), property.getName());
String customPropertyJson = JsonUtils.pojoToJson(property); EntityReference propertyType = property.getPropertyType();
String customPropertyJson = JsonUtils.pojoToJson(property.withPropertyType(null)); // Don't store entity reference
property.withPropertyType(propertyType); // Restore entity reference
LOG.info( LOG.info(
"Adding customProperty {} with type {} to the entity {}", "Adding customProperty {} with type {} to the entity {}",
customPropertyFQN, customPropertyFQN,
@ -175,7 +197,8 @@ public class TypeRepository extends EntityRepository<Type> {
Relationship.HAS.ordinal(), Relationship.HAS.ordinal(),
customPropertyJson); customPropertyJson);
} }
for (CustomProperty property : deleted) {
private void deleteCustomProperty(CustomProperty property) {
String customPropertyFQN = getCustomPropertyFQN(updated.getName(), property.getName()); String customPropertyFQN = getCustomPropertyFQN(updated.getName(), property.getName());
LOG.info( LOG.info(
"Deleting customProperty {} with type {} from the entity {}", "Deleting customProperty {} with type {} from the entity {}",
@ -194,23 +217,25 @@ public class TypeRepository extends EntityRepository<Type> {
daoCollection.entityExtensionDAO().deleteExtension(customPropertyFQN); daoCollection.entityExtensionDAO().deleteExtension(customPropertyFQN);
} }
// Record changes to updated custom properties (only description can be updated) private void updateCustomPropertyDescription(
for (CustomProperty updated : updatedFields) { Type entity, CustomProperty origProperty, CustomProperty updatedProperty) throws JsonProcessingException {
// Find property that matches name and type String fieldName = getCustomField(origProperty, FIELD_DESCRIPTION);
CustomProperty stored = if (recordChange(fieldName, origProperty.getDescription(), updatedProperty.getDescription())) {
origFields.stream().filter(c -> customFieldMatch.test(c, updated)).findAny().orElse(null); String customPropertyFQN = getCustomPropertyFQN(entity.getName(), updatedProperty.getName());
if (stored == null) { // New property added EntityReference propertyType = updatedProperty.getPropertyType(); // Don't store entity reference
continue; String customPropertyJson = JsonUtils.pojoToJson(updatedProperty.withPropertyType(null));
} updatedProperty.withPropertyType(propertyType); // Restore entity reference
daoCollection
updateCustomPropertyDescription(stored, updated); .fieldRelationshipDAO()
} .upsert(
} customPropertyFQN,
updatedProperty.getPropertyType().getName(),
private void updateCustomPropertyDescription(CustomProperty orig, CustomProperty updated) Entity.TYPE,
throws JsonProcessingException { Entity.TYPE,
String fieldName = getCustomField(orig, FIELD_DESCRIPTION); Relationship.HAS.ordinal(),
recordChange(fieldName, orig.getDescription(), updated.getDescription()); "customProperty",
customPropertyJson);
}
} }
} }
} }

View File

@ -209,13 +209,17 @@ public class TypeResourceTest extends EntityResourceTest<Type, CreateType> {
@Override @Override
public void validateCreatedEntity(Type createdEntity, CreateType createRequest, Map<String, String> authHeaders) { public void validateCreatedEntity(Type createdEntity, CreateType createRequest, Map<String, String> authHeaders) {
assertEquals(createRequest.getSchema(), createdEntity.getSchema()); assertEquals(createRequest.getSchema(), createdEntity.getSchema());
// TODO assertEquals(createRequest.getCategory(), createdEntity.getCategory());
assertEquals(createRequest.getNameSpace(), createdEntity.getNameSpace());
} }
@Override @Override
public void compareEntities(Type expected, Type patched, Map<String, String> authHeaders) { public void compareEntities(Type expected, Type patched, Map<String, String> authHeaders) {
assertEquals(expected.getSchema(), patched.getSchema()); assertEquals(expected.getSchema(), patched.getSchema());
// TODO more checks assertEquals(expected.getSchema(), patched.getSchema());
assertEquals(expected.getCategory(), patched.getCategory());
assertEquals(expected.getNameSpace(), patched.getNameSpace());
assertEquals(expected.getCustomProperties(), patched.getCustomProperties());
} }
@Override @Override