Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix owners patch issue #17900

Merged
merged 7 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,10 @@ public final PatchResponse<T> patch(UriInfo uriInfo, UUID id, String user, JsonP
updated.setUpdatedAt(System.currentTimeMillis());

prepareInternal(updated, true);
populateOwners(updated.getOwners());
// Validate and populate owners
List<EntityReference> validatedOwners = getValidatedOwners(updated.getOwners());
updated.setOwners(validatedOwners);

restorePatchAttributes(original, updated);

// Update the attributes and relationships of an entity
Expand Down Expand Up @@ -966,7 +969,9 @@ public final PatchResponse<T> patch(UriInfo uriInfo, String fqn, String user, Js
updated.setUpdatedAt(System.currentTimeMillis());

prepareInternal(updated, true);
populateOwners(updated.getOwners());
// Validate and populate owners
List<EntityReference> validatedOwners = getValidatedOwners(updated.getOwners());
updated.setOwners(validatedOwners);
restorePatchAttributes(original, updated);

// Update the attributes and relationships of an entity
Expand Down Expand Up @@ -1972,21 +1977,17 @@ public final void inheritReviewers(T entity, Fields fields, EntityInterface pare
}
}

protected void populateOwners(List<EntityReference> owners) {
protected List<EntityReference> getValidatedOwners(List<EntityReference> owners) {
if (nullOrEmpty(owners)) {
return;
return owners;
}
// populate owner entityRefs with all fields
List<EntityReference> refs = validateOwners(owners);
if (nullOrEmpty(refs)) {
return;
return owners;
}
refs.sort(Comparator.comparing(EntityReference::getName));
owners.sort(Comparator.comparing(EntityReference::getName));

for (int i = 0; i < owners.size(); i++) {
EntityUtil.copy(refs.get(i), owners.get(i));
}
return refs;
}

@Transaction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,37 @@ void patch_entityUpdateOwner_200(TestInfo test) throws IOException {
CatalogExceptionMessage.invalidOwnerType(TEST_DEFINITION));
}

@Test
@Execution(ExecutionMode.CONCURRENT)
void patch_entityUpdateOwnerFromNull_200(TestInfo test) throws IOException {
if (!supportsOwners || !supportsPatch) {
return; // Entity doesn't support ownership
}

// Create Entity with Null Owner
K request = createRequest(getEntityName(test), "description", "displayName", null);
T createdEntity = createAndCheckEntity(request, ADMIN_AUTH_HEADERS);
T entity = getEntity(createdEntity.getId(), allFields, ADMIN_AUTH_HEADERS);

List<EntityReference> previousOwners = entity.getOwners();
if (nullOrEmpty(previousOwners)) {
entity.setOwners(null);
}

// Check if the Owner is update to user1 and user 2
List<EntityReference> updateOwners =
List.of(
new EntityReference().withId(USER1.getId()).withType(USER),
new EntityReference().withId(USER2.getId()).withType(USER));

String json = JsonUtils.pojoToJson(entity);
entity.setOwners(updateOwners);
ChangeDescription change = getChangeDescription(entity, MINOR_UPDATE);
fieldAdded(change, FIELD_OWNERS, updateOwners);
entity = patchEntityAndCheck(entity, json, ADMIN_AUTH_HEADERS, MINOR_UPDATE, change);
assertEntityReferences(updateOwners, entity.getOwners());
}

@Test
@Execution(ExecutionMode.CONCURRENT)
void put_entityUpdate_as_non_owner_4xx(TestInfo test) throws IOException {
Expand Down
Loading