Swashbuckle.AspNetCore icon indicating copy to clipboard operation
Swashbuckle.AspNetCore copied to clipboard

[Bug]: Breaking change in 6.6.1; It now requires you to have the `Regex` Route Constraint even if you don't use it

Open KennethHoff opened this issue 8 months ago • 2 comments

Describe the bug

In 6.5.0 and prior you could use WebApplication.CreateSlimBuilder (which, among other things, doesn't add the Regex Route constraint due to it increasing the file size of the app needlessly), but after upgrading to 6.6.* (Tested with 6.6.1 and 6.6.2) this will throw an exception.

Expected behavior

SwashBuckle should not require RegexRouteConstraint to exist in order to work.

Actual behavior

SwashBuckle uses the Regex Route constraint despite it not existing, and therefore the app no longer starts.

Steps to reproduce

https://github.com/KennethHoff/Repros/blob/master/SwashbuckleRegexException/WebApplication1/Program.cs

Exception(s) (if any)

 Error Message:
   Microsoft.AspNetCore.Routing.RouteCreationException : An error occurred while trying to create an instance of 'Microsoft.AspNetCore.Routing.Constraints.RegexErrorStubRouteConstraint'.
---- System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
-------- System.InvalidOperationException : A route parameter uses the regex constraint, which isn't registered. If this application was configured using CreateSlimBuilder(...) or AddRoutingCore(...) then this constraint is not registered by default. To use the regex constraint, configure route options at app startup: services.Configure<RouteOptions>(options => options.SetParameterPolicy<RegexInlineRouteConstraint>("regex"));
  Stack Trace:
     at Microsoft.AspNetCore.Routing.ParameterPolicyActivator.ResolveParameterPolicy[T](IDictionary`2 inlineParameterPolicyMap, IServiceProvider serviceProvider, String inlineParameterPolicy, String& parameterPolicyKey)
   at Microsoft.AspNetCore.Routing.DefaultParameterPolicyFactory.Create(RoutePatternParameterPart parameter, String inlineText)
   at Microsoft.AspNetCore.Routing.Template.DefaultTemplateBinderFactory.Create(RoutePattern pattern)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware..ctor(RequestDelegate next, SwaggerOptions options, TemplateBinderFactory templateBinderFactory)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
   at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.ReflectionMiddlewareBinder.CreateMiddleware(RequestDelegate next)
   at Microsoft.AspNetCore.Builder.ApplicationBuilder.Build()
   at Microsoft.AspNetCore.Builder.ApplicationBuilder.Build()
   at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>b__15_1(IHostedService service, CancellationToken token)
   at Microsoft.Extensions.Hosting.Internal.Host.ForeachService[T](IEnumerable`1 services, CancellationToken token, Boolean concurrent, Boolean abortOnFirstException, List`1 exceptions, Func`3 operation)
   at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)

Swashbuckle.AspNetCore version

6.6.1, 6.6.2

.NET Version

8.0.302

Anything else?

No response

KennethHoff avatar Jun 18 '24 08:06 KennethHoff