-
Notifications
You must be signed in to change notification settings - Fork 251
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
UploadedFile: Handle content being a Pathname #210
Conversation
Great job @perlun ! |
(For reference, two people have written in #207 that the fix works for them. 🎉) |
lib/rack/test/uploaded_file.rb
Outdated
def initialize(content, content_type = 'text/plain', binary = false, original_filename: nil) | ||
if content.respond_to?(:read) | ||
if content.respond_to?(:read) && !content.is_a?(Pathname) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if content.respond_to?(:read) && (content.is_a?(IO) || content.is_a?(StringIO))
is more safety code in case of that we added new class for content?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion! Will apply this idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Right now spec/rack/test/uploaded_file_spec.rb #initialize unit test case is only for content: StringIO. Shall we add test cases for IO, Pathname and String? As this PR is urgent, it's up to you whether we add those later.
@@ -18,8 +19,16 @@ class UploadedFile | |||
# The content type of the "uploaded" file | |||
attr_accessor :content_type | |||
|
|||
# Creates a new UploadedFile instance. | |||
# | |||
# @param content [IO, Pathname, String, StringIO] a path to a file, or an {IO} or {StringIO} object representing the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@param content [IO, Pathname, String, StringIO] a path to a file
Is this correct?
For example, IO is used for both a path to a file and file object, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pathname
does not derive from IO
, or how do you mean?
I think we go with the "later" approach for now. But feel free to add them if you like, and have time & energy for it. |
Dynamic languages... Can't live with them, can't live without them. In #149, we added support for `UploadedFile` being an `StringIO `object, by means of the `if content.respond_to?(:read)` logic. _However_, that had the unintended consequence of breaking the use case when a `Pathname` is provided instead of a string as the `content` paramer, since a `Pathname` _also_ responds to the `read` message... This PR works around that, by adding some extra checking. As an added bonus, I also added some YARD comments to make it more clear what parameter types this method expects/accepts. Fixes #207.
f522560
to
bf79482
Compare
@perlun Thanks for the releasing! Of course I am fine for the "later" approach. |
@> Of course I am fine for the "later" approach.
That's perfectly fine with me. We're all human. |
In #210, we tried to fix this but the fix was perhaps not really good enough... So let's relax this check a bit and hopefully unbreak these use cases.
In #210, we tried to fix this but the fix was perhaps not really good enough... So let's relax this check a bit and hopefully unbreak these use cases.
* UploadedFile: Handle content being a Pathname Dynamic languages... Can't live with them, can't live without them. In rack#149, we added support for `UploadedFile` being an `StringIO `object, by means of the `if content.respond_to?(:read)` logic. _However_, that had the unintended consequence of breaking the use case when a `Pathname` is provided instead of a string as the `content` paramer, since a `Pathname` _also_ responds to the `read` message... This PR works around that, by adding some extra checking. As an added bonus, I also added some YARD comments to make it more clear what parameter types this method expects/accepts. Fixes rack#207. * Peer suggestion: Invert check to make the code safer.
In rack#210, we tried to fix this but the fix was perhaps not really good enough... So let's relax this check a bit and hopefully unbreak these use cases.
Dynamic languages... Can't live with them, can't live without them.
In #149, we added support for
UploadedFile
being anStringIO
object, by means of theif content.respond_to?(:read)
logic. However, that had the unintended consequence of breaking the use case when aPathname
is provided instead of a string as thecontent
paramer, since aPathname
also responds to theread
message...This PR works around that, by adding some extra checking. As an added bonus, I also added some YARD comments to make it more clear what parameter types this method expects/accepts.
Fixes #207.