From 512911a562d1291cef00d964a0a7df7e6559382b Mon Sep 17 00:00:00 2001 From: mohitdeuex Date: Wed, 18 Sep 2024 14:32:47 +0530 Subject: [PATCH 1/5] fix owners patch issue --- .../service/jdbi3/EntityRepository.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java index 85512c9e2abc..1c2641e22f94 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java @@ -934,7 +934,10 @@ public final PatchResponse patch(UriInfo uriInfo, UUID id, String user, JsonP updated.setUpdatedAt(System.currentTimeMillis()); prepareInternal(updated, true); - populateOwners(updated.getOwners()); + // Validate and populate owners + List validateOwners = validateAndGetOwners(updated.getOwners()); + updated.setOwners(validateOwners); + restorePatchAttributes(original, updated); // Update the attributes and relationships of an entity @@ -966,7 +969,9 @@ public final PatchResponse patch(UriInfo uriInfo, String fqn, String user, Js updated.setUpdatedAt(System.currentTimeMillis()); prepareInternal(updated, true); - populateOwners(updated.getOwners()); + // Validate and populate owners + List validateOwners = validateAndGetOwners(updated.getOwners()); + updated.setOwners(validateOwners); restorePatchAttributes(original, updated); // Update the attributes and relationships of an entity @@ -1972,21 +1977,17 @@ public final void inheritReviewers(T entity, Fields fields, EntityInterface pare } } - protected void populateOwners(List owners) { + protected List validateAndGetOwners(List owners) { if (nullOrEmpty(owners)) { - return; + return owners; } // populate owner entityRefs with all fields List 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 From 8e4157f6274d0b692056cd1472fea9af8919d9dc Mon Sep 17 00:00:00 2001 From: mohitdeuex Date: Wed, 18 Sep 2024 14:36:41 +0530 Subject: [PATCH 2/5] rename func --- .../org/openmetadata/service/jdbi3/EntityRepository.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java index 1c2641e22f94..cfa49eacef36 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java @@ -935,7 +935,7 @@ public final PatchResponse patch(UriInfo uriInfo, UUID id, String user, JsonP prepareInternal(updated, true); // Validate and populate owners - List validateOwners = validateAndGetOwners(updated.getOwners()); + List validateOwners = getValidatedOwners(updated.getOwners()); updated.setOwners(validateOwners); restorePatchAttributes(original, updated); @@ -970,7 +970,7 @@ public final PatchResponse patch(UriInfo uriInfo, String fqn, String user, Js prepareInternal(updated, true); // Validate and populate owners - List validateOwners = validateAndGetOwners(updated.getOwners()); + List validateOwners = getValidatedOwners(updated.getOwners()); updated.setOwners(validateOwners); restorePatchAttributes(original, updated); @@ -1977,7 +1977,7 @@ public final void inheritReviewers(T entity, Fields fields, EntityInterface pare } } - protected List validateAndGetOwners(List owners) { + protected List getValidatedOwners(List owners) { if (nullOrEmpty(owners)) { return owners; } From 956e425cf06b0d884156545a628182ede322d683 Mon Sep 17 00:00:00 2001 From: mohitdeuex Date: Wed, 18 Sep 2024 14:39:17 +0530 Subject: [PATCH 3/5] typo --- .../org/openmetadata/service/jdbi3/EntityRepository.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java index cfa49eacef36..06a2813ee573 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java @@ -935,8 +935,8 @@ public final PatchResponse patch(UriInfo uriInfo, UUID id, String user, JsonP prepareInternal(updated, true); // Validate and populate owners - List validateOwners = getValidatedOwners(updated.getOwners()); - updated.setOwners(validateOwners); + List validatedOwners = getValidatedOwners(updated.getOwners()); + updated.setOwners(validatedOwners); restorePatchAttributes(original, updated); @@ -970,8 +970,8 @@ public final PatchResponse patch(UriInfo uriInfo, String fqn, String user, Js prepareInternal(updated, true); // Validate and populate owners - List validateOwners = getValidatedOwners(updated.getOwners()); - updated.setOwners(validateOwners); + List validatedOwners = getValidatedOwners(updated.getOwners()); + updated.setOwners(validatedOwners); restorePatchAttributes(original, updated); // Update the attributes and relationships of an entity From 615c3c48f7b14b8bdcce6be434a2881618ba9934 Mon Sep 17 00:00:00 2001 From: mohitdeuex Date: Thu, 19 Sep 2024 00:39:07 +0530 Subject: [PATCH 4/5] Add Test Case --- .../service/resources/EntityResourceTest.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/openmetadata-service/src/test/java/org/openmetadata/service/resources/EntityResourceTest.java b/openmetadata-service/src/test/java/org/openmetadata/service/resources/EntityResourceTest.java index 55b4e6ce38d5..b4c951a15f83 100644 --- a/openmetadata-service/src/test/java/org/openmetadata/service/resources/EntityResourceTest.java +++ b/openmetadata-service/src/test/java/org/openmetadata/service/resources/EntityResourceTest.java @@ -1557,6 +1557,32 @@ 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(), "id", ADMIN_AUTH_HEADERS); + + // Check if the Owner is update to user1 and user 2 + List updateOwners = + List.of( + new EntityReference().withId(USER1.getId()).withType(USER), + new EntityReference().withId(USER1.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 { From a5a46916fbe4fc88f96ecaffc91f4c662b0635c4 Mon Sep 17 00:00:00 2001 From: mohitdeuex Date: Fri, 20 Sep 2024 11:40:08 +0530 Subject: [PATCH 5/5] Fix Tests --- .../service/resources/EntityResourceTest.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/openmetadata-service/src/test/java/org/openmetadata/service/resources/EntityResourceTest.java b/openmetadata-service/src/test/java/org/openmetadata/service/resources/EntityResourceTest.java index b4c951a15f83..dce74648f4cb 100644 --- a/openmetadata-service/src/test/java/org/openmetadata/service/resources/EntityResourceTest.java +++ b/openmetadata-service/src/test/java/org/openmetadata/service/resources/EntityResourceTest.java @@ -1567,13 +1567,18 @@ void patch_entityUpdateOwnerFromNull_200(TestInfo test) throws IOException { // 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(), "id", ADMIN_AUTH_HEADERS); + T entity = getEntity(createdEntity.getId(), allFields, ADMIN_AUTH_HEADERS); + + List previousOwners = entity.getOwners(); + if (nullOrEmpty(previousOwners)) { + entity.setOwners(null); + } // Check if the Owner is update to user1 and user 2 List updateOwners = List.of( new EntityReference().withId(USER1.getId()).withType(USER), - new EntityReference().withId(USER1.getId()).withType(USER)); + new EntityReference().withId(USER2.getId()).withType(USER)); String json = JsonUtils.pojoToJson(entity); entity.setOwners(updateOwners);