-
Notifications
You must be signed in to change notification settings - Fork 38.5k
MockHttpServletRequest.getReader, getInputStream should each return the same object on repeat calls [SPR-16505] #21048
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
Comments
Juergen Hoeller commented Addressed in a revised fashion along with #21042, also covering our internal copy of |
Andy Wilkinson commented As currently implemented, this change breaks Spring REST Docs. It will, I believe, also break any other tests that rely on reading the body of a request obtained from To document a request body, REST Docs relies on being able to read the body of the request obtained from the I wonder if the improvement being made for this issue could be retained while also allowing the body of a request that's obtained from |
Rossen Stoyanchev commented The InputStream or Reader are just wrappers around the actual byte[] content that mock request was configured with. To access that you can use still |
Andy Wilkinson commented I suspect that REST Docs is using |
Rossen Stoyanchev commented You're right, it looks like we only had them on the response side until recently, #19282. |
Graeme Rocher commented This is a massive breaking change IMO. Previous versions of MockHttpServletRequest allows setting the content of the request to a different value and calling getReader() or getInputStream again to obtain the new content.
IMO if this change is to be retained then the reader should at least be reset when |
Juergen Hoeller commented Graeme Rocher, indeed, we need to reset the stream/reader on |
Graeme Rocher commented Thanks Juergen |
This change is breaking Swagger Request Validator: |
Av Pinzur opened SPR-16505 and commented
A de facto rule of thumb for the Servlet paradigm is that a
ServletRequest
's body content can only be read once. This implies at least a couple limitations:getReader
andgetInputStream
) cannot be used in combination. (See MockHttpServletRequest shouldn't allow calls to both getReader and getInputStream [SPR-16499] #21042.)getReader
orgetInputStream
) will read the body only once.This latter limitation is the subject of this ticket. Although unlike #1, this behavior is not explicitly specified in the interface documentation, major Servlet implementations appear to concur in returning the same object reference for successive calls to either
getReader
orgetInputStream
. This means that it's effectively a bug for application code to attempt to read the body contents twice from two such separate calls.However, the current implementation of
MockHttpServletRequest
constructs a fresh object each time one of these methods is called. Revising this implementation to retain the returned reader or stream reference for successive calls will better reflect real-world implementations and enhance the framework's value by allowing developers to catch one more class of bugs prior to deploying to an actual container.Affects: 4.3.14, 5.0.3
Issue Links:
Referenced from: pull request #1689, and commits 3fc8ec4
0 votes, 5 watchers
The text was updated successfully, but these errors were encountered: