question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FromForm] and lists are not serialized correctly?

See original GitHub issue

Please see this stackoverflow post: https://stackoverflow.com/questions/55452461/a-list-of-guids-is-empty-when-passed-into-a-model-which-is-used-by-fromform-in

A short summary:

When using the default swagger configuration with a newly created ASP.NET Core 2.2 project and the following code, a [FromForm] model that contains a list will not be filled correctly.

A list of strings will contain 1 value that contains all values passed, but seperated by a comma.

I expect these things to be turned into a list containing x values?

Controller:

[HttpPost("bug")]
public ActionResult<string> Bug([FromForm] BugModel bugModel)
{
    var message = GetMessage(bugModel);

    return Ok(message);
}

Model:

public class BugModel
{
    /// <summary>
    /// If you send a GUID it will not appear in this list
    /// </summary>
    public IEnumerable<Guid> Ids { get; set; }
    
    
    /// <summary>
    /// If you send 3 strings, the list will contain 1 entry with the 3 string comma separated.
    /// </summary>
    public IEnumerable<string> IdsAsStringList { get; set; }
}

Swagger will, with some input values, generate the following CURL statement:

curl -X POST "https://localhost:5001/api/Bug/bug" -H "accept: text/plain" -H "Content-Type: multipart/form-data" -F "Ids="9dfb212a-a215-4991-9452-3ddf90e21ec0","9dfb212a-a215-4991-9452-3ddf90e21ec0"" -F "IdsAsStringList="9dfb212a-a215-4991-9452-3ddf90e21ec0","9dfb212a-a215-4991-9452-3ddf90e21ec0""

But this is wrong, at least for a default generated ASP.NET Core 2.2 project.

The following CURL statement, which has duplicate Ids fields, DOES work:

curl -X POST "https://localhost:5001/api/Bug/bug" -H "accept: text/plain" -H "Content-Type: multipart/form-data" -F "Ids="9dfb212a-a215-4991-9452-3ddf90e21ec0"" -F "Ids="9dfb212a-a215-4991-9452-3ddf90e21ec0"" -F "IdsAsStringList="9dfb212a-a215-4991-9452-3ddf90e21ec0","9dfb212a-a215-4991-9452-3ddf90e21ec0""

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

17reactions
bannarisoftwarescommented, Sep 6, 2021

Need fix for this bug

0reactions
PatrickGhosncommented, Jun 23, 2022

We’ve just encountered this issue. We had to do a workaround like below, but of course this is not usable as every API generation will override this.

 if (ids) {
	// localVarFormParams.append('ids', ids.join(COLLECTION_FORMATS.csv));
	for (let index = 0; index < ids.length; index++) {
		localVarFormParams.append(`ids[${index}]`, ids[index].toString());
	}
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

FromForm not deserializing FormData object to use in the ...
The argument gets successfully populated. I can deserialize the gearItem object just fine manually from the request object: JsonConvert.
Read more >
NET Core - FromForm posted Nested Object is not being sent ...
Coding example for the question NET Core - FromForm posted Nested Object is not being sent to the Microservice as a normal List-.net-core....
Read more >
Asp.net core passing an array of objects to controller
I tried everything but with no result, the method in controller get called but the suposed passed list ist empty. What I have...
Read more >
Model Binding in ASP.NET Core
Learn how model binding in ASP.NET Core works and how to customize its behavior.
Read more >
Serializing objects to URL encoded form data
How to serialize objects or object trees to URL encoded form data (application/x-www-form-urlencoded) in C# to write integration tests with ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found