Update packages, use explicit type, use Microsoft OpenApi package
This commit is contained in:
@@ -3,8 +3,6 @@ using FluentValidation;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Vegasco.WebApi.Authentication;
|
||||
using Vegasco.WebApi.Endpoints;
|
||||
using Vegasco.WebApi.Endpoints.OpenApi;
|
||||
using Vegasco.WebApi.Persistence;
|
||||
|
||||
namespace Vegasco.WebApi.Common;
|
||||
@@ -14,14 +12,12 @@ public static class DependencyInjectionExtensions
|
||||
/// <summary>
|
||||
/// Adds all the WebApi related services to the Dependency Injection container.
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="configuration"></param>
|
||||
/// <param name="environment"></param>
|
||||
/// <param name="builder"></param>
|
||||
public static void AddWebApiServices(this IHostApplicationBuilder builder)
|
||||
{
|
||||
builder.Services
|
||||
.AddMiscellaneousServices()
|
||||
.AddOpenApi()
|
||||
.AddCustomOpenApi()
|
||||
.AddApiVersioning()
|
||||
.AddAuthenticationAndAuthorization(builder.Environment);
|
||||
|
||||
@@ -38,7 +34,6 @@ public static class DependencyInjectionExtensions
|
||||
], ServiceLifetime.Singleton);
|
||||
|
||||
services.AddHealthChecks();
|
||||
services.AddEndpointsFromAssemblyContaining<IWebApiMarker>();
|
||||
|
||||
services.AddHttpContextAccessor();
|
||||
|
||||
@@ -47,32 +42,30 @@ public static class DependencyInjectionExtensions
|
||||
return services;
|
||||
}
|
||||
|
||||
private static IServiceCollection AddOpenApi(this IServiceCollection services)
|
||||
private static IServiceCollection AddCustomOpenApi(this IServiceCollection services)
|
||||
{
|
||||
services.ConfigureOptions<ConfigureSwaggerGenOptions>();
|
||||
|
||||
services.AddEndpointsApiExplorer();
|
||||
services.AddSwaggerGen(o =>
|
||||
services.AddOpenApi(o =>
|
||||
{
|
||||
o.CustomSchemaIds(type =>
|
||||
o.CreateSchemaReferenceId = jsonTypeInfo =>
|
||||
{
|
||||
if (string.IsNullOrEmpty(type.FullName))
|
||||
if (string.IsNullOrEmpty(jsonTypeInfo.Type.FullName))
|
||||
{
|
||||
return type.Name;
|
||||
return jsonTypeInfo.Type.Name;
|
||||
}
|
||||
|
||||
var fullClassName = type.FullName;
|
||||
string? fullClassName = jsonTypeInfo.Type.FullName;
|
||||
|
||||
if (!string.IsNullOrEmpty(type.Namespace))
|
||||
if (!string.IsNullOrEmpty(jsonTypeInfo.Type.Namespace))
|
||||
{
|
||||
fullClassName = fullClassName
|
||||
.Replace(type.Namespace, "")
|
||||
.Replace(jsonTypeInfo.Type.Namespace, "")
|
||||
.TrimStart('.');
|
||||
}
|
||||
|
||||
fullClassName = fullClassName.Replace('+', '_');
|
||||
return fullClassName;
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
return services;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Vegasco.WebApi.Common;
|
||||
@@ -25,7 +26,7 @@ public class FluentValidationOptions<TOptions> : IValidateOptions<TOptions>
|
||||
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
|
||||
var failedValidations = _validators.ValidateAllAsync(options).Result;
|
||||
List<ValidationResult> failedValidations = _validators.ValidateAllAsync(options).Result;
|
||||
if (failedValidations.Count == 0)
|
||||
{
|
||||
return ValidateOptionsResult.Success;
|
||||
|
||||
@@ -45,18 +45,7 @@ internal static class StartupExtensions
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(o =>
|
||||
{
|
||||
// Create a Swagger endpoint for each API version
|
||||
IReadOnlyList<ApiVersionDescription> apiVersions = app.DescribeApiVersions();
|
||||
foreach (ApiVersionDescription apiVersionDescription in apiVersions)
|
||||
{
|
||||
string url = $"/swagger/{apiVersionDescription.GroupName}/swagger.json";
|
||||
string name = apiVersionDescription.GroupName.ToUpperInvariant();
|
||||
o.SwaggerEndpoint(url, name);
|
||||
}
|
||||
});
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
return app;
|
||||
|
||||
@@ -15,7 +15,7 @@ public static class ValidatorExtensions
|
||||
/// <returns>The failed validation results.</returns>
|
||||
public static async Task<List<ValidationResult>> ValidateAllAsync<T>(this IEnumerable<IValidator<T>> validators, T instance, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var validationTasks = validators
|
||||
List<Task<ValidationResult>> validationTasks = validators
|
||||
.Select(validator => validator.ValidateAsync(instance, cancellationToken))
|
||||
.ToList();
|
||||
|
||||
@@ -34,7 +34,7 @@ public static class ValidatorExtensions
|
||||
// Use a hash set to avoid duplicate error messages.
|
||||
Dictionary<string, HashSet<string>> combinedErrors = [];
|
||||
|
||||
foreach (var error in validationResults.SelectMany(x => x.Errors))
|
||||
foreach (ValidationFailure? error in validationResults.SelectMany(x => x.Errors))
|
||||
{
|
||||
if (!combinedErrors.TryGetValue(error.PropertyName, out HashSet<string>? value))
|
||||
{
|
||||
@@ -54,7 +54,7 @@ public static class ValidatorExtensions
|
||||
{
|
||||
builder.Services.AddTransient<IValidateOptions<T>>(serviceProvider =>
|
||||
{
|
||||
var validators = serviceProvider.GetServices<IValidator<T>>() ?? [];
|
||||
IEnumerable<IValidator<T>> validators = serviceProvider.GetServices<IValidator<T>>() ?? [];
|
||||
return new FluentValidationOptions<T>(builder.Name, validators);
|
||||
});
|
||||
return builder;
|
||||
|
||||
Reference in New Issue
Block a user