mirror of https://github.com/langgenius/dify.git
fix: `File` model add known extra fields, fix issue about the tool of… (#27607)
This commit is contained in:
parent
94cd2de940
commit
59c56b1b0d
|
|
@ -74,6 +74,10 @@ class File(BaseModel):
|
||||||
storage_key: str | None = None,
|
storage_key: str | None = None,
|
||||||
dify_model_identity: str | None = FILE_MODEL_IDENTITY,
|
dify_model_identity: str | None = FILE_MODEL_IDENTITY,
|
||||||
url: str | None = None,
|
url: str | None = None,
|
||||||
|
# Legacy compatibility fields - explicitly handle known extra fields
|
||||||
|
tool_file_id: str | None = None,
|
||||||
|
upload_file_id: str | None = None,
|
||||||
|
datasource_file_id: str | None = None,
|
||||||
):
|
):
|
||||||
super().__init__(
|
super().__init__(
|
||||||
id=id,
|
id=id,
|
||||||
|
|
|
||||||
|
|
@ -23,3 +23,32 @@ def test_file():
|
||||||
assert file.extension == ".png"
|
assert file.extension == ".png"
|
||||||
assert file.mime_type == "image/png"
|
assert file.mime_type == "image/png"
|
||||||
assert file.size == 67
|
assert file.size == 67
|
||||||
|
|
||||||
|
|
||||||
|
def test_file_model_validate_with_legacy_fields():
|
||||||
|
"""Test `File` model can handle data containing compatibility fields."""
|
||||||
|
data = {
|
||||||
|
"id": "test-file",
|
||||||
|
"tenant_id": "test-tenant-id",
|
||||||
|
"type": "image",
|
||||||
|
"transfer_method": "tool_file",
|
||||||
|
"related_id": "test-related-id",
|
||||||
|
"filename": "image.png",
|
||||||
|
"extension": ".png",
|
||||||
|
"mime_type": "image/png",
|
||||||
|
"size": 67,
|
||||||
|
"storage_key": "test-storage-key",
|
||||||
|
"url": "https://example.com/image.png",
|
||||||
|
# Extra legacy fields
|
||||||
|
"tool_file_id": "tool-file-123",
|
||||||
|
"upload_file_id": "upload-file-456",
|
||||||
|
"datasource_file_id": "datasource-file-789",
|
||||||
|
}
|
||||||
|
|
||||||
|
# Should be able to create `File` object without raising an exception
|
||||||
|
file = File.model_validate(data)
|
||||||
|
|
||||||
|
# The File object does not have tool_file_id, upload_file_id, or datasource_file_id as attributes.
|
||||||
|
# Instead, check it does not expose unrecognized legacy fields (should raise on getattr).
|
||||||
|
for legacy_field in ("tool_file_id", "upload_file_id", "datasource_file_id"):
|
||||||
|
assert not hasattr(file, legacy_field)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue