Swashbuckle.WebApi
Swashbuckle.WebApi copied to clipboard
Swashbuckle Infers Bindings When Binding Inference Is Disabled
VERSION:
- .Net Core 3.0 Web App (.Net Core SDK 3.1.100)
- Swashbuckle 5.0.0-rc4
STEPS TO REPRODUCE:
- Configure AspNetCore to disable binding inference:
services.AddControllers()
.ConfigureApiBehaviorOptions(options =>
{
options.SuppressInferBindingSourcesForParameters = true;
})
- Create an ApiController[Attribute] Controller. There, create an HttpGet[Attribute] Controller Action with a complex object with no bindings:
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/currency")]
public class CurrencyController : BaseController
{
public CurrencyController(IMediator mediator)
: base(mediator)
{
}
[HttpGet]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(typeof(List<CurrencyDto>), StatusCodes.Status200OK)]
public Task<IActionResult> GetCurrenciesAsync(GetCurrenciesRequest request, CancellationToken cancellationToken) => HandleRequestAsync(request, cancellationToken);
}
public class GetCurrenciesRequest : IRequest<List<CurrencyDto>>
{
}
EXPECTED RESULT:
- Swagger UI with no route, query, or body parameters.
- Swagger Doc with no route, query, or body parameters.
ACTUAL RESULT:
- Swagger UI shows a query parameter that is an empty object ("{ }")
- Swagger Gen shows a query parameter that is the "empty" GetCurrenciesRequest model.
{
"openapi": "3.0.1",
"info": {
"title": "v1",
"version": "v1"
},
"paths": {
"/api/v1/currency": {
"get": {
"tags": [
"Currency"
],
"parameters": [
{
"name": "request",
"in": "query",
"schema": {
"$ref": "#/components/schemas/GetCurrenciesRequest"
}
}
],
........
"components": {
"schemas": {
"GetCurrenciesRequest": {
"type": "object",
"additionalProperties": false
},
........
}
........
}
ADDITIONAL DETAILS
It seems Swashbuckle defaults the model binding to Query when no binding attributes are found (correctly so, as AspNetCore does for ApiController attributed Controllers). But AspNetCore allows us to disable this behavior.
Having a complex object that models the entire request (where headers, route params, query params, body are bound to properties in the request model) plays very well when using a mediator for handling requests. Also- not every request has data/parameters to bind to (i.e. "Get All" endpoints for that have no optional filtering).
It would be nice if we can suppress this behavior like AspNetCore allows us to do, either by checking the framework setting "SuppressInferBindingSourcesForParameters " if possible or simply having its own setting. If all else, an adhoc solution (custom swashbuckle service) i can create/inject to suppress this behavior, would be great!
Great library. Cheers, -Chris
Any workaround for this?
+1
- 1