Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IncludeXmlCommentsForAssembly convience overload #2909

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,8 @@ To enhance the generated docs with human-friendly descriptions, you can annotate
);

var filePath = Path.Combine(System.AppContext.BaseDirectory, "MyApi.xml");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this line could be removed

c.IncludeXmlComments(filePath);
c.IncludeXmlComments(Assembly.GetExecutingAssembly());
// or c.IncludeXmlComments(typeof(MyController).Assembly));
leotsarev marked this conversation as resolved.
Show resolved Hide resolved
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Xml.XPath;
using Microsoft.OpenApi.Models;
martincostello marked this conversation as resolved.
Show resolved Hide resolved
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using Microsoft.AspNetCore.Authentication;

namespace Microsoft.Extensions.DependencyInjection
{
Expand Down Expand Up @@ -556,6 +558,26 @@ public static void IncludeXmlComments(
swaggerGenOptions.IncludeXmlComments(() => new XPathDocument(filePath), includeControllerXmlComments);
}

/// <summary>
/// Inject human-friendly descriptions for Operations, Parameters and Schemas based on XML comments
/// from specific Assembly
/// </summary>
/// <param name="swaggerGenOptions"></param>
/// <param name="assembly">Assembly that contains XML Comments</param>
/// <param name="includeControllerXmlComments">
/// Flag to indicate if controller XML comments (i.e. summary) should be used to assign Tag descriptions.
/// Don't set this flag if you're customizing the default tag for operations via TagActionsBy.
/// </param>
public static void IncludeXmlComments(
this SwaggerGenOptions swaggerGenOptions,
Assembly assembly,
bool includeControllerXmlComments = false)
{
swaggerGenOptions.IncludeXmlComments(
Path.Combine(AppContext.BaseDirectory, $"{assembly.GetName().Name}.xml"),
includeControllerXmlComments);
}

/// <summary>
/// Generate polymorphic schemas (i.e. "oneOf") based on discovered subtypes.
/// Deprecated: Use the \"UseOneOfForPolymorphism\" and \"UseAllOfForInheritance\" settings instead
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System.Xml.XPath;
using System.Collections.Generic;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Xml.XPath;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.OpenApi.Models;
using Xunit;
using Swashbuckle.AspNetCore.TestSupport;

namespace Swashbuckle.AspNetCore.SwaggerGen.Test
{
Expand Down Expand Up @@ -144,7 +143,7 @@ public void Ensure_IncludeXmlComments_Adds_Filter_To_Options()
services.AddSwaggerGen(c =>
{
c.IncludeXmlComments(
$"{typeof(FakeControllerWithXmlComments).Assembly.GetName().Name}.xml",
typeof(FakeControllerWithXmlComments).Assembly,
includeControllerXmlComments: true);
});

Expand Down
8 changes: 4 additions & 4 deletions test/WebSites/Basic/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using Basic.Swagger;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Microsoft.AspNetCore.Localization;
using Basic.Swagger;

namespace Basic
{
Expand Down Expand Up @@ -52,7 +52,7 @@ public void ConfigureServices(IServiceCollection services)
c.SelectDiscriminatorNameUsing((baseType) => "TypeName");
c.SelectDiscriminatorValueUsing((subType) => subType.Name);

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

c.EnableAnnotations();
});
Expand Down
6 changes: 2 additions & 4 deletions test/WebSites/GenericControllers/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System.IO;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using GenericControllers.Swagger;

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

var xmlCommentsPath = Path.Combine(System.AppContext.BaseDirectory, "GenericControllers.xml");
c.IncludeXmlComments(xmlCommentsPath);
c.IncludeXmlComments(Assembly.GetExecutingAssembly());

//c.OperationFilter<ApplySummariesOperationFilter>();
});
Expand Down
Loading