Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
KennethHoff opened this issue Jun 18, 2024 · 2 comments
Labels
bug help-wanted A change up for grabs for contributions from the community

Comments

@KennethHoff
Copy link

KennethHoff commented Jun 18, 2024

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

@martincostello
Copy link
Collaborator

This is a result of the changes in #2418.

You can unblock yourself by explicitly setting SwaggerOptions.RouteTemplate to the previous route that doesn't use a Regex, e.g. swagger/{documentName}/swagger.{json|yaml}.

We'd be happy to take a PR that changes the route template to still fixes the issue that using the Regex resolved that doesn't need a Regex.

@martincostello martincostello added the help-wanted A change up for grabs for contributions from the community label Jun 18, 2024
@martincostello
Copy link
Collaborator

Just hit this myself with our native AoT test application.

Easily resolvable by adding:

builder.Services.Configure<RouteOptions>(
    options => options.SetParameterPolicy<RegexInlineRouteConstraint>("regex"));

martincostello added a commit to martincostello/Swashbuckle.AspNetCore that referenced this issue Sep 30, 2024
- Add support for using the ReDoc middleware in native AoT applications.
- Add missing test coverage for ReDoc extensions.
- Fix some code analysis suggestions.
- Fix native AoT test app exception on start-up (see domaindrivendev#2951).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help-wanted A change up for grabs for contributions from the community
Projects
None yet
Development

No branches or pull requests

2 participants