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

MultipartUtils throws NPE/IllegalStateException when request with expected key has no file associated #1152

Closed
ghn1712 opened this issue Jan 25, 2021 · 5 comments

Comments

@ghn1712
Copy link
Contributor

ghn1712 commented Jan 25, 2021

Actual behavior (the bug)
An IllegalStateException caused by null value not allowed is thrown when calling Context.uploadedFile(filename)

Expected behavior
Context.uploadedFile(fileName) should return null and the user decides what to do with the file not present

To Reproduce
Create a server with

    javalin.routes(() -> post("validation", ctx -> Optional.ofNullable(ctx.uploadedFile("filename"))
        .map(file -> "test")
        .map(ctx::result)
        .orElseThrow(() -> new BadRequestResponse("No file sent")))).start()

Send a multipart request containing key "filename" but with no file associated with that key. Instead of returning 400 the server throws 500 and logs the NPE/IllegalStateException.

Additional context
I could not find a way to do the request using a Java lib HTTP client or Curl, but via Postman that was possible

@tipsy
Copy link
Member

tipsy commented Jan 28, 2021

Thanks @ghn1712, I agree that it should work how you describe it.
Would you like to submit a PR? There are tests that cover multipart in the repo.

@ghn1712
Copy link
Contributor Author

ghn1712 commented Jan 28, 2021

@tipsy Sure, I don't have time to look into it this weekend but probably next weekend I can.

@tipsy
Copy link
Member

tipsy commented Jan 28, 2021

Great!

@rafalsiwiec
Copy link
Contributor

rafalsiwiec commented Feb 14, 2021

@tipsy the problem is caused by the fact that when no file is selected in Postman, there's no Content-Type header for that part (which causes instantiation of UploadedFile to throw IllegalArgumentException because contentType property is null), raw body looks like this:

----------------------------837132855102110751896960
Content-Disposition: form-data; name="not-a-file"; filename=""


----------------------------837132855102110751896960--

Whereas upload of an empty text file results in:

----------------------------003788231545526856351775
Content-Disposition: form-data; name="not-a-file"; filename="test.txt"
Content-Type: text/plain


----------------------------003788231545526856351775--

And now, the question is how this should be handled? I don't see any better option than using an empty string as a fallback, when contentType is null. WDYT?

@GitRowin
Copy link
Contributor

GitRowin commented Mar 6, 2021

I ran into the same issue around a week ago and my merged pull request should fix it: #1188

@tipsy tipsy closed this as completed Mar 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants