Fix #5042: Add a field to an entity API is failing (#5215)

This commit is contained in:
Sriharsha Chintalapani 2022-05-30 09:23:35 -07:00 committed by GitHub
parent 233699a7a1
commit a75fd13695
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 4 deletions

View File

@ -56,7 +56,7 @@ public class TypeRegistry {
}
}
public static void removeType(String typeName) {
public void removeType(String typeName) {
TYPES.remove(typeName);
LOG.info("Deleted type {}\n", typeName);
// TODO cleanup custom fields

View File

@ -409,6 +409,8 @@ public abstract class EntityRepository<T extends EntityInterface> {
PutResponse<T> response = createOrUpdateInternal(uriInfo, updated);
if (response.getStatus() == Status.CREATED) {
postCreate(response.getEntity());
} else if (response.getStatus() == Status.OK) {
postUpdate(response.getEntity());
}
return response;
}

View File

@ -84,7 +84,12 @@ public class TypeRepository extends EntityRepository<Type> {
@Override
protected void postDelete(Type entity) {
TypeRegistry.removeType(entity.getName());
TypeRegistry.instance().removeType(entity.getName());
}
@Override
protected void postUpdate(Type entity) {
TypeRegistry.instance().addType(entity);
}
@Override
@ -95,6 +100,7 @@ public class TypeRepository extends EntityRepository<Type> {
public PutResponse<Type> addCustomField(UriInfo uriInfo, String updatedBy, String id, CustomField field)
throws IOException {
Type type = dao.findEntityById(UUID.fromString(id), Include.NON_DELETED);
field.setFieldType(dao.findEntityReferenceById(field.getFieldType().getId(), Include.NON_DELETED));
if (type.getCategory().equals(Category.Field)) {
throw new IllegalArgumentException("Field types can't be extended");
}

View File

@ -41,6 +41,7 @@ import org.openmetadata.catalog.entity.type.CustomField;
import org.openmetadata.catalog.resources.EntityResourceTest;
import org.openmetadata.catalog.resources.types.TypeResource;
import org.openmetadata.catalog.resources.types.TypeResource.TypeList;
import org.openmetadata.catalog.type.EntityReference;
import org.openmetadata.catalog.util.TestUtils;
@Slf4j
@ -91,9 +92,13 @@ public class TypeResourceTest extends EntityResourceTest<Type, CreateType> {
assertEquals(fieldA, tableEntity.getCustomFields().get(0));
// Add a second field with name intB with type integer
CustomField fieldB =
new CustomField().withName("intB").withDescription("intB").withFieldType(INT_TYPE.getEntityReference());
EntityReference typeRef =
new EntityReference()
.withType(INT_TYPE.getEntityReference().getType())
.withId(INT_TYPE.getEntityReference().getId());
CustomField fieldB = new CustomField().withName("intB").withDescription("intB").withFieldType(typeRef);
tableEntity = addCustomField(tableEntity.getId(), fieldB, Status.OK, ADMIN_AUTH_HEADERS);
fieldB.setFieldType(INT_TYPE.getEntityReference());
assertEquals(2, tableEntity.getCustomFields().size());
assertEquals(fieldA, tableEntity.getCustomFields().get(0));
assertEquals(fieldB, tableEntity.getCustomFields().get(1));