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

Fix exception when uploading a file without an extension #3554

Conversation

SapientGuardian
Copy link
Contributor

@SapientGuardian SapientGuardian commented Dec 5, 2023

Fix UploadButton to use System.IO.Path.GetExtension instead of Substring to support filenames without extensions

🤔 This is a ...

  • New feature
  • Bug fix
  • Site / documentation update
  • Demo update
  • Component style update
  • Bundle size optimization
  • Performance optimization
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Other (about what?)

🔗 Related issue link

#3553

💡 Background and solution

Uploading a file that does not have a file extension using the Upload[button] component results in the following exception being thrown:

blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: ArgumentOutOfRange_Generic_MustBeNonNegative, startIndex, -1 Arg_ParamName_Name, startIndex
      ArgumentOutOfRange_ActualValue, -1
System.ArgumentOutOfRangeException: ArgumentOutOfRange_Generic_MustBeNonNegative, startIndex, -1 Arg_ParamName_Name, startIndex
ArgumentOutOfRange_ActualValue, -1
   at AntDesign.Internal.UploadButton.FileNameChanged(ChangeEventArgs e)
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task , ComponentState )

This is happening because the UploadButton component uses Substring based on the position of the last ., which is -1 when no . is present. This PR fixes this by using the built-in System.IO.Path.GetExtension method instead, which better expresses intent and is not affected by this bug.

There are no existing test cases covering this code path and I struggled to create one, but would be happy to add one with assistance.

📝 Changelog

Language Changelog
🇺🇸 English Fixed exception when uploading a file without an extension
🇨🇳 Chinese 修复了上传没有扩展名的文件时出现的异常

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Changelog is provided or not needed

Copy link

github-actions bot commented Dec 5, 2023

Copy link

codecov bot commented Dec 5, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (316d0aa) 44.38% compared to head (9f37487) 1.53%.
Report is 1 commits behind head on master.

Files Patch % Lines
components/upload/UploadButton.razor.cs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #3554       +/-   ##
==========================================
- Coverage   44.38%   1.53%   -42.85%     
==========================================
  Files         566     566               
  Lines       27174   27114       -60     
  Branches     5576    5554       -22     
==========================================
- Hits        12061     417    -11644     
- Misses      14128   26657    +12529     
+ Partials      985      40      -945     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ElderJames
Copy link
Member

Thanks for contribution @SapientGuardian , this helps a lot! And it would be nice to add tests.

@SapientGuardian
Copy link
Contributor Author

I've updated the PR with a test and changed the commit message to match the standard format.

Copy link
Member

@ElderJames ElderJames left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @SapientGuardian

@ElderJames ElderJames merged commit dade3d0 into ant-design-blazor:master Dec 7, 2023
4 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants