Skip to content

Commit 059df3f

Browse files
authored
Merge pull request #588 from aspnetboilerplate/pr/6002
allow to show summaries on swagger
2 parents 6a056d2 + 4a6f3e3 commit 059df3f

File tree

5 files changed

+67
-42
lines changed

5 files changed

+67
-42
lines changed

aspnet-core/src/AbpCompanyName.AbpProjectName.Application/AbpCompanyName.AbpProjectName.Application.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
1111
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
1212
<RootNamespace>AbpCompanyName.AbpProjectName</RootNamespace>
13+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1314
</PropertyGroup>
1415

1516
<ItemGroup>

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Core/AbpCompanyName.AbpProjectName.Web.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<OpenApiGenerateDocuments>false</OpenApiGenerateDocuments>
1818
<RootNamespace>AbpCompanyName.AbpProjectName</RootNamespace>
1919
<LangVersion>7.2</LangVersion>
20+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
2021
</PropertyGroup>
2122

2223
<ItemGroup>

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Host/AbpCompanyName.AbpProjectName.Web.Host.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
1212
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
1313
<LangVersion>7.2</LangVersion>
14+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1415
</PropertyGroup>
1516

1617
<ItemGroup>

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Host/Startup/Startup.cs

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using Microsoft.Extensions.Hosting;
2020
using Microsoft.OpenApi.Models;
2121
using Newtonsoft.Json.Serialization;
22+
using System.IO;
2223

2324
namespace AbpCompanyName.AbpProjectName.Web.Host.Startup
2425
{
@@ -41,10 +42,7 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
4142
{
4243
//MVC
4344
services.AddControllersWithViews(
44-
options =>
45-
{
46-
options.Filters.Add(new AbpAutoValidateAntiforgeryTokenAttribute());
47-
}
45+
options => { options.Filters.Add(new AbpAutoValidateAntiforgeryTokenAttribute()); }
4846
).AddNewtonsoftJson(options =>
4947
{
5048
options.SerializerSettings.ContractResolver = new AbpMvcContractResolver(IocManager.Instance)
@@ -53,8 +51,6 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
5351
};
5452
});
5553

56-
57-
5854
IdentityRegistrar.Register(services);
5955
AuthConfigurer.Configure(services, _appConfiguration);
6056

@@ -79,51 +75,21 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
7975
);
8076

8177
// Swagger - Enable this line and the related lines in Configure method to enable swagger UI
82-
services.AddSwaggerGen(options =>
83-
{
84-
options.SwaggerDoc(_apiVersion, new OpenApiInfo
85-
{
86-
Version = _apiVersion,
87-
Title = "AbpProjectName API",
88-
Description = "AbpProjectName",
89-
// uncomment if needed TermsOfService = new Uri("https://example.com/terms"),
90-
Contact = new OpenApiContact
91-
{
92-
Name = "AbpProjectName",
93-
Email = string.Empty,
94-
Url = new Uri("https://twitter.com/aspboilerplate"),
95-
},
96-
License = new OpenApiLicense
97-
{
98-
Name = "MIT License",
99-
Url = new Uri("https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/LICENSE"),
100-
}
101-
});
102-
options.DocInclusionPredicate((docName, description) => true);
103-
104-
// Define the BearerAuth scheme that's in use
105-
options.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme()
106-
{
107-
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
108-
Name = "Authorization",
109-
In = ParameterLocation.Header,
110-
Type = SecuritySchemeType.ApiKey
111-
});
112-
});
78+
ConfigureSwagger(services);
11379

11480
// Configure Abp and Dependency Injection
11581
return services.AddAbp<AbpProjectNameWebHostModule>(
11682
// Configure Log4Net logging
11783
options => options.IocManager.IocContainer.AddFacility<LoggingFacility>(
11884
f => f.UseAbpLog4Net().WithConfig(_hostingEnvironment.IsDevelopment()
119-
? "log4net.config"
120-
: "log4net.Production.config"
121-
)
85+
? "log4net.config"
86+
: "log4net.Production.config"
87+
)
12288
)
12389
);
12490
}
12591

126-
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
92+
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
12793
{
12894
app.UseAbp(options => { options.UseAbpRequestLocalization = false; }); // Initializes ABP framework.
12995

@@ -137,7 +103,7 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
137103

138104
app.UseAbpRequestLocalization();
139105

140-
106+
141107
app.UseEndpoints(endpoints =>
142108
{
143109
endpoints.MapHub<AbpCommonHub>("/signalr");
@@ -158,5 +124,58 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
158124
options.DisplayRequestDuration(); // Controls the display of the request duration (in milliseconds) for "Try it out" requests.
159125
}); // URL: /swagger
160126
}
127+
128+
private void ConfigureSwagger(IServiceCollection services)
129+
{
130+
services.AddSwaggerGen(options =>
131+
{
132+
options.SwaggerDoc(_apiVersion, new OpenApiInfo
133+
{
134+
Version = _apiVersion,
135+
Title = "AbpProjectName API",
136+
Description = "AbpProjectName",
137+
// uncomment if needed TermsOfService = new Uri("https://example.com/terms"),
138+
Contact = new OpenApiContact
139+
{
140+
Name = "AbpProjectName",
141+
Email = string.Empty,
142+
Url = new Uri("https://twitter.com/aspboilerplate"),
143+
},
144+
License = new OpenApiLicense
145+
{
146+
Name = "MIT License",
147+
Url = new Uri("https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/LICENSE"),
148+
}
149+
});
150+
options.DocInclusionPredicate((docName, description) => true);
151+
152+
// Define the BearerAuth scheme that's in use
153+
options.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme()
154+
{
155+
Description =
156+
"JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
157+
Name = "Authorization",
158+
In = ParameterLocation.Header,
159+
Type = SecuritySchemeType.ApiKey
160+
});
161+
162+
//add summaries to swagger
163+
bool canShowSummaries = _appConfiguration.GetValue<bool>("Swagger:ShowSummaries");
164+
if (canShowSummaries)
165+
{
166+
var hostXmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
167+
var hostXmlPath = Path.Combine(AppContext.BaseDirectory, hostXmlFile);
168+
options.IncludeXmlComments(hostXmlPath);
169+
170+
var applicationXml = $"AbpCompanyName.AbpProjectName.Application.xml";
171+
var applicationXmlPath = Path.Combine(AppContext.BaseDirectory, applicationXml);
172+
options.IncludeXmlComments(applicationXmlPath);
173+
174+
var webCoreXmlFile = $"AbpCompanyName.AbpProjectName.Web.Core.xml";
175+
var webCoreXmlPath = Path.Combine(AppContext.BaseDirectory, webCoreXmlFile);
176+
options.IncludeXmlComments(webCoreXmlPath);
177+
}
178+
});
179+
}
161180
}
162181
}

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Host/appsettings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@
2121
"Url": "https://localhost:44311/"
2222
}
2323
}
24+
},
25+
"Swagger": {
26+
"ShowSummaries": false
2427
}
2528
}

0 commit comments

Comments
 (0)