mirror of
https://github.com/Tribufu/ServerManagers
synced 2026-08-08 12:11:05 +00:00
WebApi Changes
This commit is contained in:
parent
d217c08ab0
commit
33af7cf391
17 changed files with 319 additions and 309 deletions
|
|
@ -0,0 +1,18 @@
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ServerManager.WebApplication.Models.Data;
|
||||
using ServerManager.WebApplication.Services;
|
||||
|
||||
namespace ServerManager.WebApplication.Extensions;
|
||||
|
||||
public static class ServerQueryExtensions
|
||||
{
|
||||
public static IServiceCollection AddServerQueryServices(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
var settings = configuration.GetSectionAs<ServerQuerySettings>();
|
||||
services.AddSingleton(settings);
|
||||
|
||||
services.AddScoped<IServerQueryService, QueryMasterService>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace ServerManager.WebApplication.Extensions;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static T GetSectionAs<T>(this IConfiguration configuration, string sectionName = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(sectionName))
|
||||
sectionName = typeof(T).Name;
|
||||
|
||||
return configuration.GetSection(sectionName).Get<T>();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,56 +0,0 @@
|
|||
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