Files
raven/server/AyaNova/SwaggerDefaultValues.cs

137 lines
5.2 KiB
C#

// namespace AyaNova
// {
// using Microsoft.OpenApi.Models;
// using Swashbuckle.AspNetCore.SwaggerGen;
// using System.Linq;
// using Microsoft.AspNetCore.Authorization;
// //https://github.com/domaindrivendev/Swashbuckle.AspNetCore/releases/tag/v5.0.0-rc3
// /// <summary>
// /// Represents the Swagger/Swashbuckle operation filter used to document the implicit API version parameter.
// /// </summary>
// /// <remarks>This <see cref="IOperationFilter"/> is only required due to bugs in the <see cref="SwaggerGenerator"/>.
// /// Once they are fixed and published, this class can be removed.</remarks>
// public class SwaggerDefaultValues : IOperationFilter
// {
// /// <summary>
// /// Applies the filter to the specified operation using the given context.
// /// </summary>
// /// <param name="operation">The operation to apply the filter to.</param>
// /// <param name="context">The current operation filter context.</param>
// public void Apply(OpenApiOperation operation, OperationFilterContext context)
// {
// // REF: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/412
// // REF: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/pull/413
// foreach (var parameter in operation.Parameters)
// {
// var description = context.ApiDescription.ParameterDescriptions.First(p => p.Name == parameter.Name);
// var routeInfo = description.RouteInfo;
// if (parameter.Description == null)
// {
// parameter.Description = description.ModelMetadata?.Description;
// }
// if (routeInfo == null)
// {
// continue;
// }
// // if (parameter.Default == null)
// // {
// // parameter.Default = routeInfo.DefaultValue;
// // }
// if (parameter.Schema.Default == null) { parameter.Schema.Default = new OpenApiString(description.DefaultValue.ToString()); }
// parameter.Required |= !routeInfo.IsOptional;
// }
// }
// }
namespace AyaNova
{
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Linq;
/// <summary>
/// Represents the Swagger/Swashbuckle operation filter used to document the implicit API version parameter.
/// </summary>
/// <remarks>This <see cref="IOperationFilter"/> is only required due to bugs in the <see cref="SwaggerGenerator"/>.
/// Once they are fixed and published, this class can be removed.</remarks>
public class SwaggerDefaultValues : IOperationFilter
{
/// <summary>
/// Applies the filter to the specified operation using the given context.
/// </summary>
/// <param name="operation">The operation to apply the filter to.</param>
/// <param name="context">The current operation filter context.</param>
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
var apiDescription = context.ApiDescription;
operation.Deprecated |= apiDescription.IsDeprecated();
if (operation.Parameters == null)
{
return;
}
// REF: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/412
// REF: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/pull/413
foreach (var parameter in operation.Parameters)
{
var description = apiDescription.ParameterDescriptions.First(p => p.Name == parameter.Name);
if (parameter.Description == null)
{
parameter.Description = description.ModelMetadata?.Description;
}
if (parameter.Schema.Default == null && description.DefaultValue != null)
{
parameter.Schema.Default = new OpenApiString(description.DefaultValue.ToString());
}
parameter.Required |= description.IsRequired;
}
}
}
}
// public void ConfigureServices(IServiceCollection services)
// {
// #region Swagger
// services
// .AddApiVersioning(options =>
// {
// options.AssumeDefaultVersionWhenUnspecified = true;
// options.DefaultApiVersion = Microsoft.AspNetCore.Mvc.ApiVersion.Parse("8.0");
// options.ReportApiVersions = true;
// });
// services.AddVersionedApiExplorer(o => o.GroupNameFormat = "'v'VVV");
// services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
// services.AddSwaggerGen(
// c =>
// {
// c.OperationFilter<SwaggerDefaultValues>();
// });
// #endregion