mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +00:00
Web APi
This commit is contained in:
parent
f6ae6364a9
commit
2e8596e0b7
16 changed files with 588 additions and 1 deletions
|
|
@ -0,0 +1,56 @@
|
|||
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ServerManager.WebApplication.Extensions
|
||||
{
|
||||
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.Any(x => x.MediaType == contentType))
|
||||
{
|
||||
response.Content.Remove(contentType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (operation.Parameters is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var parameter in operation.Parameters)
|
||||
{
|
||||
var description = apiDescription.ParameterDescriptions
|
||||
.First(p => p.Name == parameter.Name);
|
||||
|
||||
if (parameter.Description is null)
|
||||
{
|
||||
parameter.Description = description.ModelMetadata?.Description;
|
||||
}
|
||||
|
||||
if (parameter.Schema.Default is null && description.DefaultValue is not null)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(description.DefaultValue, description.ModelMetadata.ModelType);
|
||||
parameter.Schema.Default = OpenApiAnyFactory.CreateFromJson(json);
|
||||
}
|
||||
|
||||
parameter.Required |= description.IsRequired;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue