Skip to content
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
3 changes: 3 additions & 0 deletions .github/playwright/impact-map.json
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,9 @@
"openmetadata-service/src/test/java/org/openmetadata/service/jdbi3/ContextMemoryLifecycleTest.java",
"openmetadata-service/src/main/java/org/openmetadata/service/resources/knowledge/**",
"openmetadata-service/src/main/java/org/openmetadata/service/resources/drive/**",
"openmetadata-service/src/test/java/org/openmetadata/service/resources/drive/**",
"openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/ContextFile*.java",
"openmetadata-service/src/test/java/org/openmetadata/service/jdbi3/ContextFile*Test.java",
"openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/ContextMemoryRepository.java",
"openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/KnowledgePageRepository.java",
"openmetadata-service/src/main/java/org/openmetadata/service/search/indexes/ContextMemoryIndex.java",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Context file metadata edits preserve the stored content references

- **Status:** Accepted
- **Revisions:** v1 2026-10-30 (initial)
- **Deciders:** OpenMetadata maintainers
- **Guard:** `DriveMapperTest`, `ContextFileRepositoryTest`, `ContextFileIT`, `DriveFileUploadIT`
- **Related:** Context Center Drive upload and metadata endpoints

## Context

Context Center uploads create the stored asset and content snapshot together. Metadata requests
do not create that content, and a PUT that omits content fields must keep the uploaded document.

## Decision

The upload path assigns `ContextFile.assetId` and `ContextFile.headContentId` after request mapping.
POST and PUT metadata requests supplying the legacy `CreateContextFile.assetId` field receive 400
and must use the upload endpoint to add content. PUT and PATCH updates always preserve both stored
references, including when the submitted fields are absent or null. Processing and moves keep
using the existing references.

## Consequences

