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

UploadedFile: Handle content being a Pathname #210

Merged
merged 2 commits into from
Nov 21, 2017

Conversation

perlun
Copy link
Contributor

@perlun perlun commented Nov 20, 2017

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.

@junaruga
Copy link
Contributor

Great job @perlun !
@scepticulous could you help to review?

@perlun
Copy link
Contributor Author

perlun commented Nov 20, 2017

(For reference, two people have written in #207 that the fix works for them. 🎉)

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)
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

@junaruga junaruga left a 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
Copy link
Contributor

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?

Copy link
Contributor Author

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?

@perlun
Copy link
Contributor Author

perlun commented Nov 21, 2017

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.

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.
@perlun perlun force-pushed the fix/uploaded-file-regression branch from f522560 to bf79482 Compare November 21, 2017 13:21
@perlun perlun merged commit be7392d into master Nov 21, 2017
@perlun perlun deleted the fix/uploaded-file-regression branch November 21, 2017 13:21
@junaruga
Copy link
Contributor

junaruga commented Nov 22, 2017

@perlun Thanks for the releasing! Of course I am fine for the "later" approach.
Sorry I have no energy to add them right now.

@perlun
Copy link
Contributor Author

perlun commented Nov 23, 2017

@> Of course I am fine for the "later" approach.

Sorry I have no energy to add them right now.

That's perfectly fine with me. We're all human.

perlun added a commit that referenced this pull request Dec 27, 2017
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.
perlun added a commit that referenced this pull request Feb 13, 2018
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.
alex-damian-negru pushed a commit to alex-damian-negru/rack-test that referenced this pull request Apr 5, 2021
* 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.
alex-damian-negru pushed a commit to alex-damian-negru/rack-test that referenced this pull request Apr 5, 2021
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants