Azure / Azure/azure-functions-host

500 error when [FromBody] object has same name as field of [FromBody] object

Open
#3,071 4 comments 2 reactions 1 assignee Claimed by @soninaren View on GitHub
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 12h
Merged PRs (30d)
38

Description

#### Investigative information

Please provide the following:

- Timestamp:
- Function App version (1.0 or 2.0-beta): 2.0.11888.0 (beta)
- Function App name: ReproBugFunc
- Function name(s) (as appropriate): ReproBugFunction
- Invocation ID: N/A
- Region: EU West

#### Repro steps

Provide the steps required to reproduce the problem:

Create a new Function running on the latest Beta and put this C# code in there:

```
#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static string Run([FromBody]TheClass req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");

return "Hello";
}

public class TheClass
{
//Change this to anything but Req and it works
public string Req { get; set; }
}
```

It now fails with the following error:
`The page cannot be displayed because an internal server error has occurred.`

Digging a bit deeper into the errors when this was actually deployed as a DLL I saw the following:

File: D:\home\LogFiles\Application\Functions\Host\2018-06-28T12-48-06Z-a70831ecb7.log
```
2018-06-28T12:48:10.073 [Error] Error indexing method 'AccountLinking2.Run'
2018-06-28T12:48:10.143 [Warning] Function 'AccountLinking2.Run' failed indexing and will be disabled.
2018-06-28T12:48:10.144 [Debug] Development settings applied
2018-06-28T12:48:10.144 [Warning] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.).
2018-06-28T12:48:10.145 [Information] Host initialized (3890ms)
2018-06-28T12:48:11.788 [Information] Host started (5530ms)
2018-06-28T12:48:11.788 [Information] Job host started
2018-06-28T12:48:11.789 [Error] The following 1 functions are in error:
Run: Microsoft.Azure.WebJobs.Host: Error indexing method 'AccountLinking2.Run'. Microsoft.Azure.WebJobs.Host: Can't bind parameter 'request' to type 'Alexa.NET.Request.SkillRequest'.
```

Sometimes the Streaming logs also give this error:
`2018-06-28T13:49:47.319 [Error] Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.ReproBugFunction'. Microsoft.Azure.WebJobs.Host: Can't bind parameter 'req' to type 'Submission#0+TheClass'.`

#### Expected behavior

It shouldn't matter what the name is of fields/properties in the Request object.

#### Actual behavior

An exception occurs if the name of the input field [FromBody] is the same as the name of the property inside the class it refers to.

E.g. in this example we specify: `[FromBody] TheClass req`
If the field in TheClass matches req then the Function will fail:

```
public class TheClass
{
//Change this to anything but Req and it works
public string Req { get; set; }
}
```

#### Known workarounds

Rename the field Req to anything else in TheClass

#### Related information

Provide any related information

* Programming language used: C#

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.