From 1d2f6f27c00ecfcaf0e57a82a09c908130c290af Mon Sep 17 00:00:00 2001 From: Dan J Date: Mon, 24 Mar 2025 10:25:26 -0500 Subject: [PATCH 1/3] Updating packages for compatability with Swashbuckle.AspNetCore.SwaggerGen 8 and getting rid of some legacy package dependencies. Looks like Should take care of issues #158 and #160. One issue is the newer versions of Swashbuckle.AspNetCore.SwaggerGen made SwaggerMiddleware internal (see https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2805). This breaks the scoped middleware, not sure if this is still needed as I have not used in a project --- samples/MinimalApi/MinimalApi.csproj | 11 ++-- .../SampleNSwagWebApi.csproj | 6 +-- samples/SampleNSwagWebApi/Startup.cs | 15 +++--- samples/SampleWebApi/Controllers/Issue100.cs | 2 +- samples/SampleWebApi/SampleWebApi.csproj | 14 ++--- ...icroElements.NSwag.FluentValidation.csproj | 43 +++++++++------- .../NSwagRuleContext.cs | 2 +- .../NSwagSchemaProcessorContext.cs | 3 +- ...roElements.OpenApi.FluentValidation.csproj | 49 ++++++++++-------- ...hbuckle.FluentValidation.AspNetCore.csproj | 19 +++---- ...ements.Swashbuckle.FluentValidation.csproj | 7 +-- .../Swashbuckle/ScopedSwaggerMiddleware.cs | 51 ------------------- ....Swashbuckle.FluentValidation.Tests.csproj | 8 +-- 13 files changed, 98 insertions(+), 132 deletions(-) delete mode 100644 src/MicroElements.Swashbuckle.FluentValidation/Swashbuckle/ScopedSwaggerMiddleware.cs diff --git a/samples/MinimalApi/MinimalApi.csproj b/samples/MinimalApi/MinimalApi.csproj index fa11ff0..6622298 100644 --- a/samples/MinimalApi/MinimalApi.csproj +++ b/samples/MinimalApi/MinimalApi.csproj @@ -7,10 +7,13 @@ - - - - + + + + + + + diff --git a/samples/SampleNSwagWebApi/SampleNSwagWebApi.csproj b/samples/SampleNSwagWebApi/SampleNSwagWebApi.csproj index e9eca62..5c7b436 100644 --- a/samples/SampleNSwagWebApi/SampleNSwagWebApi.csproj +++ b/samples/SampleNSwagWebApi/SampleNSwagWebApi.csproj @@ -7,11 +7,11 @@ - + - - + + diff --git a/samples/SampleNSwagWebApi/Startup.cs b/samples/SampleNSwagWebApi/Startup.cs index 4f71e2f..9645b08 100644 --- a/samples/SampleNSwagWebApi/Startup.cs +++ b/samples/SampleNSwagWebApi/Startup.cs @@ -15,20 +15,17 @@ public void ConfigureServices(IServiceCollection services) // HttpContextValidatorRegistry requires access to HttpContext services.AddHttpContextAccessor(); - services - .AddControllers() - // Adds fluent validators to Asp.net - .AddFluentValidation(c => - { - c.RegisterValidatorsFromAssemblyContaining(includeInternalTypes: true); - }); + services.AddControllers(); + + // Adds fluent validators to Asp.net + services.AddFluentValidationAutoValidation().AddFluentValidationClientsideAdapters(); services.AddOpenApiDocument((settings, serviceProvider) => { var fluentValidationSchemaProcessor = serviceProvider.CreateScope().ServiceProvider.GetService(); // Add the fluent validations schema processor - settings.SchemaProcessors.Add(fluentValidationSchemaProcessor); + settings.SchemaSettings.SchemaProcessors.Add(fluentValidationSchemaProcessor); }); // Register FV validators @@ -49,7 +46,7 @@ public void Configure(IApplicationBuilder app) }); app.UseOpenApi(); // serve OpenAPI/Swagger documents - app.UseSwaggerUi3(); // serve Swagger UI + app.UseSwaggerUi(); // serve Swagger UI } } } diff --git a/samples/SampleWebApi/Controllers/Issue100.cs b/samples/SampleWebApi/Controllers/Issue100.cs index b6dd6a0..92a0cb2 100644 --- a/samples/SampleWebApi/Controllers/Issue100.cs +++ b/samples/SampleWebApi/Controllers/Issue100.cs @@ -68,7 +68,7 @@ public Person100Validator() [SwaggerDiscriminator("discriminator")] [SwaggerSubType(typeof(Person100A), DiscriminatorValue = Person100A.DiscriminatorValue)] [SwaggerSubType(typeof(Person100B), DiscriminatorValue = Person100B.DiscriminatorValue)] - [JsonConverter(typeof(JsonInheritanceConverter), "discriminator")] + [JsonConverter(typeof(JsonInheritanceConverter), "discriminator")] [JsonInheritance(Person100A.DiscriminatorValue, typeof(Person100A))] [JsonInheritance(Person100B.DiscriminatorValue, typeof(Person100B))] public abstract class BasePerson100 diff --git a/samples/SampleWebApi/SampleWebApi.csproj b/samples/SampleWebApi/SampleWebApi.csproj index 5660591..4c79ab2 100644 --- a/samples/SampleWebApi/SampleWebApi.csproj +++ b/samples/SampleWebApi/SampleWebApi.csproj @@ -7,15 +7,15 @@ - + - - - - - - + + + + + + diff --git a/src/MicroElements.NSwag.FluentValidation/MicroElements.NSwag.FluentValidation.csproj b/src/MicroElements.NSwag.FluentValidation/MicroElements.NSwag.FluentValidation.csproj index a43100e..51da5e3 100644 --- a/src/MicroElements.NSwag.FluentValidation/MicroElements.NSwag.FluentValidation.csproj +++ b/src/MicroElements.NSwag.FluentValidation/MicroElements.NSwag.FluentValidation.csproj @@ -1,24 +1,29 @@ - - - netstandard2.0 - enable - latest - true - false - Adds FluentValidation rules to swagger schema. - Swagger NSwag OpenApi FluentValidation - - - - - - - + + Library + net8.0;net9.0 + enable + latest + true + false + Adds FluentValidation rules to swagger schema. + Swagger NSwag OpenApi FluentValidation + - - - + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/MicroElements.NSwag.FluentValidation/NSwagRuleContext.cs b/src/MicroElements.NSwag.FluentValidation/NSwagRuleContext.cs index 5635f25..9e0084e 100644 --- a/src/MicroElements.NSwag.FluentValidation/NSwagRuleContext.cs +++ b/src/MicroElements.NSwag.FluentValidation/NSwagRuleContext.cs @@ -27,7 +27,7 @@ public SchemaProcessorContext Property get { return new SchemaProcessorContext( - Schema.Type, + Schema.ContextualType, Schema.Schema.Properties[PropertyKey], Schema.Resolver, Schema.Generator, diff --git a/src/MicroElements.NSwag.FluentValidation/NSwagSchemaProcessorContext.cs b/src/MicroElements.NSwag.FluentValidation/NSwagSchemaProcessorContext.cs index 49c1c9f..a995b7e 100644 --- a/src/MicroElements.NSwag.FluentValidation/NSwagSchemaProcessorContext.cs +++ b/src/MicroElements.NSwag.FluentValidation/NSwagSchemaProcessorContext.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using FluentValidation.Validators; using MicroElements.OpenApi.FluentValidation; +using Namotion.Reflection; using NJsonSchema.Generation; namespace MicroElements.NSwag.FluentValidation @@ -77,7 +78,7 @@ public SchemaProcessorContext GetSchemaForType(Type type) { var schemaForType = _processorContext.Resolver.GetSchema(type, isIntegerEnumeration: false); return new SchemaProcessorContext( - type, + type.ToContextualType(), schemaForType, _processorContext.Resolver, _processorContext.Generator, diff --git a/src/MicroElements.OpenApi.FluentValidation/MicroElements.OpenApi.FluentValidation.csproj b/src/MicroElements.OpenApi.FluentValidation/MicroElements.OpenApi.FluentValidation.csproj index 15a40e4..459ce4a 100644 --- a/src/MicroElements.OpenApi.FluentValidation/MicroElements.OpenApi.FluentValidation.csproj +++ b/src/MicroElements.OpenApi.FluentValidation/MicroElements.OpenApi.FluentValidation.csproj @@ -1,22 +1,31 @@ - - - netstandard2.0 - enable - latest - true - MicroElements.OpenApi - - - - - - - - - - - - - + + + Library + net8.0;net9.0 + enable + latest + true + MicroElements.OpenApi + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/MicroElements.Swashbuckle.FluentValidation.AspNetCore/MicroElements.Swashbuckle.FluentValidation.AspNetCore.csproj b/src/MicroElements.Swashbuckle.FluentValidation.AspNetCore/MicroElements.Swashbuckle.FluentValidation.AspNetCore.csproj index 1b86d3d..a322b21 100644 --- a/src/MicroElements.Swashbuckle.FluentValidation.AspNetCore/MicroElements.Swashbuckle.FluentValidation.AspNetCore.csproj +++ b/src/MicroElements.Swashbuckle.FluentValidation.AspNetCore/MicroElements.Swashbuckle.FluentValidation.AspNetCore.csproj @@ -1,13 +1,14 @@ - + - - net8.0;net9.0 - enable - enable - + + Library + net8.0;net9.0 + enable + enable + - - - + + + diff --git a/src/MicroElements.Swashbuckle.FluentValidation/MicroElements.Swashbuckle.FluentValidation.csproj b/src/MicroElements.Swashbuckle.FluentValidation/MicroElements.Swashbuckle.FluentValidation.csproj index 96f265b..de98134 100644 --- a/src/MicroElements.Swashbuckle.FluentValidation/MicroElements.Swashbuckle.FluentValidation.csproj +++ b/src/MicroElements.Swashbuckle.FluentValidation/MicroElements.Swashbuckle.FluentValidation.csproj @@ -1,7 +1,8 @@  - netstandard2.0 + Library + net8.0;net9.0 enable latest true @@ -13,9 +14,9 @@ - + - + diff --git a/src/MicroElements.Swashbuckle.FluentValidation/Swashbuckle/ScopedSwaggerMiddleware.cs b/src/MicroElements.Swashbuckle.FluentValidation/Swashbuckle/ScopedSwaggerMiddleware.cs deleted file mode 100644 index 13edcd9..0000000 --- a/src/MicroElements.Swashbuckle.FluentValidation/Swashbuckle/ScopedSwaggerMiddleware.cs +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (c) MicroElements. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Http; -using Swashbuckle.AspNetCore.Swagger; - -namespace MicroElements.Swashbuckle.FluentValidation -{ - /// - /// SwaggerMiddleware that resolves on scope. - /// Resolves problems with validators with dependency on scoped services. - /// - public class ScopedSwaggerMiddleware - { - private readonly RequestDelegate _next; - private readonly SwaggerOptions _options; - - /// - /// ctor. - /// - /// - /// - public ScopedSwaggerMiddleware(RequestDelegate next, SwaggerOptions options) - { - _next = next; - _options = options; - } - - public async Task Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) - { - await new SwaggerMiddleware(_next, _options).Invoke(httpContext, swaggerProvider); - } - } - - public static class SwaggerBuilderExtensions - { - /// - /// Replaces standard with . - /// Use instead of if you have services with scoped services like DbContext. - /// - public static IApplicationBuilder UseScopedSwagger(this IApplicationBuilder app, Action setupAction = null) - { - SwaggerOptions swaggerOptions = new SwaggerOptions(); - setupAction?.Invoke(swaggerOptions); - return app.UseMiddleware(swaggerOptions); - } - } -} \ No newline at end of file diff --git a/test/MicroElements.Swashbuckle.FluentValidation.Tests/MicroElements.Swashbuckle.FluentValidation.Tests.csproj b/test/MicroElements.Swashbuckle.FluentValidation.Tests/MicroElements.Swashbuckle.FluentValidation.Tests.csproj index dca1dfa..7cc184f 100644 --- a/test/MicroElements.Swashbuckle.FluentValidation.Tests/MicroElements.Swashbuckle.FluentValidation.Tests.csproj +++ b/test/MicroElements.Swashbuckle.FluentValidation.Tests/MicroElements.Swashbuckle.FluentValidation.Tests.csproj @@ -8,10 +8,10 @@ - - - - + + + + From b1b020e5c8a9de647bca64b4e3addd1eeab66f77 Mon Sep 17 00:00:00 2001 From: Dan J Date: Wed, 21 May 2025 09:00:42 -0500 Subject: [PATCH 2/3] More package cleanup, remove unused project. Fix sample apps --- MicroElements.Swashbuckle.FluentValidation.sln | 18 ++++++------------ samples/MinimalApi/MinimalApi.csproj | 7 +++---- samples/MinimalApi/Program.cs | 5 ----- .../SampleNSwagWebApi/SampleNSwagWebApi.csproj | 6 ++---- samples/SampleNSwagWebApi/Startup.cs | 4 ---- samples/SampleWebApi/SampleWebApi.csproj | 15 +++++++-------- samples/SampleWebApi/Startup.cs | 4 ---- ...MicroElements.NSwag.FluentValidation.csproj | 5 ++--- ...croElements.OpenApi.FluentValidation.csproj | 9 ++++----- ...shbuckle.FluentValidation.AspNetCore.csproj | 14 -------------- ...lements.Swashbuckle.FluentValidation.csproj | 8 +++++--- ...s.Swashbuckle.FluentValidation.Tests.csproj | 5 ++--- .../SwaggerTestHost.cs | 4 ---- 13 files changed, 31 insertions(+), 73 deletions(-) delete mode 100644 src/MicroElements.Swashbuckle.FluentValidation.AspNetCore/MicroElements.Swashbuckle.FluentValidation.AspNetCore.csproj diff --git a/MicroElements.Swashbuckle.FluentValidation.sln b/MicroElements.Swashbuckle.FluentValidation.sln index a627e33..aef3baf 100644 --- a/MicroElements.Swashbuckle.FluentValidation.sln +++ b/MicroElements.Swashbuckle.FluentValidation.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.29324.140 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36109.1 d17.14 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MicroElements.Swashbuckle.FluentValidation", "src\MicroElements.Swashbuckle.FluentValidation\MicroElements.Swashbuckle.FluentValidation.csproj", "{433D1CD9-A091-43C1-B230-7E25954DA621}" EndProject @@ -32,8 +32,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{9ED7 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MinimalApi", "samples\MinimalApi\MinimalApi.csproj", "{6F4B88CA-B550-4C5E-981A-4608EC254976}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MicroElements.Swashbuckle.FluentValidation.AspNetCore", "src\MicroElements.Swashbuckle.FluentValidation.AspNetCore\MicroElements.Swashbuckle.FluentValidation.AspNetCore.csproj", "{C80E2EF6-6408-4BF3-8245-9F9DD1FD8F50}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -68,20 +66,16 @@ Global {6F4B88CA-B550-4C5E-981A-4608EC254976}.Debug|Any CPU.Build.0 = Debug|Any CPU {6F4B88CA-B550-4C5E-981A-4608EC254976}.Release|Any CPU.ActiveCfg = Release|Any CPU {6F4B88CA-B550-4C5E-981A-4608EC254976}.Release|Any CPU.Build.0 = Release|Any CPU - {C80E2EF6-6408-4BF3-8245-9F9DD1FD8F50}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C80E2EF6-6408-4BF3-8245-9F9DD1FD8F50}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C80E2EF6-6408-4BF3-8245-9F9DD1FD8F50}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C80E2EF6-6408-4BF3-8245-9F9DD1FD8F50}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {1AA0A677-C642-44C8-A6CE-495E7B7074B8} - EndGlobalSection GlobalSection(NestedProjects) = preSolution - {2E20C501-F63A-4CA2-9A9F-8F1BE7BC5E14} = {9ED7D819-FC90-4504-A46D-D38E3BE107B7} {FEDDFEAF-E8D0-4E7D-BCDC-AEFE3517BEAD} = {9ED7D819-FC90-4504-A46D-D38E3BE107B7} + {2E20C501-F63A-4CA2-9A9F-8F1BE7BC5E14} = {9ED7D819-FC90-4504-A46D-D38E3BE107B7} {6F4B88CA-B550-4C5E-981A-4608EC254976} = {9ED7D819-FC90-4504-A46D-D38E3BE107B7} EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {1AA0A677-C642-44C8-A6CE-495E7B7074B8} + EndGlobalSection EndGlobal diff --git a/samples/MinimalApi/MinimalApi.csproj b/samples/MinimalApi/MinimalApi.csproj index 6622298..ce0459b 100644 --- a/samples/MinimalApi/MinimalApi.csproj +++ b/samples/MinimalApi/MinimalApi.csproj @@ -1,4 +1,4 @@ - + net9.0 @@ -7,9 +7,8 @@ - - - + + diff --git a/samples/MinimalApi/Program.cs b/samples/MinimalApi/Program.cs index 749b24c..1121c23 100644 --- a/samples/MinimalApi/Program.cs +++ b/samples/MinimalApi/Program.cs @@ -1,5 +1,4 @@ using FluentValidation; -using FluentValidation.AspNetCore; using MicroElements.Swashbuckle.FluentValidation.AspNetCore; var builder = WebApplication.CreateBuilder(args); @@ -12,10 +11,6 @@ // Add Swagger services.AddSwaggerGen(); -// Add FV -services.AddFluentValidationAutoValidation(); -services.AddFluentValidationClientsideAdapters(); - // Add FV validators services.AddValidatorsFromAssemblyContaining(); diff --git a/samples/SampleNSwagWebApi/SampleNSwagWebApi.csproj b/samples/SampleNSwagWebApi/SampleNSwagWebApi.csproj index 5c7b436..eb72cd4 100644 --- a/samples/SampleNSwagWebApi/SampleNSwagWebApi.csproj +++ b/samples/SampleNSwagWebApi/SampleNSwagWebApi.csproj @@ -7,11 +7,9 @@ - + - - - + diff --git a/samples/SampleNSwagWebApi/Startup.cs b/samples/SampleNSwagWebApi/Startup.cs index 9645b08..2d74438 100644 --- a/samples/SampleNSwagWebApi/Startup.cs +++ b/samples/SampleNSwagWebApi/Startup.cs @@ -1,5 +1,4 @@ using FluentValidation; -using FluentValidation.AspNetCore; using MicroElements.NSwag.FluentValidation; using MicroElements.NSwag.FluentValidation.AspNetCore; using Microsoft.AspNetCore.Builder; @@ -17,9 +16,6 @@ public void ConfigureServices(IServiceCollection services) services.AddControllers(); - // Adds fluent validators to Asp.net - services.AddFluentValidationAutoValidation().AddFluentValidationClientsideAdapters(); - services.AddOpenApiDocument((settings, serviceProvider) => { var fluentValidationSchemaProcessor = serviceProvider.CreateScope().ServiceProvider.GetService(); diff --git a/samples/SampleWebApi/SampleWebApi.csproj b/samples/SampleWebApi/SampleWebApi.csproj index 4c79ab2..5b9d663 100644 --- a/samples/SampleWebApi/SampleWebApi.csproj +++ b/samples/SampleWebApi/SampleWebApi.csproj @@ -7,15 +7,14 @@ - + - - - - - - - + + + + + + diff --git a/samples/SampleWebApi/Startup.cs b/samples/SampleWebApi/Startup.cs index c8532bb..d250935 100644 --- a/samples/SampleWebApi/Startup.cs +++ b/samples/SampleWebApi/Startup.cs @@ -1,6 +1,5 @@ using System.Linq; using FluentValidation; -using FluentValidation.AspNetCore; using MicroElements.Swashbuckle.FluentValidation.AspNetCore; using Microsoft.AspNetCore.Builder; using Microsoft.EntityFrameworkCore; @@ -40,9 +39,6 @@ public void ConfigureServices(IServiceCollection services) // Register FV validators services.AddValidatorsFromAssemblyContaining(lifetime: ServiceLifetime.Scoped); - // Add FV to Asp.net - services.AddFluentValidationAutoValidation(); - // Register all validators as IValidator? //var serviceDescriptors = services.Where(descriptor => descriptor.ServiceType.GetInterfaces().Contains(typeof(IValidator))).ToList(); //serviceDescriptors.ForEach(descriptor => services.Add(ServiceDescriptor.Describe(typeof(IValidator), descriptor.ImplementationType, descriptor.Lifetime))); diff --git a/src/MicroElements.NSwag.FluentValidation/MicroElements.NSwag.FluentValidation.csproj b/src/MicroElements.NSwag.FluentValidation/MicroElements.NSwag.FluentValidation.csproj index 51da5e3..5893fd5 100644 --- a/src/MicroElements.NSwag.FluentValidation/MicroElements.NSwag.FluentValidation.csproj +++ b/src/MicroElements.NSwag.FluentValidation/MicroElements.NSwag.FluentValidation.csproj @@ -1,4 +1,4 @@ - + Library @@ -14,8 +14,7 @@ - - + diff --git a/src/MicroElements.OpenApi.FluentValidation/MicroElements.OpenApi.FluentValidation.csproj b/src/MicroElements.OpenApi.FluentValidation/MicroElements.OpenApi.FluentValidation.csproj index 459ce4a..2079568 100644 --- a/src/MicroElements.OpenApi.FluentValidation/MicroElements.OpenApi.FluentValidation.csproj +++ b/src/MicroElements.OpenApi.FluentValidation/MicroElements.OpenApi.FluentValidation.csproj @@ -1,4 +1,4 @@ - + Library @@ -17,11 +17,10 @@ - + - - - + + diff --git a/src/MicroElements.Swashbuckle.FluentValidation.AspNetCore/MicroElements.Swashbuckle.FluentValidation.AspNetCore.csproj b/src/MicroElements.Swashbuckle.FluentValidation.AspNetCore/MicroElements.Swashbuckle.FluentValidation.AspNetCore.csproj deleted file mode 100644 index a322b21..0000000 --- a/src/MicroElements.Swashbuckle.FluentValidation.AspNetCore/MicroElements.Swashbuckle.FluentValidation.AspNetCore.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - Library - net8.0;net9.0 - enable - enable - - - - - - - diff --git a/src/MicroElements.Swashbuckle.FluentValidation/MicroElements.Swashbuckle.FluentValidation.csproj b/src/MicroElements.Swashbuckle.FluentValidation/MicroElements.Swashbuckle.FluentValidation.csproj index de98134..278df8c 100644 --- a/src/MicroElements.Swashbuckle.FluentValidation/MicroElements.Swashbuckle.FluentValidation.csproj +++ b/src/MicroElements.Swashbuckle.FluentValidation/MicroElements.Swashbuckle.FluentValidation.csproj @@ -14,13 +14,15 @@ - - - + + + + + \ No newline at end of file diff --git a/test/MicroElements.Swashbuckle.FluentValidation.Tests/MicroElements.Swashbuckle.FluentValidation.Tests.csproj b/test/MicroElements.Swashbuckle.FluentValidation.Tests/MicroElements.Swashbuckle.FluentValidation.Tests.csproj index 7cc184f..760b9c6 100644 --- a/test/MicroElements.Swashbuckle.FluentValidation.Tests/MicroElements.Swashbuckle.FluentValidation.Tests.csproj +++ b/test/MicroElements.Swashbuckle.FluentValidation.Tests/MicroElements.Swashbuckle.FluentValidation.Tests.csproj @@ -7,11 +7,10 @@ - - + - + diff --git a/test/MicroElements.Swashbuckle.FluentValidation.Tests/SwaggerTestHost.cs b/test/MicroElements.Swashbuckle.FluentValidation.Tests/SwaggerTestHost.cs index eaf9027..e448c4c 100644 --- a/test/MicroElements.Swashbuckle.FluentValidation.Tests/SwaggerTestHost.cs +++ b/test/MicroElements.Swashbuckle.FluentValidation.Tests/SwaggerTestHost.cs @@ -1,6 +1,5 @@ using System; using FluentValidation; -using FluentValidation.AspNetCore; using MicroElements.OpenApi.FluentValidation; using MicroElements.Swashbuckle.FluentValidation.AspNetCore; using Microsoft.AspNetCore.Mvc; @@ -30,9 +29,6 @@ public SwaggerTestHost Configure( Action? configureSchemaGenerationOptions = null, Action? configureRegistration = null) { - // Add FV - Services.AddFluentValidation(); - // Json options by default no name policy. Services.Configure(options => options.JsonSerializerOptions.PropertyNamingPolicy = null); From c66a484e41c4f4687bf5c9ddacb3380ab9e3b58c Mon Sep 17 00:00:00 2001 From: Dan J Date: Wed, 21 May 2025 09:48:30 -0500 Subject: [PATCH 3/3] Update sourcelink. Add workaround to scoped middleware, not sure if I like this option though --- src/Directory.Build.props | 2 +- ...icroElements.NSwag.FluentValidation.csproj | 4 -- ...roElements.OpenApi.FluentValidation.csproj | 4 -- ...ements.Swashbuckle.FluentValidation.csproj | 3 +- .../Swashbuckle/ScopedSwaggerMiddleware.cs | 48 +++++++++++++++++++ 5 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 src/MicroElements.Swashbuckle.FluentValidation/Swashbuckle/ScopedSwaggerMiddleware.cs diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 9ea8743..312d3a8 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -1,7 +1,7 @@ - + diff --git a/src/MicroElements.NSwag.FluentValidation/MicroElements.NSwag.FluentValidation.csproj b/src/MicroElements.NSwag.FluentValidation/MicroElements.NSwag.FluentValidation.csproj index 5893fd5..18bf7cb 100644 --- a/src/MicroElements.NSwag.FluentValidation/MicroElements.NSwag.FluentValidation.csproj +++ b/src/MicroElements.NSwag.FluentValidation/MicroElements.NSwag.FluentValidation.csproj @@ -21,8 +21,4 @@ - - - - \ No newline at end of file diff --git a/src/MicroElements.OpenApi.FluentValidation/MicroElements.OpenApi.FluentValidation.csproj b/src/MicroElements.OpenApi.FluentValidation/MicroElements.OpenApi.FluentValidation.csproj index 2079568..8925e28 100644 --- a/src/MicroElements.OpenApi.FluentValidation/MicroElements.OpenApi.FluentValidation.csproj +++ b/src/MicroElements.OpenApi.FluentValidation/MicroElements.OpenApi.FluentValidation.csproj @@ -23,8 +23,4 @@ - - - - \ No newline at end of file diff --git a/src/MicroElements.Swashbuckle.FluentValidation/MicroElements.Swashbuckle.FluentValidation.csproj b/src/MicroElements.Swashbuckle.FluentValidation/MicroElements.Swashbuckle.FluentValidation.csproj index 278df8c..5fe208e 100644 --- a/src/MicroElements.Swashbuckle.FluentValidation/MicroElements.Swashbuckle.FluentValidation.csproj +++ b/src/MicroElements.Swashbuckle.FluentValidation/MicroElements.Swashbuckle.FluentValidation.csproj @@ -22,7 +22,8 @@ - + + \ No newline at end of file diff --git a/src/MicroElements.Swashbuckle.FluentValidation/Swashbuckle/ScopedSwaggerMiddleware.cs b/src/MicroElements.Swashbuckle.FluentValidation/Swashbuckle/ScopedSwaggerMiddleware.cs new file mode 100644 index 0000000..26473c4 --- /dev/null +++ b/src/MicroElements.Swashbuckle.FluentValidation/Swashbuckle/ScopedSwaggerMiddleware.cs @@ -0,0 +1,48 @@ +using System; +using System.Threading.Tasks; +using global::Swashbuckle.AspNetCore.Swagger; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; + +namespace Swashbuckle.FluentValidation +{ + /// + /// SwaggerMiddleware that resolves on scope. + /// Resolves problems with validators with dependency on scoped services. + /// + public class ScopedSwaggerMiddleware + { + private readonly RequestDelegate _next; + private readonly SwaggerOptions _options; + + /// + /// ctor. + /// + /// + /// + public ScopedSwaggerMiddleware(RequestDelegate next, SwaggerOptions options) + { + _next = next; + _options = options; + } + + public async Task Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) + { + await new SwaggerMiddleware(_next, _options).Invoke(httpContext, swaggerProvider); + } + } + + public static class SwaggerBuilderExtensions + { + /// + /// Replaces standard with . + /// Use instead of if you have services with scoped services like DbContext. + /// + public static IApplicationBuilder UseScopedSwagger(this IApplicationBuilder app, Action setupAction = null) + { + SwaggerOptions swaggerOptions = new(); + setupAction?.Invoke(swaggerOptions); + return app.UseMiddleware(swaggerOptions); + } + } +} \ No newline at end of file