Closed andersson09 closed 1 week ago
Thanks @andersson09 for reporting the issue.
Same as #344
Quoting Fabio's response on the fix for 344.
As is, this PR introduces a behavior change that could be breaking for customer relying on the existing functionality (typically, these kinds of behavior changes are not made in a minor version). An option to address this is to introduce an overload that takes the status code as an additional argument, keeping the default as-is for backwards compatibility.
So, currently the fix is available as an overload of WriteAsJsonAsync
method where you can explicitly pass the status code.
var responseData = req.CreateResponse(HttpStatusCode.InternalServerError);
var apiResponse = new ApiResponse(responseData.StatusCode, "my error");
await responseData.WriteAsJsonAsync(apiResponse, responseData.StatusCode);
return responseData;
We will consider bringing in the fix(breaking change) in the 2.X release of worker package. Let us know if you have further questions.
Any progress on this please? The status code should not be overwritten when we call WriteAsJsonAsync.
This issue KILLED ME. Easily a solid 2 hours to find. I looked at the code 1000 times. I started ripping out middleware trying to figure out how my Response status code was changing to 200 OK.....
public static async Task<HttpResponseData> CreateProblemDetailsResponseAsync(this HttpRequestData req, ProblemDetails details)
{
var response = req.CreateResponse(HttpStatusCode.BadRequest);
await response.WriteAsJsonAsync(details); //EVIL
return response;
}
This is not intuitive, even if it's a breaking change, this is well worth the change.
Just want to add that this issue has caused me significant wasted time trying to figure out why it kept changing the status code. Should be fixed to be intuitive.
If you aren't going to fix it, maybe make these issues easier to find or put it as a massive "LOOK HERE" in the documentattion so less time is wasted.
It's a bit strange that this is already reported more than 2 years ago and we have had multiple releases of Azure Functions with still this bug inside.
Certainly, it was a surprise to come across this. It's a shame the Single Responsibility Principle wasn't respected here. At the very least, they could rename it to WriteAsJsonAsyncAndSetStatus200, haha.
@kshyju Any update on this ? Shouldn't take 4 years to fix a wrong parameter or add an Obsolete attribute on a function
I faced this issue today :( Do we have any ETA for this?
@RohitRanjanMS when will this be available as a release?
As this is a breaking change, it won't be included in the 1.x package. Next release of 2.x will have this change.
The HttpResponseData WriteAsJsonAsync extension should not be overriding an existing status code that has been set to 200. This is not intuitive at all. Can also see from the comment
This has the potential to cause a lot of critical bugs when developers are writing code like:
and expecting it to return 500 but instead returning 200.