Azure / Azure/azure-functions-dotnet-worker

HTTP body conversion will throw an InvalidOperationException when the request have an empty body

Open
#2,516 1 comment 0 reactions 0 assignees View on GitHub
area: http needs-investigation
Dominant language
C#
Stars
466
Forks
215
Avg merge
3d 10h
Merged PRs (30d)
7

Description

### Description

HTTP body conversion will throw an InvalidOperationException when the request have an empty body.

* **Fixed in v1.3.2**
~~When using the ASP.NET Core integration, a response is never sent back to the client, the request will hang. (Maybe related to #2476)~~

* With the built-in HTTP type with the default ``System.Text.Json`` serializer, a response with status code 500 will be sent back to the client.

* With the built-in HTTP type with ``Newtonsoft`` as the serializer :

- Sending an empty body will throw and a response with status code 500 will be sent back to the client.
- Sending an empty body with the header ``Content-Type: application/json`` allows the conversion to proceed without any error. The AzureFunction will receive a null object.

* With the in-process model sending an empty body will create an instance of the object.

With the built-in HTTP type I have traced the origin of the issue(s) back to this line of the [DefaultFromBodyConversionFeature](https://github.com/Azure/azure-functions-dotnet-worker/blob/2ece46d8dc04b6b9135c3fd6aef816d6ea4eab8f/extensions/Worker.Extensions.Http/src/DefaultFromBodyConversionFeature.cs#L66).

### Steps to reproduce

This [repository](https://github.com/Tri125/FromBodyExceptionReproCase) has been made available to demonstrate the issue.

Debug each project one by one and send all 3 request to them :

``curl -d '{ "Name": "potato" }' -H "Content-Type: application/json" -X POST http://localhost:7021/api/Function1 -v``

``curl -H "Content-Type: application/json" -X POST http://localhost:7021/api/Function1 -v``

``curl -X POST http://localhost:7021/api/Function1 -v``

This will demonstrate the issues and the difference in behavior between them.

The latest version of the packages have been used :

```
Microsoft.Azure.Core.NewtonsoftJson v2.0.0
Microsoft.Azure.Functions.Worker v1.22.0
Microsoft.Azure.Functions.Worker.Extensions.Http v3.2.0
Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore v1.3.1
Microsoft.Azure.Functions.Worker.Sdk v1.17.2
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with extensions/Worker.Extensions.Http/src/DefaultFromBodyConversionFeature.cs at the linked line, then run the three curl requests from the reproduction repository against the listed HTTP configurations. Confirm whether the empty-body conversion still throws or returns a 500 response; the issue states this was fixed in v1.3.2, so the current done criteria are not otherwise defined.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.