question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to document Swagger/Swashbuckle parameter descriptions when using [FromQuery]

See original GitHub issue

In a standard .NET Core 2.2 Api project with Swashbuckle.AspNetCore 5.0.0-rc2:

[HttpGet]
public ActionResult GetSomeData([FromQuery] SomeDataRequest request) {
    return File(returnImage(), "image/png");
}

The definition of SomeDataRequest


public class SomeDataRequest {
  /// <summary>
  /// Description 1
  /// </summary>
  [Description("description 1")]
  public string foo;

  /// <summary>
  /// Description 2
  /// </summary>
  [Description("description 2")]
  public string bar;
}

When I bring up the Swagger UI, it doesn’t show any kind of descriptions for the properties of SomeDataRequest.

As you can see I tried the Description attribute and the XML comments. Nothing seems to work.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
rgelbcommented, Sep 15, 2021

@davidkmitrais So let’s say you have 2 projects.

api.csproj – this project has Controller actions for your API entities.csproj – this project contains entities that you either pass in or return to/from the API.

You generate the XML documentation file for both, which results in api.xml and entities.xml. These files get placed in their respective bin/debug directories. And then the Startup.cs in your Api.cs has Swagger declaration to read the xml documentation:

services.AddSwaggerGen(c => {
    c.SwaggerDoc("v1", new Info { Title = "Widgets Images", Version = "v1" });
    var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
    var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
    c.IncludeXmlComments(xmlPath);
    ...

You can see the problem here, right? It’s only getting api.xml because that it’s in the executing directory. So you need to somehow bring entities.xml to the same directory. To do, that I defined a post-build step for the api project:

copy /Y $(ProjectDir)..\entities\bin\$(ConfigurationName)\netstandard2.0\entities.xml $(TargetDir)

You might have to fudge directory names a little bit if you are not using .net core 2.x

Now after every build, entities.xml will be in the same folder as api.xml. The last step is to let Swagger know about. We do this by extending AddSwaggerGen code a bit:

var entitiesXmlPath = Path.Combine(AppContext.BaseDirectory, "entities.xml");
c.IncludeXmlComments(entitiesXmlPath);

I hope this helps.

2reactions
utarncommented, Feb 4, 2022

@rgelb Thanks! I just add GenerateDocumentationFile to all of referencing .csproj files without post build setting. Once the main project is compiled, the all the xml files are copied to the same directory as the main project is.

  <PropertyGroup>  
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
    <NoWarn>$(NoWarn);1591</NoWarn>
  </PropertyGroup>  
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Swashbuckle parameter descriptions
I'm using SwaggerResponse attributes to decorate my api controller actions, this all works fine, however when I look at the generated ...
Read more >
Multiple example for parameters in Swagger with ASP.NET ...
A Short guide on how to add multiple example of route parameter in swagger doc.
Read more >
Complex GET query param objects in Swashbuckle
Instead of listing these few parameters as separate query params, it's much easier to just go Filter([FromQuery]PagedCollectionFilter filter) ...
Read more >
Swashbuckle.AspNetCore
AspNetCore. In the ConfigureServices method of Startup.cs , register the Swagger generator, defining one or more Swagger documents. using Microsoft.OpenApi.
Read more >
How to make API documentation using Swagger and ReDoc
In this tutorial, I will show you how to generate API documentation using Swagger and ReDoc for your ASP.NET Core Web API (.NET...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found