'Swashbuckle and Web Api method with ModelBinder
I have a C# WebAPI project with a method that uses a custom ModelBinder for its input parameter. When I look at the Swagger UI that Swashbuckle generates, rather than having a single "body" parameter, all of the properties' of the method's parameters are listed out separately. If I get rid of the [ModelBinder] attribute and switch to using the [FromBody] attribute, the Swagger UI shows the right thing. I have to use the [ModelBinder] attribute, so switching that out isn't an option.
Is there a way to make Swashbuckle generate the "[FromBody] attribute style" single parameter rather than the multiple parameters that it's doing?
Solution 1:[1]
Ondrej is right. Just chain them:
IHttpActionResult Foo([FromBody][ModelBinder]SomeModel model)
Solution 2:[2]
Want to add that for Swashbuckle the order of attributes is important:
IActionResult Foo([FromBody, ModelBinder(typeof(SomeModelBinder))]SomeModel model)
will produce a correct OpenApi schema with SomeModel ref in requestBody.
But
IActionResult Foo([ModelBinder(typeof(SomeModelBinder)), FromBody]SomeModel model)
will produce an OpenApi schema with SomeModel defined as a query parameter
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Brent Arias |
Solution 2 | tseshevsky |