Fix: Enforce 255-byte filename limit (#8290)

### What problem does this PR solve?

- Add filename length validation (<=255 bytes) for document
upload/rename in both HTTP and SDK APIs
- Update error messages for consistency
- Fix comparison operator in SDK from '>=' to '>' for filename length
check

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
Liu An
2025-06-16 16:39:41 +08:00
committed by GitHub
parent bde76d2f55
commit a3bebeb599
7 changed files with 26 additions and 20 deletions

View File

@ -25,7 +25,7 @@ class TestDocumentsUpdated:
"name, expected_message",
[
("new_name.txt", ""),
(f"{'a' * (DOCUMENT_NAME_LIMIT - 3)}.txt", "The name should be less than 128 bytes"),
(f"{'a' * (DOCUMENT_NAME_LIMIT - 4)}.txt", ""),
(0, "AttributeError"),
(None, "AttributeError"),
("", "The extension of file can't be changed"),

View File

@ -113,16 +113,17 @@ class TestDocumentsUpload:
assert str(excinfo.value) == "No file selected!", str(excinfo.value)
@pytest.mark.p2
def test_filename_exceeds_max_length(self, add_dataset_func, tmp_path):
def test_filename_max_length(self, add_dataset_func, tmp_path):
dataset = add_dataset_func
fp = create_txt_file(tmp_path / f"{'a' * (DOCUMENT_NAME_LIMIT - 3)}.txt")
fp = create_txt_file(tmp_path / f"{'a' * (DOCUMENT_NAME_LIMIT - 4)}.txt")
with fp.open("rb") as f:
blob = f.read()
with pytest.raises(Exception) as excinfo:
dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
assert str(excinfo.value) == "File name should be less than 128 bytes.", str(excinfo.value)
documents = dataset.upload_documents([{"display_name": fp.name, "blob": blob}])
for document in documents:
assert document.dataset_id == dataset.id, str(document)
assert document.name == fp.name, str(document)
@pytest.mark.p2
def test_duplicate_files(self, add_dataset_func, tmp_path):