Skip to content

Commit 5e955bf

Browse files
committed
swagger doc dispaly
swagger auto dispaly API version
1 parent bf1dcff commit 5e955bf

File tree

1 file changed

+61
-26
lines changed

1 file changed

+61
-26
lines changed

Supermarket/Extensions/ServiceExtensions.cs

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using Microsoft.AspNetCore.Authentication.JwtBearer;
33
using Microsoft.AspNetCore.Builder;
44
using Microsoft.AspNetCore.Identity;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.AspNetCore.Mvc.ApiExplorer;
7+
using Microsoft.AspNetCore.Mvc.Versioning;
58
using Microsoft.EntityFrameworkCore;
69
using Microsoft.Extensions.Configuration;
710
using Microsoft.Extensions.DependencyInjection;
@@ -13,8 +16,11 @@
1316
using Supermarket.Persistent.Context;
1417
using Supermarket.Persistent.Contracts;
1518
using Supermarket.Persistent.Repositories;
19+
using Supermarket.Swagger;
1620
using Swashbuckle.AspNetCore.Swagger;
1721
using System;
22+
using System.Collections.Generic;
23+
using System.Linq;
1824
using System.Text;
1925

2026
namespace Supermarket.Extensions
@@ -66,28 +72,34 @@ public static void ConfigureAutoMapper(this IServiceCollection services)
6672
services.AddAutoMapper();
6773
}
6874

69-
public static void ConfigureSwagger(this IServiceCollection services)
75+
public static void ConfigureSwagger(this IServiceCollection services, Type type)
7076
{
71-
services.AddSwaggerGen(c =>
77+
services.AddSwaggerGen(options =>
7278
{
73-
c.SwaggerDoc("v1", new Info
79+
// Resolve the temprary IApiVersionDescriptionProvider service
80+
var provider = services.BuildServiceProvider().GetRequiredService<IApiVersionDescriptionProvider>();
81+
82+
// Add a swagger document for each discovered API version
83+
foreach (var description in provider.ApiVersionDescriptions)
84+
{
85+
options.SwaggerDoc(description.GroupName, SwaggerInfo.CreateInfoForApiVersion(description, type));
86+
}
87+
88+
// Define the BearerAuth scheme that's in use
89+
options.AddSecurityDefinition("Bearer", new ApiKeyScheme()
90+
{
91+
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
92+
Name = "Authorization",
93+
In = "header",
94+
Type = "apiKey"
95+
});
96+
options.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>>
7497
{
75-
Title = "Supermarket API",
76-
Version = "v1",
77-
Description = "A simple example ASP.NET Core Web API",
78-
TermsOfService = "None",
79-
Contact = new Contact
80-
{
81-
Name = "Mathavan N",
82-
Email = "mathavan@gmail.com",
83-
Url = "https://github.com/Mathavana"
84-
},
85-
License = new License
86-
{
87-
Name = "Use under LICX",
88-
Url = "https://example.com/license"
89-
}
98+
{ "Bearer", Enumerable.Empty<string>() },
9099
});
100+
101+
// Add a custom filter for setting the default values
102+
options.OperationFilter<SwaggerDefaultValues>();
91103
});
92104
}
93105

@@ -111,16 +123,16 @@ public static void ConfigurePasswordPolicy(this IServiceCollection services)
111123
public static void ConfigureAuthentication(this IServiceCollection services, IConfiguration configuration)
112124
{
113125
var key = Encoding.UTF8.GetBytes(configuration["ApplicationSettings:JWT_Secret"].ToString());
114-
services.AddAuthentication(x =>
126+
services.AddAuthentication(options =>
115127
{
116-
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
117-
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
118-
x.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
119-
}).AddJwtBearer(x =>
128+
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
129+
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
130+
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
131+
}).AddJwtBearer(options =>
120132
{
121-
x.RequireHttpsMetadata = false;
122-
x.SaveToken = false;
123-
x.TokenValidationParameters = new TokenValidationParameters
133+
options.RequireHttpsMetadata = false; //only for DEV ENV
134+
options.SaveToken = false;
135+
options.TokenValidationParameters = new TokenValidationParameters
124136
{
125137
ValidateIssuerSigningKey = true,
126138
IssuerSigningKey = new SymmetricSecurityKey(key),
@@ -130,5 +142,28 @@ public static void ConfigureAuthentication(this IServiceCollection services, ICo
130142
};
131143
});
132144
}
145+
146+
public static void ConfigureApiVersioning(this IServiceCollection services)
147+
{
148+
services.AddApiVersioning(options =>
149+
{
150+
options.ReportApiVersions = true;
151+
options.DefaultApiVersion = new ApiVersion(1, 0);
152+
options.AssumeDefaultVersionWhenUnspecified = true;
153+
//options.ApiVersionReader = new MediaTypeApiVersionReader();
154+
options.ApiVersionSelector = new CurrentImplementationApiVersionSelector(options);
155+
});
156+
}
157+
158+
public static void ConfigureVersionedApiExplorer(this IServiceCollection services)
159+
{
160+
services.AddVersionedApiExplorer(options =>
161+
{
162+
//The format of the version added to the route URL
163+
options.GroupNameFormat = "'v'VVV";
164+
//Tells swagger to replace the version in the controller route
165+
options.SubstituteApiVersionInUrl = true;
166+
});
167+
}
133168
}
134169
}

0 commit comments

Comments
 (0)