Fix Allowed Fields (#4756)

This commit is contained in:
Sriharsha Chintalapani 2022-05-05 22:56:26 -07:00 committed by GitHub
parent ee1dda5366
commit ebf329dd7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 5 deletions

View File

@ -273,6 +273,12 @@ public final class Entity {
return new ArrayList<>(Arrays.asList(propertyOrder.value()));
}
public static <T> List<String> getAllowedFields(Class<T> clz) {
String entityType = getEntityTypeFromClass(clz);
EntityRepository<?> repository = getEntityRepository(entityType);
return repository.getAllowedFields();
}
/** Class for getting validated entity list from a queryParam with list of entities. */
public static class EntityList {
private EntityList() {}

View File

@ -906,6 +906,10 @@ public abstract class EntityRepository<T> {
return new Fields(allowedFields, fields);
}
public final List<String> getAllowedFields() {
return allowedFields;
}
enum Operation {
PUT,
PATCH,

View File

@ -36,7 +36,7 @@ public abstract class EntityResource<T, K extends EntityRepository<T>> {
public EntityResource(Class<T> entityClass, K repository, Authorizer authorizer) {
this.entityClass = entityClass;
allowedFields = Entity.getEntityFields(entityClass);
allowedFields = Entity.getAllowedFields(entityClass);
supportsOwner = allowedFields.contains(FIELD_OWNER);
this.dao = repository;
this.authorizer = authorizer;

View File

@ -224,12 +224,11 @@ public abstract class EntityResourceTest<T, K> extends CatalogApplicationTest {
this.entityListClass = entityListClass;
this.collectionName = collectionName;
this.allFields = fields;
ENTITY_RESOURCE_TEST_MAP.put(entityType, this);
List<String> allowedFields = Entity.getEntityFields(entityClass);
this.supportsFollowers = allowedFields.contains(FIELD_FOLLOWERS);
this.supportsOwner = allowedFields.contains(FIELD_OWNER);
this.supportsTags = allowedFields.contains(FIELD_TAGS);
ENTITY_RESOURCE_TEST_MAP.put(entityType, this);
}
@BeforeAll
@ -340,7 +339,7 @@ public abstract class EntityResourceTest<T, K> extends CatalogApplicationTest {
T entity = createEntity(createRequest(test, 0), ADMIN_AUTH_HEADERS);
EntityInterface<T> entityInterface = getEntityInterface(entity);
String allFields = String.join(",", Entity.getEntityFields(entityClass));
String allFields = String.join(",", Entity.getAllowedFields(entityClass));
// GET an entity by ID with all the field names of an entity should be successful
getEntity(entityInterface.getId(), allFields, ADMIN_AUTH_HEADERS);

View File

@ -111,7 +111,6 @@ public class UserResourceTest extends EntityResourceTest<User, CreateUser> {
public UserResourceTest() {
super(Entity.USER, User.class, UserList.class, "users", UserResource.FIELDS);
this.supportsAuthorizedMetadataOperations = false;
this.supportsFieldsQueryParam = false;
}
public void setupUsers(TestInfo test) throws HttpResponseException {