Skip to content

files_upload_v2 filename required even though content provided #1356

@tirameshu

Description

@tirameshu

Reproducible in:

pip freeze | grep slack
python --version
sw_vers && uname -v # or `ver`

The Slack SDK version

slack-sdk 3.21.2

Python runtime version

Python 3.10.11

OS info

ProductName: macOS
ProductVersion: 12.3
BuildVersion: 21E230
Darwin Kernel Version 21.4.0: Mon Feb 21 20:36:53 PST 2022; root:xnu-8020.101.4~2/RELEASE_ARM64_T8101

Steps to reproduce:

SLACK_TOKEN = XXX
slack_client = slack_sdk.WebClient(SLACK_TOKEN)
slack_client.files_upload_v2(channel="test", content="test")

Expected result:

File uploaded successfully without specifying filename parameter because according to the docstring of files_upload_v2:

        filename: Optional[str] = None,  # you can skip this only when sending along with content parameter

Actual result:

slack_sdk.errors.SlackApiError: The request to the Slack API failed. (url: https://www.slack.com/api/files.getUploadURLExternal)
The server responded with: {'ok': False, 'error': 'invalid_arguments', 'response_metadata': {'messages': ['[ERROR] missing required field: filename']}}

This happens because files_upload_v2 calls _to_v2_file_upload_item which does not create filename if file is None. The resultant files are then passed to files_getUploadURLExternal for which filename is required.

Suggestion

Currently:

def _to_v2_file_upload_item(upload_file: Dict[str, Any]) -> Dict[str, Optional[Any]]:
    ...
    filename = upload_file.get("filename")
    if upload_file.get("filename") is None and isinstance(file, str):
        # use the local filename if filename is missing
        if upload_file.get("filename") is None:  # <- this will always be true
            filename = file.split(os.path.sep)[-1]
        else:
            filename = "Uploaded file"

Change to:

def _to_v2_file_upload_item(upload_file: Dict[str, Any]) -> Dict[str, Optional[Any]]:
    ...
    filename = upload_file.get("filename")
    if filename is None:
        if isinstance(file, str):
            # use the local filename if filename is missing
            filename = file.split(os.path.sep)[-1]
        else:
            filename = "Uploaded file"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Version: 3xbugM-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documentedweb-client

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions