How do I set "Parameter content type" using Swashbuckle?
Asked Answered
G

1

26

My swagger ui shows "Parameter content type" with various entries: "application/json-patch+json", "text/json", "application/json", and "application/*+json".

I only want "application/json".

There's a similar unsolved issue on the repo, which uses this visual (older ui, but same idea):

enter image description here

Is there some way to set this?

Swashbuckle.AspNetCore version 4.0.1
Swashbuckle.AspNetCore.Filters version 4.5.5

Grimona answered 3/5, 2019 at 23:11 Comment(0)
P
47

Use the [Produces] and [Consumes] attributes. Swashbuckle (and others, like NSwag) will convert them into the appropriate Swagger documentation.

The [Consumes] attribute's constructor's first parameter is String contentType:

public ConsumesAttribute ( string contentType, params string[] otherContentTypes );

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.consumesattribute.-ctor?view=aspnetcore-2.2#Microsoft_AspNetCore_Mvc_ConsumesAttribute__ctor_System_String_System_String___

Like so:

[ApiController]
public class MyController : ControllBase
{
    [HttpPost( "/foo/bar" )]
    [Consumes( MediaTypeNames.Application.Json )] // "application/json"
    [Produces( typeof(MyResponseDto) ) ]
    public async Task<IActionResult> Post( [FromBody] MyRequestDto dto )
    {
        // 
    }
}
Positivism answered 3/5, 2019 at 23:18 Comment(2)
is there a global way to set this?Malacology
@Malacology have a look at https://mcmap.net/q/536413/-how-do-i-set-or-remove-the-default-response-content-type-using-swashbuckleIntertwist

© 2022 - 2024 — McMap. All rights reserved.