Swashbuckle.aspnetcore: SwaggerParameter not generating data for FromBody parameter

Created on 19 Mar 2020  路  7Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

I'm currently using Swashbuckle.AspNetCore and Swashbuckle.AspNetCore.Annotations both in version 5.1.0.

I have set up a parameter annotation like so:
``` c#
public async Task> SetRuleParts(
[SwaggerParameter("The import ID of the group to set the rule parts to", Required = true)]
string groupImportId,
[FromBody, SwaggerParameter("A list of ruleParts", Required = true)]
List ruleParts)
{

However the description for the `[FromBody]` parameter is not generated:

"requestBody": {
      "content": {
        "application/json": {
          "schema": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/CreateRulePartDto"
            },
            "nullable": true
          }
        },
        "text/json": {
          "schema": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/CreateRulePartDto"
            },
            "nullable": true
          }
        },
        "application/*+json": {
          "schema": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/CreateRulePartDto"
            },
            "nullable": true
          }
        }
      }
    },

```

Most helpful comment

Thanks for the hint but that's not always possible.

All 7 comments

The SwaggerParameterAttribute is for enriching the generated parameters in the Swagger/OpenAPI document whereas you're wanting to enrich the requestBody. It seems the Annotations library should have a SwaggerRequestBodyAttribute to fill the gap you've identified. Interested in subnmitting a PR for this? (hint: A custom IRequestBodyFilter would make this very simple).

I thought it was already in it, as it works with eg FromQuery. I can have a look on the weekend if I can submit a PR!

[FromQuery] is generated as a "parameter" in the generated Swagger/OpenAPI document, hence why the SwaggerParameterAttribute works in that case. [FromBody] on the other hand is generated as a "requestBody". In other words, the SwaggerParameterAttribute is only applicable for _C# parameters_ (note the distinction) that are ultimately mapped to a "parameter" in the generated Swagger/OpenAPI document.

A solution for this is now merged to master and queued up for the v5.3.0 release (due next week). There's now a SwaggerRequestBodyAttribute that can be applied to "body" bound parameters OR properties:

public async Task<ActionResult<DisplayGroupDto>> SetRuleParts(
    [SwaggerParameter("The import ID of the group to set the rule parts to", Required = true)]
    string groupImportId,
    [FromBody, SwaggerRequestBodyParameter("A list of ruleParts", Required = true)]
    List<CreateRulePartDto> ruleParts)
{
    ...
}

To use this you'll have to have the Annotations package installed and explicitly opted-in as described here

Sweet, thanks!

You might want to avoid relying on FromBody and other such parameter binding attributes and instead use the ApiController attribute which does binding source parameter inference. It is much easier.

https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.1#apicontroller-attribute

Thanks for the hint but that's not always possible.

Was this page helpful?
0 / 5 - 0 ratings