Fix #17118: Elasticsearch HTTP Method Error in update_by_query; Fix: patch tags for request/response schema in ApiEndpoint (#17383)

* Minor: fix patch tags on apiendpoint

* Fix #17118: Elasticsearch HTTP Method Error in update_by_query; Fix: patch tags for request/response schema in ApiEndpoint
This commit is contained in:
Sriharsha Chintalapani 2024-08-10 17:56:52 -07:00 committed by GitHub
parent 0548342239
commit a6ebac9b68
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 130 additions and 10 deletions

View File

@ -74,11 +74,13 @@ public class APIEndpointRepository extends EntityRepository<APIEndpoint> {
apiEndpoint.getApiCollection().getFullyQualifiedName(), apiEndpoint.getName()));
if (apiEndpoint.getRequestSchema() != null) {
setFieldFQN(
apiEndpoint.getFullyQualifiedName(), apiEndpoint.getRequestSchema().getSchemaFields());
apiEndpoint.getFullyQualifiedName() + ".requestSchema",
apiEndpoint.getRequestSchema().getSchemaFields());
}
if (apiEndpoint.getResponseSchema() != null) {
setFieldFQN(
apiEndpoint.getFullyQualifiedName(), apiEndpoint.getResponseSchema().getSchemaFields());
apiEndpoint.getFullyQualifiedName() + ".responseSchema",
apiEndpoint.getResponseSchema().getSchemaFields());
}
}
@ -147,14 +149,14 @@ public class APIEndpointRepository extends EntityRepository<APIEndpoint> {
populateEntityFieldTags(
entityType,
apiEndpoint.getRequestSchema().getSchemaFields(),
apiEndpoint.getFullyQualifiedName(),
apiEndpoint.getFullyQualifiedName() + ".requestSchema",
fields.contains(FIELD_TAGS));
}
if (apiEndpoint.getResponseSchema() != null) {
populateEntityFieldTags(
entityType,
apiEndpoint.getResponseSchema().getSchemaFields(),
apiEndpoint.getFullyQualifiedName(),
apiEndpoint.getFullyQualifiedName() + ".responseSchema",
fields.contains(FIELD_TAGS));
}
}
@ -242,7 +244,7 @@ public class APIEndpointRepository extends EntityRepository<APIEndpoint> {
// Add table level tags by adding tag to table relationship
super.applyTags(apiEndpoint);
if (apiEndpoint.getRequestSchema() != null) {
applyTags(apiEndpoint.getResponseSchema().getSchemaFields());
applyTags(apiEndpoint.getRequestSchema().getSchemaFields());
}
if (apiEndpoint.getResponseSchema() != null) {
applyTags(apiEndpoint.getResponseSchema().getSchemaFields());
@ -270,11 +272,18 @@ public class APIEndpointRepository extends EntityRepository<APIEndpoint> {
List<TagLabel> allTags = new ArrayList<>();
APIEndpoint apiEndpoint = (APIEndpoint) entity;
EntityUtil.mergeTags(allTags, apiEndpoint.getTags());
List<Field> schemaFields =
List<Field> requestSchemaFields =
apiEndpoint.getRequestSchema() != null
? apiEndpoint.getRequestSchema().getSchemaFields()
: null;
List<Field> responseSchemaFields =
apiEndpoint.getResponseSchema() != null
? apiEndpoint.getResponseSchema().getSchemaFields()
: null;
for (Field schemaField : listOrEmpty(schemaFields)) {
for (Field schemaField : listOrEmpty(responseSchemaFields)) {
EntityUtil.mergeTags(allTags, schemaField.getTags());
}
for (Field schemaField : listOrEmpty(requestSchemaFields)) {
EntityUtil.mergeTags(allTags, schemaField.getTags());
}
return allTags;

View File

@ -389,9 +389,9 @@ public class SearchRepository {
} else {
parentMatch = new ImmutablePair<>(entityType + ".id", entityId);
}
if (updates.getKey() != null && !updates.getKey().isEmpty()) {
searchClient.updateChildren(
indexMapping.getChildAliases(clusterAlias), parentMatch, updates);
List<String> childAliases = indexMapping.getChildAliases(clusterAlias);
if (updates.getKey() != null && !updates.getKey().isEmpty() && !nullOrEmpty(childAliases)) {
searchClient.updateChildren(childAliases, parentMatch, updates);
}
}
}

View File

@ -42,9 +42,11 @@ import org.openmetadata.schema.type.ChangeDescription;
import org.openmetadata.schema.type.EntityReference;
import org.openmetadata.schema.type.Field;
import org.openmetadata.schema.type.FieldDataType;
import org.openmetadata.schema.type.TagLabel;
import org.openmetadata.service.Entity;
import org.openmetadata.service.resources.EntityResourceTest;
import org.openmetadata.service.util.FullyQualifiedName;
import org.openmetadata.service.util.JsonUtils;
import org.openmetadata.service.util.ResultList;
import org.openmetadata.service.util.TestUtils;
@ -155,6 +157,115 @@ public class APIEndpointResourceTest extends EntityResourceTest<APIEndpoint, Cre
createAPIEndpoint, Response.Status.OK, ADMIN_AUTH_HEADERS, MINOR_UPDATE, change);
}
@Test
void put_patch_endPointTags_200_ok(TestInfo test) throws IOException {
APISchema responseSchema = new APISchema().withSchemaFields(api_response_fields);
CreateAPIEndpoint createAPIEndpoint =
createRequest(test)
.withOwners(List.of(USER1_REF))
.withRequestMethod(APIRequestMethod.GET)
.withEndpointURL(URI.create("https://localhost:8585/api/v1/users"))
.withResponseSchema(responseSchema);
// Patch and update the topic
APIEndpoint apiEndpoint = createEntity(createAPIEndpoint, ADMIN_AUTH_HEADERS);
createAPIEndpoint
.withOwners(List.of(TEAM11_REF))
.withResponseSchema(responseSchema)
.withRequestMethod(APIRequestMethod.POST);
ChangeDescription change = getChangeDescription(apiEndpoint, MINOR_UPDATE);
fieldAdded(change, FIELD_OWNERS, List.of(TEAM11_REF));
fieldDeleted(change, FIELD_OWNERS, List.of(USER1_REF));
fieldUpdated(change, "requestMethod", "GET", "POST");
updateAndCheckEntity(
createAPIEndpoint, Response.Status.OK, ADMIN_AUTH_HEADERS, MINOR_UPDATE, change);
APIEndpoint endpoint = getAPIEndpoint(apiEndpoint.getId(), "tags", ADMIN_AUTH_HEADERS);
String endpointJson = JsonUtils.pojoToJson(endpoint);
List<Field> fields = endpoint.getResponseSchema().getSchemaFields();
assertFields(api_response_fields, fields);
fields.get(0).getTags().add(PERSONAL_DATA_TAG_LABEL);
fields.get(0).getTags().add(PII_SENSITIVE_TAG_LABEL);
endpoint.getResponseSchema().setSchemaFields(fields);
patchEntity(endpoint.getId(), endpointJson, endpoint, ADMIN_AUTH_HEADERS);
endpoint = getAPIEndpoint(apiEndpoint.getId(), "tags", ADMIN_AUTH_HEADERS);
fields = endpoint.getResponseSchema().getSchemaFields();
List<TagLabel> tags = fields.get(0).getTags();
for (TagLabel tag : tags) {
assertTrue(tag.equals(PERSONAL_DATA_TAG_LABEL) || tag.equals(PII_SENSITIVE_TAG_LABEL));
}
endpointJson = JsonUtils.pojoToJson(endpoint);
fields = endpoint.getResponseSchema().getSchemaFields();
fields.get(0).getTags().remove(PERSONAL_DATA_TAG_LABEL);
endpoint.getResponseSchema().setSchemaFields(fields);
patchEntity(endpoint.getId(), endpointJson, endpoint, ADMIN_AUTH_HEADERS);
endpoint = getAPIEndpoint(apiEndpoint.getId(), "tags", ADMIN_AUTH_HEADERS);
fields = endpoint.getResponseSchema().getSchemaFields();
tags = fields.get(0).getTags();
assertEquals(1, tags.size());
for (TagLabel tag : tags) {
assertEquals(tag, PII_SENSITIVE_TAG_LABEL);
}
// add 2 new tags
endpoint = getAPIEndpoint(apiEndpoint.getId(), "tags", ADMIN_AUTH_HEADERS);
endpointJson = JsonUtils.pojoToJson(endpoint);
fields = endpoint.getResponseSchema().getSchemaFields();
fields.get(0).getTags().add(PERSONAL_DATA_TAG_LABEL);
fields.get(0).getTags().add(USER_ADDRESS_TAG_LABEL);
patchEntity(endpoint.getId(), endpointJson, endpoint, ADMIN_AUTH_HEADERS);
endpoint = getAPIEndpoint(apiEndpoint.getId(), "tags", ADMIN_AUTH_HEADERS);
fields = endpoint.getResponseSchema().getSchemaFields();
tags = fields.get(0).getTags();
assertEquals(3, tags.size());
for (TagLabel tag : tags) {
assertTrue(
tag.equals(PERSONAL_DATA_TAG_LABEL)
|| tag.equals(PII_SENSITIVE_TAG_LABEL)
|| tag.equals(USER_ADDRESS_TAG_LABEL));
}
// remove 1 tag
endpoint = getAPIEndpoint(apiEndpoint.getId(), "tags", ADMIN_AUTH_HEADERS);
endpointJson = JsonUtils.pojoToJson(endpoint);
fields = endpoint.getResponseSchema().getSchemaFields();
fields.get(0).getTags().remove(PERSONAL_DATA_TAG_LABEL);
patchEntity(endpoint.getId(), endpointJson, endpoint, ADMIN_AUTH_HEADERS);
endpoint = getAPIEndpoint(apiEndpoint.getId(), "tags", ADMIN_AUTH_HEADERS);
fields = endpoint.getResponseSchema().getSchemaFields();
tags = fields.get(0).getTags();
assertEquals(2, tags.size());
for (TagLabel tag : tags) {
assertTrue(tag.equals(PII_SENSITIVE_TAG_LABEL) || tag.equals(USER_ADDRESS_TAG_LABEL));
}
endpoint = getAPIEndpoint(apiEndpoint.getId(), "tags", ADMIN_AUTH_HEADERS);
endpointJson = JsonUtils.pojoToJson(endpoint);
endpoint.setRequestSchema(RESPONSE_SCHEMA);
patchEntity(endpoint.getId(), endpointJson, endpoint, ADMIN_AUTH_HEADERS);
endpoint = getAPIEndpoint(apiEndpoint.getId(), "tags", ADMIN_AUTH_HEADERS);
List<Field> requestFields = endpoint.getRequestSchema().getSchemaFields();
assertFields(api_response_fields, requestFields);
requestFields.get(0).getTags().add(PII_SENSITIVE_TAG_LABEL);
requestFields.get(0).getTags().add(USER_ADDRESS_TAG_LABEL);
patchEntity(endpoint.getId(), endpointJson, endpoint, ADMIN_AUTH_HEADERS);
endpoint = getAPIEndpoint(apiEndpoint.getId(), "tags", ADMIN_AUTH_HEADERS);
endpointJson = JsonUtils.pojoToJson(endpoint);
requestFields = endpoint.getRequestSchema().getSchemaFields();
fields = endpoint.getResponseSchema().getSchemaFields();
requestFields.get(0).getTags().remove(PII_SENSITIVE_TAG_LABEL);
fields.get(0).getTags().remove(USER_ADDRESS_TAG_LABEL);
patchEntity(endpoint.getId(), endpointJson, endpoint, ADMIN_AUTH_HEADERS);
endpoint = getAPIEndpoint(apiEndpoint.getId(), "tags", ADMIN_AUTH_HEADERS);
requestFields = endpoint.getRequestSchema().getSchemaFields();
fields = endpoint.getResponseSchema().getSchemaFields();
assertEquals(1, requestFields.get(0).getTags().size());
assertEquals(1, fields.get(0).getTags().size());
assertEquals(USER_ADDRESS_TAG_LABEL, requestFields.get(0).getTags().get(0));
assertEquals(PII_SENSITIVE_TAG_LABEL, fields.get(0).getTags().get(0));
}
@Override
public CreateAPIEndpoint createRequest(String name) {
return new CreateAPIEndpoint()