I have an API method with this signature:
public async Task<IActionResult> PostCompanies([FromBody] List<Company> companies)
...and the auto generated swagger docs show that the JSON should be a plain array:
[
{
"fuelSiteId":228972,
"name": "foo"
},
{
"fuelSiteId":300000010,
"name": "bar"
}
]
However, if I post this back, it doesn't work and I get the error:
"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Company' because the type requires a JSON object"
The fix is to add the array to a dummy property:
{
data: [
{
"fuelSiteId":228972,
"name": "foo"
},
{
"fuelSiteId":300000010,
"name": "bar"
}
]
}
But everything I'm finding on this site implies this shouldn't be necessary and isn't how you'd normally do it in REST. Additionally, it means what the auto-generated docs say isn't actually what should be posted!
Which is wrong? My API code or the auto-gen Swagger docs or something else?
Ideally I would prefer my API to accept the 'plain' version as this seems more standard and more natural.