How do I annotate my ASP.NET WebAPI actions so that the swagger metadata includes the content-types that my resources support?
Specifically, I want the documentation to show that one of my resources can return the 'original' application/json
and application/xml
but also now returns a new format, application/vnd.blah+json
or +xml
.
1 - Open the Properties dialog for your project, click the "Build" tab and ensure that "XML documentation file" is checked. This will produce a file containing all XML comments at build-time. At this point, any classes or methods that are NOT annotated with XML comments will trigger a build warning.
XML comments Swagger will use the XML file to show comments on swagger UI. To make the XML file, right click on project in Visual Studio and then go to properties. Navigate to build tab and check “XML documentation file” option in output section. Now save the settings and Add a method in Startup.
SwaggerOperation is a useful attribute where you can set the summary, description, id, and the tags of an individual request/route.
Open API and NSwag provide limited supports for enum , however, Swashbuckle supports even less. NSwag does support namespace and enum, however, not worrking well with the Swagger definition file generated by Swashbuckle. AspNet Core 5.0.
Extending @VisualBean's answer.
On a Controller's API method, you could use the code below to set an Attribute like:
[SwaggerResponseContentType(responseType:"application/pdf", Exclusive=true)] public HttpResponseMessage GetAuthorityForm(string id) { ....
Note: 'Exclusive=true' will remove all other content types, otherwise using the new Attribute will add a new Response Content Type in the Swagger UI drop down. It will NOT modify your Controller or API, just the documentation.
SwaggerConfig.cs
GlobalConfiguration.Configuration .EnableSwagger(c => // Set filter to apply Custom Content Types to operations // c.OperationFilter<ResponseContentTypeOperationFilter>();
SwaggerResponseContentTypeAttribute.cs
/// <summary> /// SwaggerResponseContentTypeAttribute /// </summary> [AttributeUsage(AttributeTargets.Method)] public sealed class SwaggerResponseContentTypeAttribute : Attribute { /// <summary> /// SwaggerResponseContentTypeAttribute /// </summary> /// <param name="responseType"></param> public SwaggerResponseContentTypeAttribute(string responseType) { ResponseType = responseType; } /// <summary> /// Response Content Type /// </summary> public string ResponseType { get; private set; } /// <summary> /// Remove all other Response Content Types /// </summary> public bool Exclusive { get; set; } }
ResponseContentTypeOperationFilter.cs
public class ResponseContentTypeOperationFilter : IOperationFilter { public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) { var requestAttributes = apiDescription.GetControllerAndActionAttributes<SwaggerResponseContentTypeAttribute>().FirstOrDefault(); if (requestAttributes != null) { if (requestAttributes.Exclusive) operation.produces.Clear(); operation.produces.Add(requestAttributes.ResponseType); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With