![]() ![]() You basically need to add the below to the ConfigureServices() method in Startup.cs. Add Swagger Operation Filter and API Versioning to ConfigureServices() in Startup.cs Parameter.Required |= description.IsRequired Ĥ. If ( is null & description.DefaultValue is not null) Parameter.Description = description.ModelMetadata?.Description Var description = (p => p.Name = parameter.Name) Operation.Deprecated |= apiDescription.IsDeprecated() įoreach (var parameter in operation.Parameters) Var apiDescription = context.ApiDescription Public void Apply(OpenApiOperation operation, OperationFilterContext context) Public class SwaggerDefaultValues : IOperationFilter This class implements IOperationFilter which basically performs some transformations on the operation by setting the Description and Schema properties for each of the parameters within the API operation. Create a new class called SwaggerDefaultValues as below. Private static OpenApiInfo CreateOpenApiInfo(ApiVersionDescription description) Options.SwaggerDoc(description.GroupName, CreateOpenApiInfo(description)) Public void Configure(SwaggerGenOptions options)įoreach (var description in _apiVersionDescriptionProvider.ApiVersionDescriptions) => _apiVersionDescriptionProvider = apiVersionDescriptionProvider Public ConfigureSwaggerGenOptions(IApiVersionDescriptionProvider apiVersionDescriptionProvider) Private readonly IApiVersionDescriptionProvider _apiVersionDescriptionProvider Public class ConfigureSwaggerGenOptions : IConfigureOptions Make sure you change the Title property on line 28. This class has a Configure() method which loops through all the API versions and adds a new Swagger doc for each version. Create a new class called ConfigureSwaggerGenOptions as below. Install required NuGet packages into your API project:.Add API versions in Swaggerįollow these below steps to add API versions into your Swagger page. So, using API versions allows you to have two versions running side by side and then allow the clients to migrate over to the new versions when they can. Ideally you want to avoid breaking changes but sometimes it’s not avoidable and you cannot update all the clients at the same time. Okay, so this is quite cool but why would we do this? Basically you may have to make a breaking change but have many consumers of the API. ASP.NET doesn’t do this by default so you have to configure it yourself but no problem, I can show you how. This will allow users to go to the Swagger page and they’ll have a drop down with the different API versions and they can then view the docs for those particular versions. In this post, we’ll talk through how we can add API versions to the Swagger page using ASP.NET 6.0. ![]()
0 Comments
Leave a Reply. |