-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to fix swagger docs after versioning
- Loading branch information
1 parent
503c34c
commit 4796889
Showing
3 changed files
with
125 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using Asp.Versioning; | ||
using Microsoft.OpenApi.Any; | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace RestDesign.Services; | ||
|
||
public class SwaggerConfig | ||
{ | ||
public enum VersioningType | ||
{ | ||
None, CustomHeader, QueryString, AcceptHeader | ||
} | ||
|
||
public static String QueryStringParam { get; private set; } = ""; | ||
public static String CustomHeaderParam { get; private set; } = ""; | ||
public static String AcceptHeaderParam { get; private set; } = ""; | ||
|
||
public static VersioningType CurrentVersioningMethod = VersioningType.None; | ||
|
||
public static void UseCustomHeaderApiVersion(string parameterName) | ||
{ | ||
CurrentVersioningMethod = VersioningType.CustomHeader; | ||
CustomHeaderParam = parameterName; | ||
} | ||
|
||
public static void UseQueryStringApiVersion() | ||
{ | ||
QueryStringParam = "api-version"; | ||
CurrentVersioningMethod = VersioningType.QueryString; | ||
} | ||
public static void UseQueryStringApiVersion(string parameterName) | ||
{ | ||
CurrentVersioningMethod = VersioningType.QueryString; | ||
QueryStringParam = parameterName; | ||
} | ||
public static void UseAcceptHeaderApiVersion(String paramName) | ||
{ | ||
CurrentVersioningMethod = VersioningType.AcceptHeader; | ||
AcceptHeaderParam = paramName; | ||
} | ||
} | ||
|
||
public class SwaggerParameterFilters : IOperationFilter | ||
{ | ||
public void Apply(OpenApiOperation operation, OperationFilterContext context) | ||
{ | ||
try | ||
{ | ||
var maps = context.MethodInfo.GetCustomAttributes(true).OfType<MapToApiVersionAttribute>().SelectMany(attr => attr.Versions).ToList(); | ||
var version = maps[0].MajorVersion; | ||
if (SwaggerConfig.CurrentVersioningMethod == SwaggerConfig.VersioningType.CustomHeader && !context.ApiDescription.RelativePath!.Contains("{version}")) | ||
{ | ||
operation.Parameters.Add(new OpenApiParameter { Name = SwaggerConfig.CustomHeaderParam, In = ParameterLocation.Header, Required = false, Schema = new OpenApiSchema { Type = "String", Default = new OpenApiString(version.ToString()) } }); | ||
} | ||
else if (SwaggerConfig.CurrentVersioningMethod == SwaggerConfig.VersioningType.QueryString && !context.ApiDescription.RelativePath!.Contains("{version}")) | ||
{ | ||
operation.Parameters.Add(new OpenApiParameter { Name = SwaggerConfig.QueryStringParam, In = ParameterLocation.Query, Schema = new OpenApiSchema { Type = "String", Default = new OpenApiString(version.ToString()) } }); | ||
} | ||
else if (SwaggerConfig.CurrentVersioningMethod == SwaggerConfig.VersioningType.AcceptHeader && !context.ApiDescription.RelativePath!.Contains("{version}")) | ||
{ | ||
|
||
operation.Parameters.Add(new OpenApiParameter { Name = "Accept", In = ParameterLocation.Header, Required = false, Schema = new OpenApiSchema { Type = "String", Default = new OpenApiString($"application/json;{SwaggerConfig.AcceptHeaderParam}=" + version.ToString()) } }); | ||
|
||
} | ||
|
||
var versionParameter = operation.Parameters.Single(p => p.Name == "version"); | ||
|
||
if (versionParameter != null) | ||
{ | ||
operation.Parameters.Remove(versionParameter); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex.Message); | ||
} | ||
} | ||
} | ||
|
||
public class SwaggerVersionMapping : IDocumentFilter | ||
{ | ||
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) | ||
{ | ||
var pathLists = new OpenApiPaths(); | ||
IDictionary<string, OpenApiPaths> paths = new Dictionary<string, OpenApiPaths>(); | ||
var version = swaggerDoc.Info.Version.Replace("v", "").Replace("version", "").Replace("ver", "").Replace(" ", ""); | ||
foreach (var path in swaggerDoc.Paths) | ||
{ | ||
pathLists.Add(path.Key.Replace("v{version}", swaggerDoc.Info.Version), path.Value); | ||
} | ||
swaggerDoc.Paths = pathLists; | ||
} | ||
} |