Skip to content

Commit 579fe4e

Browse files
authored
Add IncludeXmlCommentsForAssembly convience overload (#2909)
Add IncludeXmlCommentsForAssembly convenience overload.
1 parent 14f29bb commit 579fe4e

5 files changed

Lines changed: 36 additions & 16 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,8 @@ To enhance the generated docs with human-friendly descriptions, you can annotate
540540
);
541541

542542
var filePath = Path.Combine(System.AppContext.BaseDirectory, "MyApi.xml");
543-
c.IncludeXmlComments(filePath);
543+
c.IncludeXmlComments(Assembly.GetExecutingAssembly());
544+
// or c.IncludeXmlComments(typeof(MyController).Assembly));
544545
}
545546
```
546547

src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/SwaggerGenOptionsExtensions.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Reflection;
35
using System.Xml.XPath;
4-
using Microsoft.OpenApi.Models;
6+
using Microsoft.AspNetCore.Authentication;
57
using Microsoft.AspNetCore.Mvc.ApiExplorer;
8+
using Microsoft.OpenApi.Models;
69
using Swashbuckle.AspNetCore.SwaggerGen;
7-
using Microsoft.AspNetCore.Authentication;
810

911
namespace Microsoft.Extensions.DependencyInjection
1012
{
@@ -556,6 +558,26 @@ public static void IncludeXmlComments(
556558
swaggerGenOptions.IncludeXmlComments(() => new XPathDocument(filePath), includeControllerXmlComments);
557559
}
558560

561+
/// <summary>
562+
/// Inject human-friendly descriptions for Operations, Parameters and Schemas based on XML comments
563+
/// from specific Assembly
564+
/// </summary>
565+
/// <param name="swaggerGenOptions"></param>
566+
/// <param name="assembly">Assembly that contains XML Comments</param>
567+
/// <param name="includeControllerXmlComments">
568+
/// Flag to indicate if controller XML comments (i.e. summary) should be used to assign Tag descriptions.
569+
/// Don't set this flag if you're customizing the default tag for operations via TagActionsBy.
570+
/// </param>
571+
public static void IncludeXmlComments(
572+
this SwaggerGenOptions swaggerGenOptions,
573+
Assembly assembly,
574+
bool includeControllerXmlComments = false)
575+
{
576+
swaggerGenOptions.IncludeXmlComments(
577+
Path.Combine(AppContext.BaseDirectory, $"{assembly.GetName().Name}.xml"),
578+
includeControllerXmlComments);
579+
}
580+
559581
/// <summary>
560582
/// Generate polymorphic schemas (i.e. "oneOf") based on discovered subtypes.
561583
/// Deprecated: Use the \"UseOneOfForPolymorphism\" and \"UseAllOfForInheritance\" settings instead

test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsDocumentFilterTests.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
using System.Xml.XPath;
2-
using System.Collections.Generic;
3-
using System.Reflection;
1+
using System.Collections.Generic;
42
using System.IO;
3+
using System.Reflection;
4+
using System.Xml.XPath;
55
using Microsoft.AspNetCore.Hosting;
66
using Microsoft.AspNetCore.Mvc.ApiExplorer;
77
using Microsoft.AspNetCore.Mvc.Controllers;
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.FileProviders;
1010
using Microsoft.OpenApi.Models;
1111
using Xunit;
12-
using Swashbuckle.AspNetCore.TestSupport;
1312

1413
namespace Swashbuckle.AspNetCore.SwaggerGen.Test
1514
{
@@ -144,7 +143,7 @@ public void Ensure_IncludeXmlComments_Adds_Filter_To_Options()
144143
services.AddSwaggerGen(c =>
145144
{
146145
c.IncludeXmlComments(
147-
$"{typeof(FakeControllerWithXmlComments).Assembly.GetName().Name}.xml",
146+
typeof(FakeControllerWithXmlComments).Assembly,
148147
includeControllerXmlComments: true);
149148
});
150149

test/WebSites/Basic/Startup.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using System;
22
using System.Globalization;
3-
using System.IO;
3+
using System.Reflection;
4+
using Basic.Swagger;
45
using Microsoft.AspNetCore.Builder;
56
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.Localization;
68
using Microsoft.Extensions.Configuration;
79
using Microsoft.Extensions.DependencyInjection;
810
using Microsoft.Extensions.Hosting;
911
using Microsoft.OpenApi.Models;
10-
using Microsoft.AspNetCore.Localization;
11-
using Basic.Swagger;
1212

1313
namespace Basic
1414
{
@@ -52,7 +52,7 @@ public void ConfigureServices(IServiceCollection services)
5252
c.SelectDiscriminatorNameUsing((baseType) => "TypeName");
5353
c.SelectDiscriminatorValueUsing((subType) => subType.Name);
5454

55-
c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "Basic.xml"));
55+
c.IncludeXmlComments(Assembly.GetExecutingAssembly());
5656

5757
c.EnableAnnotations();
5858
});

test/WebSites/GenericControllers/Startup.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using System.IO;
1+
using System.Reflection;
22
using Microsoft.AspNetCore.Builder;
33
using Microsoft.AspNetCore.Hosting;
44
using Microsoft.Extensions.Configuration;
55
using Microsoft.Extensions.DependencyInjection;
66
using Microsoft.Extensions.Hosting;
77
using Microsoft.OpenApi.Models;
8-
using GenericControllers.Swagger;
98

109
namespace GenericControllers
1110
{
@@ -27,8 +26,7 @@ public void ConfigureServices(IServiceCollection services)
2726
{
2827
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Test API", Version = "1" });
2928

30-
var xmlCommentsPath = Path.Combine(System.AppContext.BaseDirectory, "GenericControllers.xml");
31-
c.IncludeXmlComments(xmlCommentsPath);
29+
c.IncludeXmlComments(Assembly.GetExecutingAssembly());
3230

3331
//c.OperationFilter<ApplySummariesOperationFilter>();
3432
});

0 commit comments

Comments
 (0)