Metadata edits return and index the same content references that remain in the stored file.
Any future flow that attaches content must establish the references through a server-owned
operation, rather than a metadata request.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.apache.http.client.HttpResponseException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.openmetadata.schema.api.context.CreateContextMemory;
import org.openmetadata.schema.api.data.CreateContextFile;
import org.openmetadata.schema.api.data.CreateFolder;
Expand Down Expand Up @@ -103,6 +105,22 @@ private List<String> listFileIds(RestClient rest, String path) {

// --- CRUD ---

@ParameterizedTest
@ValueSource(booleans = {false, true})
void testMetadataRequestsRejectAssetId(boolean createOrUpdate, TestNamespace ns) {
RestClient rest = RestClient.admin();
CreateContextFile request =
new CreateContextFile().withName(ns.prefix("metadata-file")).withAssetId("stored-asset");

try (Response response =
createOrUpdate ? rest.rawPut(FILE_PATH, request) : rest.rawPost(FILE_PATH, request)) {
assertEquals(400, response.getStatus());
assertEquals(
"File content must be assigned through the upload endpoint.",
JsonUtils.readTree(response.readEntity(String.class)).get("message").asText());
}
}

@Test
void testCreateContextFile(TestNamespace ns) throws HttpResponseException {
RestClient rest = RestClient.admin();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openmetadata.schema.api.data.CreateContextFile;
import org.openmetadata.schema.api.data.CreateFolder;
import org.openmetadata.schema.api.domains.CreateDomain;
import org.openmetadata.schema.entity.context.MemoryShareConfig;
import org.openmetadata.schema.entity.context.MemoryVisibility;
import org.openmetadata.schema.entity.data.ContextFile;
import org.openmetadata.schema.entity.data.ContextFileType;
import org.openmetadata.schema.entity.data.Folder;
import org.openmetadata.schema.entity.data.ProcessingStatus;
import org.openmetadata.schema.entity.domains.Domain;
import org.openmetadata.schema.entity.teams.User;
import org.openmetadata.schema.utils.JsonUtils;
import org.openmetadata.sdk.test.util.RestClient;
import org.openmetadata.sdk.test.util.SdkClients;
Expand All @@ -84,6 +88,7 @@
class DriveFileUploadIT {

private static final String S3_BUCKET = "test-bucket";
private static final String FILE_PATH = "v1/contextCenter/drive/files";
private static final Duration EXTRACTION_TIMEOUT = Duration.ofSeconds(60);
private static final Duration SEARCH_VISIBLE_TIMEOUT = Duration.ofSeconds(60);
private static final Duration POLL_INTERVAL = Duration.ofMillis(500);
Expand Down Expand Up @@ -952,6 +957,105 @@ void testSoftDeletedFileCanDownloadFromTrash(TestNamespace ns) throws Exception
}
}

@Test
void testMetadataUpdatesPreserveUploadedContent(TestNamespace ns) throws Exception {
RestClient rest = RestClient.admin();
byte[] content = readFixture("/drive/sample-notes.txt");
ContextFile uploaded;
try (Response response = uploadUniqueFixture(ns, "/drive/sample-notes.txt", "Metadata edits")) {
uploaded = readCreatedContextFile(response, "Upload failed");
}
assertNotNull(uploaded.getAssetId());
assertNotNull(uploaded.getHeadContentId());

ContextFile updated =
rest.update(
FILE_PATH,
new CreateContextFile()
.withName(uploaded.getName())
.withDescription("Updated metadata"),
ContextFile.class);
assertContentReferences(uploaded, updated);
assertContentReferences(uploaded, fetchFile(uploaded.getId()));

String original = JsonUtils.pojoToJson(updated);
updated.withAssetId(null).withHeadContentId(null).withDescription("Cleared metadata fields");
updated = rest.patch(FILE_PATH, updated.getId(), original, updated, ContextFile.class);
assertContentReferences(uploaded, updated);
assertContentReferences(uploaded, fetchFile(uploaded.getId()));
assertArrayEquals(content, downloadContent(rest, uploaded.getId()));
}

@Test
void testMetadataOnlyFileKeepsIndependentContent(TestNamespace ns) throws Exception {
RestClient admin = RestClient.admin();
User owner = DriveTestUsers.createUser(ns, "metadata-owner");
RestClient ownerRest = RestClient.forUser(owner.getEmail(), new String[] {});
byte[] content = readFixture("/drive/sample-notes.txt");
ContextFile uploaded;
try (Response response = uploadUniqueFixture(ns, "/drive/sample-notes.txt", "Stored content")) {
uploaded = readCreatedContextFile(response, "Upload failed");
}
String original = JsonUtils.pojoToJson(uploaded);
uploaded.setShareConfig(new MemoryShareConfig().withVisibility(MemoryVisibility.PRIVATE));
uploaded = admin.patch(FILE_PATH, uploaded.getId(), original, uploaded, ContextFile.class);
try (Response response = ownerRest.rawGet(FILE_PATH + "/" + uploaded.getId() + "/download")) {
assertEquals(403, response.getStatus());
}
try (Response response =
ownerRest.rawDelete(FILE_PATH + "/" + uploaded.getId() + "?hardDelete=true")) {
assertEquals(403, response.getStatus());
}

ContextFile metadata =
admin.create(
FILE_PATH,
new CreateContextFile()
.withName(ns.prefix("metadata-only"))
.withOwners(List.of(owner.getEntityReference())),
ContextFile.class);
original = JsonUtils.pojoToJson(metadata);
metadata.withAssetId(uploaded.getAssetId()).withDescription("First metadata edit");
metadata = ownerRest.patch(FILE_PATH, metadata.getId(), original, metadata, ContextFile.class);
assertNull(metadata.getAssetId());
assertNull(fetchFile(metadata.getId()).getAssetId());

original = JsonUtils.pojoToJson(metadata);
metadata.withHeadContentId(uploaded.getHeadContentId()).withDescription("Second metadata edit");
metadata = ownerRest.patch(FILE_PATH, metadata.getId(), original, metadata, ContextFile.class);
assertNull(metadata.getHeadContentId());
assertNull(fetchFile(metadata.getId()).getHeadContentId());
try (Response response = ownerRest.rawGet(FILE_PATH + "/" + metadata.getId() + "/download")) {
assertEquals(404, response.getStatus());
}

UUID metadataId = metadata.getId();
ownerRest.hardDelete(FILE_PATH, metadataId);
await()
.atMost(Duration.ofSeconds(30))
.untilAsserted(
() -> {
try (Response response =
admin.rawGet(FILE_PATH + "/" + metadataId + "?include=all")) {
assertEquals(404, response.getStatus());
}
});
assertArrayEquals(content, downloadContent(admin, uploaded.getId()));
assertStoredInS3(uploaded.getAssetId(), content);
}

private void assertContentReferences(ContextFile expected, ContextFile actual) {
assertEquals(expected.getAssetId(), actual.getAssetId());
assertEquals(expected.getHeadContentId(), actual.getHeadContentId());
}

private byte[] downloadContent(RestClient rest, UUID id) {
try (Response response = rest.rawGet(FILE_PATH + "/" + id + "/download?redirect=false")) {
assertEquals(200, response.getStatus());
return response.readEntity(byte[].class);
}
}

@Test
void testHardDeleteRemovesObjectFromS3(TestNamespace ns) throws Exception {
byte[] content = readFixture("/drive/sample-notes.txt");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.openmetadata.it.util.SdkClients;
import org.openmetadata.it.util.TestNamespace;
import org.openmetadata.it.util.TestNamespaceExtension;
import org.openmetadata.schema.api.data.CreateContextFile;
import org.openmetadata.schema.api.data.CreatePage;
import org.openmetadata.schema.entity.context.ContextMemory;
import org.openmetadata.schema.entity.context.ContextMemoryScope;
Expand Down Expand Up @@ -473,15 +472,23 @@ private Page readPage(Page page) throws Exception {
}

private ContextFile file(TestNamespace ns, String label, String text) throws Exception {
UUID contentId = UUID.randomUUID();
ContextFile file =
ns.trackRoot(
Entity.CONTEXT_FILE,
RestClient.admin()
fileRepository()
.create(
FILES, new CreateContextFile().withName(ns.prefix(label)), ContextFile.class));
null,
new ContextFile()
.withId(UUID.randomUUID())
.withName(ns.prefix(label))
.withHeadContentId(contentId.toString())
.withProcessingStatus(ProcessingStatus.ExtractingContext)
.withUpdatedBy(Entity.ADMIN_USER_NAME)
.withUpdatedAt(System.currentTimeMillis())));
ContextFileContent content =
new ContextFileContent()
.withId(UUID.randomUUID())
.withId(contentId)
.withName(UUID.randomUUID().toString())
.withContextFile(file.getEntityReference())
.withChecksum(DigestUtils.sha256Hex(text))
Expand All @@ -490,12 +497,7 @@ private ContextFile file(TestNamespace ns, String label, String text) throws Exc
.withUpdatedAt(System.currentTimeMillis())
.withProcessingStatus(ProcessingStatus.Processed);
fileRepository().getContentRepository().create(null, content);
ContextFile updated =
JsonUtils.deepCopy(file, ContextFile.class)
.withHeadContentId(content.getId().toString())
.withProcessingStatus(ProcessingStatus.ExtractingContext);
fileRepository().update(null, file, updated, Entity.ADMIN_USER_NAME);
return updated;
return file;
}

