Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwaggerRequestExample attribute does not work in ASP.NET MVC 5 (.NET Framework 4.5.2)

I am toying with Swashbuckle.Examples package (3.10.0) in an ASP.NET MVC project. However, I cannot make request examples appear within the UI.

Configuration (SwaggerConfig.cs)

public static void Register()
{
    var thisAssembly = typeof(SwaggerConfig).Assembly;

    GlobalConfiguration.Configuration
        .EnableSwagger(c => {
             c.SingleApiVersion("v1", "TestApp.Web");
             c.IncludeXmlComments(string.Format(@"{0}\bin\TestApp.Web.xml", System.AppDomain.CurrentDomain.BaseDirectory));
             c.OperationFilter<ExamplesOperationFilter>();
             c.OperationFilter<DescriptionOperationFilter>();
             c.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
                })
            .EnableSwaggerUi(c => { });
 }  

Request example classes

public class EchoRequestExample : IExamplesProvider
{
    public object GetExamples()
    {
        return new EchoInput { Value = 7 } ;
    }
}

public class EchoInput
{
    public int Value { get; set; }
}

Action

[HttpGet]
[Route("Echo")]
[CustomApiAuthorize]
[SwaggerRequestExample(typeof(EchoInput), typeof(EchoRequestExample))]
[ResponseType(typeof(EchoServiceModel))]
public HttpResponseMessage Echo([FromUri] EchoInput model)
{
    var ret = new EchoServiceModel
    {
        Username = RequestContext.Principal.Identity.Name,
        Value = value
    };
    return Request.CreateResponse(HttpStatusCode.OK, ret);
}

Swagger UI shows xml comments and output metadata (model and an example containing default values), but shows no request example. I attached to process and EchoRequestExample.GetExamples is not hit.

Question: How to make SwaggerRequestExample attribute work in ASP.NET MVC 5?

Note: Windows identity is used for authorization.

like image 492
Alexei - check Codidact Avatar asked Nov 30 '25 19:11

Alexei - check Codidact


1 Answers

I received an answer from library owner here:

Swagger request examples can only set on [HttpPost] actions

It is not clear if this is a design choice or just a limitation, as I find [HttpGet] examples also relevant.

like image 63
Alexei - check Codidact Avatar answered Dec 04 '25 18:12

Alexei - check Codidact