Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

files_upload_v2 filename required even though content provided #1356

Closed
tirameshu opened this issue Apr 21, 2023 · 1 comment · Fixed by #1361
Closed

files_upload_v2 filename required even though content provided #1356

tirameshu opened this issue Apr 21, 2023 · 1 comment · Fixed by #1361
Labels
bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented Version: 3x web-client
Milestone

Comments

@tirameshu
Copy link

tirameshu commented Apr 21, 2023

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"
@seratch seratch added bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented web-client Version: 3x and removed untriaged labels Apr 21, 2023
@seratch seratch added this to the 3.21.3 milestone Apr 21, 2023
@seratch
Copy link
Member

seratch commented Apr 21, 2023

Hi @tirameshu, thanks for taking the time to report this issue! It seems that the v2 method needs to be improved for the pattern. We will look into it next week and some fix will be included in the next patch release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented Version: 3x web-client
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants