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

View File

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