private List<ContextMemory> extracted(EntityReference source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,18 @@ private void emitMoveChangeEvent(ContextFile original, ContextFile updated) {
public class ContextFileUpdater extends EntityUpdater {
public ContextFileUpdater(ContextFile original, ContextFile updated, Operation operation) {
super(original, updated, operation);
keepStoredContent(original, updated);
// Consolidated edits still need distinct timestamps for conditional processing writes.
if (original.getUpdatedAt() != null && updated.getUpdatedAt() != null) {
updated.setUpdatedAt(Math.max(updated.getUpdatedAt(), original.getUpdatedAt() + 1));
}
}

private static void keepStoredContent(ContextFile original, ContextFile updated) {
updated.setAssetId(original.getAssetId());
updated.setHeadContentId(original.getHeadContentId());
}

@Override
public void entitySpecificUpdate(boolean consolidatingChanges) {
recordChange("fileType", original.getFileType(), updated.getFileType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
import org.openmetadata.schema.api.data.CreateContextFile;
import org.openmetadata.schema.entity.data.ContextFile;
import org.openmetadata.schema.type.Votes;
import org.openmetadata.service.exception.BadRequestException;
import org.openmetadata.service.jdbi3.FolderRepository;
import org.openmetadata.service.mapper.EntityMapper;

public class ContextFileMapper implements EntityMapper<ContextFile, CreateContextFile> {
@Override
public ContextFile createToEntity(CreateContextFile create, String user) {
if (create.getAssetId() != null) {
throw new BadRequestException("File content must be assigned through the upload endpoint.");
}
return copy(new ContextFile(), create, user)
.withTags(create.getTags())
.withVotes(new Votes().withUpVotes(0).withDownVotes(0))
Expand All @@ -19,7 +23,6 @@ public ContextFile createToEntity(CreateContextFile create, String user) {
.withContentType(create.getContentType())
.withFileExtension(create.getFileExtension())
.withFolder(getEntityReference(FolderRepository.FOLDER_ENTITY, create.getFolder()))
.withAssetId(create.getAssetId())
.withProcessingStatus(create.getProcessingStatus())
.withSourceType(create.getSourceType())
.withSourceId(create.getSourceId())
Expand Down
Loading
Loading