2

I'm using Swagger with ASP.Net Core 2.1 Web API project. Here's an example controller action method:

[HttpGet]
public string GetString([Required, MaxLength(20)] string name) =>
    $"Hi there, {name}.";

And here's what I get in the Swagger documentation. As you can see, Swagger shows the Required attribute, but not the MaxLength one:

enter image description here

If I use Required and MaxLength attributes on a DTO class that's the argument of a POST action method, then Swagger shows them both:

enter image description here

How can I get Swagger to show MaxLength (and other) validation attributes for query parameters?

Note: I have tried to replace the string name argument with a class that has one string property called name - Swagger produces exactly the same documentation.

Andrew
  • 1,139
  • 1
  • 12
  • 27
  • Which version of Swagger UI do you use? (F12 -> Console -> type `versions` and Enter). Swagger UI 3.14+ has the [`showCommonExtensions`](https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md#display) option to display parameter `maxLength`, but it's off by default and needs to be turned on in the Swagger UI initialization code. – Helen Jun 29 '18 at 08:11
  • Thank you @Helen. I have tried the following. In the `Startup.Confugre()` method where I configure ASP.Net middleware, I replaced `applicationBuilder.UseSwaggerUI();` with `applicationBuilder.UseSwaggerUI(swaggerUiOptions => swaggerUiOptions.ConfigObject.Add("showCommonExtensions", true));`. Unfortunately it made no difference. I'm not sure though if this is the right way to set `showCommonExtensions`. Also I checked the SwaggerUi version - it's `3.16.0` – Andrew Jun 30 '18 at 02:51

1 Answers1

5

In .NET Core, you can use ShowCommonExtensions = true, with given sequence (ConfigObject on top).

public static IApplicationBuilder UseR6SwaggerDocumentationUI(
            this IApplicationBuilder app)
{
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        //Allow to add addition attribute info on doc. like [MaxLength(50)]
        c.ConfigObject = new ConfigObject
        {
            ShowCommonExtensions = true
        };

        c.SwaggerEndpoint("/swagger/v1/swagger.json", "Asptricks.net API");
        c.RoutePrefix = "api_documentation/index";
        c.InjectStylesheet("/swagger-ui/custom.css");
        c.InjectJavascript("/swagger-ui/custom.js");
        c.SupportedSubmitMethods( new[] { SubmitMethod.Patch });

        //Collapse model near example.
        c.DefaultModelExpandDepth(0);

        //Remove separate model definition.
        c.DefaultModelsExpandDepth(-1);
    });

    return app;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Raj kumar
  • 1,275
  • 15
  • 20
  • 1
    Thanks @Raj. I'm using Net Core, and `ConfigObject` is a `JObject`, so I cannot say `new ConfigObject`. But I tried my old code again `ConfigObject.Add("showCommonExtensions", true)`, and that worked now! – Andrew Jun 18 '19 at 12:13
  • 1
    @adiga/Raj `c.ShowCommonExtensions();` inside the `c => {` seems to do the same trick. So I think the new ConfigObject is not necessarry. – Hoarst Nov 03 '20 at 23:10