Configure ASP.NET for the API to work #7
36
Endpoint/ConfigureSwaggerOptions.cs
Normal file
36
Endpoint/ConfigureSwaggerOptions.cs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
using Microsoft.OpenApi.Models;
|
||||||
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Mirea.Api.Endpoint;
|
||||||
|
|
||||||
|
public class ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider) : IConfigureOptions<SwaggerGenOptions>
|
||||||
|
{
|
||||||
|
public void Configure(SwaggerGenOptions options)
|
||||||
|
{
|
||||||
|
foreach (var description in provider.ApiVersionDescriptions)
|
||||||
|
{
|
||||||
|
options.SwaggerDoc(description.GroupName, CreateInfoForApiVersion(description));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static OpenApiInfo CreateInfoForApiVersion(ApiVersionDescription description)
|
||||||
|
{
|
||||||
|
var info = new OpenApiInfo()
|
||||||
|
{
|
||||||
|
Title = "MIREA Schedule Web API",
|
||||||
|
Version = description.ApiVersion.ToString(),
|
||||||
|
Description = "This API provides a convenient interface for retrieving data stored in the database. Special attention was paid to the lightweight and easy transfer of all necessary data. Made by the Winsomnia team.",
|
||||||
|
Contact = new OpenApiContact { Name = "Author name", Email = "support@winsomnia.net" },
|
||||||
|
License = new OpenApiLicense { Name = "MIT", Url = new Uri("https://opensource.org/licenses/MIT") }
|
||||||
|
};
|
||||||
|
|
||||||
|
if (description.IsDeprecated)
|
||||||
|
info.Description += " This API version has been deprecated.";
|
||||||
|
|
||||||
|
return info;
|
||||||
|
}
|
||||||
|
}
|
55
Endpoint/SwaggerDefaultValues.cs
Normal file
55
Endpoint/SwaggerDefaultValues.cs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
||||||
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
|
using Microsoft.OpenApi.Models;
|
||||||
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace Mirea.Api.Endpoint;
|
||||||
|
|
||||||
|
public class SwaggerDefaultValues : IOperationFilter
|
||||||
|
{
|
||||||
|
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||||||
|
{
|
||||||
|
var apiDescription = context.ApiDescription;
|
||||||
|
operation.Deprecated |= apiDescription.IsDeprecated();
|
||||||
|
|
||||||
|
foreach (var responseType in context.ApiDescription.SupportedResponseTypes)
|
||||||
|
{
|
||||||
|
var responseKey = responseType.IsDefaultResponse ? "default" : responseType.StatusCode.ToString();
|
||||||
|
var response = operation.Responses[responseKey];
|
||||||
|
|
||||||
|
foreach (var contentType in response.Content.Keys)
|
||||||
|
{
|
||||||
|
if (responseType.ApiResponseFormats.All(x => x.MediaType != contentType))
|
||||||
|
{
|
||||||
|
response.Content.Remove(contentType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (operation.Parameters == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var parameter in operation.Parameters)
|
||||||
|
{
|
||||||
|
var description = apiDescription.ParameterDescriptions.First(p => p.Name == parameter.Name);
|
||||||
|
|
||||||
|
parameter.Description ??= description.ModelMetadata?.Description;
|
||||||
|
|
||||||
|
if (parameter.Schema.Default == null &&
|
||||||
|
description.DefaultValue != null &&
|
||||||
|
description.DefaultValue is not DBNull &&
|
||||||
|
description.ModelMetadata is ModelMetadata modelMetadata)
|
||||||
|
{
|
||||||
|
var json = JsonSerializer.Serialize(description.DefaultValue, modelMetadata.ModelType);
|
||||||
|
parameter.Schema.Default = OpenApiAnyFactory.CreateFromJson(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
parameter.Required |= description.IsRequired;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user