giraffe-fsharp / Giraffe

A native functional ASP.NET Core web framework for F# developers.
https://giraffe.wiki
Apache License 2.0
2.13k stars 266 forks source link

Fix `ReadBodyFromRequestAsync` disposing `ctx.Request.Body` #615

Closed 64J0 closed 2 months ago

64J0 commented 2 months ago

Description

As noticed by @xperiandri, the ReadBodyFromRequestAsync seems to be incorrectly disposing the ctx.Request.Body. I'm adopting the suggestion from @Banashek, to use the leaveOpen = true parameter.

From the docs:

Unless you set the leaveOpen parameter to true, the StreamReader object calls Dispose() on the provided Stream object when StreamReader.Dispose is called.

How to test

Check the automated tests.

*Thanks @Banashek and @1eyewonder for the comments and ideas.

Related issues

64J0 commented 2 months ago

I'm not sure how to test this properly. Ideas are welcome!

Banashek commented 2 months ago

The original bug report was that the body gets disposed.

I would think that asserting the presence of the body post-use would be a good test.

1eyewonder commented 2 months ago

@64J0 Here is a similar type of scenario I had to add over in FsToolkit.ErrorHandling to deal with dispose issues https://github.com/demystifyfp/FsToolkit.ErrorHandling/pull/271 if you were still looking for some unit test ideas

64J0 commented 2 months ago

Thanks for the ideas folks! I'll work on it during this week