Azure functions HttpTriggers Json deserialization failure should return BadRequest instead of Internal Server error #2710
Labels
area: http
Items related to experience improvements for HTTP triggers
enhancement
New feature or request
Description
Feature request
When using HttpTrigger and a Json de-serialization fails there is a
InvalidOperationException
and it will result inInternal Server Error
.When you compare it to ASP.NET Core, especially when migrating from it to azure functions, this is an unexpected behavior as there it would result in
BadRequest
withValidationProblemDetails
.Details
This example uses ASP.NET Core integration, however same happens with native one.
Assumed:
JsonSerializerOptions
has been set to use JsonStringEnumConverter.When invoked with a json payload that has a value not in the enum get an exception because "Error" is not the deserialization, with the message
System.InvalidOperationException: The Json value could not be converted ....
Additional Information
A few points regarding the feature request:
InvalidOperationException
a middleware cannot help determine if it was a json exception without interpreting the message.FromBodyConversionFeature
would be needed, because a non-unique one likeJsonException
or so just means you still have to find out if the input deserialization is the cause or some other layer that leaked.FromBodyuConversionFeature
has a model state with results, so even aBadRequest
with properValidationProblemsDetails
would be expected.IInputConverter
and a copy ofDefaultFromBodyConversion
, it was noticed that ussingConversionResult.Failed(exception)
actually has no use, as theGeneratedFunctionExecutor.g.cs
just perform asBindFunctionInputAsync
and even if that returns a failed type it means the result isnull
. (Seems weird)JsonStringEnumConverter
, but will all forms of json that fail, it is just used in the example as it is the fastest way to reproduce.Currently the only way to have this into a
badrequest
is either:FromBody
and deserialize manually from HttpRequest(Data), however that does feel like a step backFromBodyAttribute
and either catch the exception fromFromBodyConversionFeature
to a custom one or have copy fromFromBodyConversionFeature
and have it in there thrownIFromBodyConversionFeature
in FunctionContext features is also an option if possible, though not checked nore sure about side effectsThe first option would mean that you potentially have to do that for a lot of http function triggers, while it would feel more out-of-box feature.
The second/third are an option, however you are very dependent on what the sdk will do and very upgrade dependent breaking.
The text was updated successfully, but these errors were encountered: