Swashbuckle.aspnetcore: Modelbinding parameters not binding

Created on 27 Sep 2016  路  9Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

See below for screenshot and code to reproduce. Parameters of type modelbinding do not actually get populated with the value inputted when using the Swagger UI. However, if I make the request from Postman, the controller returns the expected result (and binds the method parameter correctly).

image

using Microsoft.AspNetCore.Mvc;

namespace WebApplication.Controllers
{
    public class Test
    {
        public string Name { get; set; }
        public bool IsPassing { get; set; }
    }

    [Route("api/[controller]")]
    public class TestController : Controller
    {
        [HttpPost]
        public Test CreateTest(Test test)
        {
            return test;
        }
    }
}

Most helpful comment

"in" is a property in the parameter object of Swagger JSON. Values understood by Swagger UI are "query", "header", "path", "formData" and "body" (i.e. not "modelbinding").

The default behavior of ASP.NET Core in model binding is (translated to Swagger language)

  1. formData
  2. path
  3. query

I would suggest adding [FromForm] (formData), [FromRoute] (path) or [FromQuery] (query) depending on what you want to do. Perhaps you can even use all three if you want the default behavior but I don't know if it's possible and how Swashbuckle would handle it.

All 9 comments

Because "in" couldn't be a "modelbinding" in swagger specification. Just add [FromBody] attribute to your test parameter.

I'm not sure what you mean by "in". Can you elaborate?

P.S. the [FromBody] worked, but the input now has to be a JSON blob. This is a slightly different API then before.

"in" is a property in the parameter object of Swagger JSON. Values understood by Swagger UI are "query", "header", "path", "formData" and "body" (i.e. not "modelbinding").

The default behavior of ASP.NET Core in model binding is (translated to Swagger language)

  1. formData
  2. path
  3. query

I would suggest adding [FromForm] (formData), [FromRoute] (path) or [FromQuery] (query) depending on what you want to do. Perhaps you can even use all three if you want the default behavior but I don't know if it's possible and how Swashbuckle would handle it.

@SimonTouchtech your last suggestion (using all of them) won't work because of Swagger schema, but everything else is quite right.

Hmm, I tried using [FromForm] like so:

    [Route("api/[controller]")]
    public class TestController : Controller
    {
        [HttpPost]
        public Test CreateTest([FromForm] Test test)
        {
            return test;
        }
    }

However, the model does not get bound from the swagger ui. Interestingly enough, the Curl command does not include the body:

curl -X POST --header 'Accept: application/json' 'http://localhost:17466/api/Test'

While the swagger ui does not pass in the form fields, if I try the same query from postman, the model is bound.

Here's the swagger ui response:

image

Here's the postman response:

image

That looks like a Swashbuckle bug to me, Parameter type should be "formData" and not "form" if I'm not mistaken (i.e. you're not doing anything wrong and this issue is an actual issue).

Yes this appears to be a dup of #81.

Was this page helpful?
0 / 5 - 0 ratings