0

I know that similar questions have been asked here but I'm truly at a loss. I currently have around 5ish .csproj files as a part of a web application that all contain XML comments. Proj1 is the only one that is currently populating those comments into the swagger documentation. I can see that .xml files are being created with the correct information in the file paths (ex file created at proj3/bin/Debug/net6.0/proj3.xml). But, as stated, the comments from proj1 are the only ones showing up in Swagger.

I currently have a Directory.Build.props with the following lines:

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
        <GenerateDocumentationFile>true</GenerateDocumentationFile>
        <NoWarn>$(NoWarn);1570;1572;1573;1587;1591;1701;1702;1705;1591</NoWarn>
     </PropertyGroup>

and the following swagger configuration in my startup:

  //  configure swagger API documentation
    builder.Services.AddSwaggerGen(config => {
       config.SwaggerDoc(appVersion, new OpenApiInfo {
           Title = "Proj1 API",
           Version = appVersion,
           Description = "REST API for control of Proj1 data"
       });
       config.SchemaFilter<ResourceSchemaFilter>();
       config.OrderActionsBy(a => $"{a.RelativePath}_{a.HttpMethod}");
       config.CustomSchemaIds(x => x.FullName);

       //  add the xml comments to the swagger generated json
       var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
       var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
       config.IncludeXmlComments(xmlPath);
    });

    //  build the application with all services

    var app = builder.Build();

    if (app.Environment.IsDevelopment()) {

       //  enable swagger documentation
       var swaggerRoute = "api-docs";
       app.UseSwagger(c => {
           c.RouteTemplate = $"{swaggerRoute}/{{documentName}}/swagger.json";
       });
       app.UseSwaggerUI(c => {
           c.SwaggerEndpoint($"/{swaggerRoute}/{appVersion}/swagger.json", $"Proj1 API {appVersion})");
           c.RoutePrefix = swaggerRoute;
       });

I tried adding a $(OutputPath)$(AssemblyName).xml line as another property group as well as trying it as a line under the GenerateDocumentationFile line in the original project group. I've also tried adding the true blurb individually to each .csproj file. Each time, I get the Xml comments from files listed in only 1 of the .csproj files. Any help would be greatly appreciated! Does doing mental laps count as cardio?

2 Answers2

0

I was finally able to find an answer and figured I'd leave this up in case someone else finds it useful:

https://github.com/domaindrivendev/Swashbuckle.WebApi/issues/1387

Basically, use an array of xml file names and then do a foreach (var xmlFile in xmlFiles) on it to get the xmlPath and .IncludeXmlComments if it exists on that path for each of the .csproj files.

Here's what I ended up with, and appears to be working at the moment:

  //  add the xml comments to the swagger generated json
    var xmlFiles = new[] {
        "Proj1.xml",
        "Proj2.xml",
        "Proj3.xml",
        "Proj4.xml"
    };
    foreach (var xmlFile in xmlFiles) {
        var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
        if (File.Exists(xmlPath)) {
            config.IncludeXmlComments(xmlPath);
        }
    }

I can see how this may not be reasonable with an exorbitant amount of project files but it's a suitable interim solution for my current needs.

0

You can find all XML files in your solution using below 2 lines:

List<string> xmlFiles = Directory.GetFiles(AppContext.BaseDirectory,"*.xml",SearchOption.TopDirectoryOnly).ToList();
xmlFiles.ForEach(xmlFile => swaggerGenOptions.IncludeXmlComments(xmlFile)); 

make sure to change the patter used in search (*.xml) if you have XML files rather than those for "documentation comments".