Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swashbuckle Infers Bindings When Binding Inference Is Disabled #1351

Open
ckarcz opened this issue Dec 8, 2019 · 3 comments
Open

Swashbuckle Infers Bindings When Binding Inference Is Disabled #1351

ckarcz opened this issue Dec 8, 2019 · 3 comments

Comments

@ckarcz
Copy link

ckarcz commented Dec 8, 2019

VERSION:

  • .Net Core 3.0 Web App (.Net Core SDK 3.1.100)
  • Swashbuckle 5.0.0-rc4

STEPS TO REPRODUCE:

  1. Configure AspNetCore to disable binding inference:
            services.AddControllers()
                    .ConfigureApiBehaviorOptions(options =>
                    {
                        options.SuppressInferBindingSourcesForParameters = true;
                    })
  1. 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

@divinci
Copy link

divinci commented Apr 29, 2020

Any workaround for this?

@91651
Copy link

91651 commented Jun 22, 2020

+1

@Gavinluo
Copy link

Gavinluo commented Nov 3, 2022

  • 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants