-
Notifications
You must be signed in to change notification settings - Fork 186
Trim forward slashes from route prefix #395
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Diagnostics.Contracts; | ||
| using Microsoft.AspNetCore.OData.Abstracts; | ||
| using Microsoft.AspNetCore.OData.Batch; | ||
|
|
@@ -129,14 +130,22 @@ public ODataOptions AddRouteComponents(string routePrefix, IEdmModel model, Acti | |
| throw Error.ArgumentNull(nameof(model)); | ||
| } | ||
|
|
||
| if (RouteComponents.ContainsKey(routePrefix)) | ||
| if (routePrefix == null) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we have cases where the prefix is null?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a case where the prefix is empty (the default case). Not sure about null though. In any case, you can't add a |
||
| { | ||
| throw Error.InvalidOperation(SRResources.ModelPrefixAlreadyUsed, routePrefix); | ||
| throw Error.ArgumentNull(nameof(routePrefix)); | ||
| } | ||
|
|
||
| string sanitizedRoutePrefix = SanitizeRoutePrefix(routePrefix); | ||
|
|
||
| if (RouteComponents.ContainsKey(sanitizedRoutePrefix)) | ||
| { | ||
| throw Error.InvalidOperation(SRResources.ModelPrefixAlreadyUsed, sanitizedRoutePrefix); | ||
| } | ||
|
|
||
|
|
||
| // Consider to use Lazy<IServiceProvider> ? | ||
| IServiceProvider serviceProvider = BuildRouteContainer(model, configureServices); | ||
| RouteComponents[routePrefix] = (model, serviceProvider); | ||
| RouteComponents[sanitizedRoutePrefix] = (model, serviceProvider); | ||
|
gathogojr marked this conversation as resolved.
|
||
| return this; | ||
| } | ||
|
|
||
|
|
@@ -147,9 +156,16 @@ public ODataOptions AddRouteComponents(string routePrefix, IEdmModel model, Acti | |
| /// <returns>The root service provider for the route (prefix) name.</returns> | ||
| public IServiceProvider GetRouteServices(string routePrefix) | ||
| { | ||
| if (routePrefix != null && RouteComponents.ContainsKey(routePrefix)) | ||
| if (routePrefix == null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| string sanitizedRoutePrefix = SanitizeRoutePrefix(routePrefix); | ||
|
|
||
| if (RouteComponents.TryGetValue(sanitizedRoutePrefix, out var components)) | ||
| { | ||
| return RouteComponents[routePrefix].ServiceProvider; | ||
| return components.ServiceProvider; | ||
| } | ||
|
|
||
| return null; | ||
|
|
@@ -300,5 +316,22 @@ private IServiceProvider BuildRouteContainer(IEdmModel model, Action<IServiceCol | |
|
|
||
| return builder.BuildContainer(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sanitizes the route prefix by stripping leading and trailing forward slashes. | ||
| /// </summary> | ||
| /// <param name="routePrefix">Route prefix to sanitize.</param> | ||
| /// <returns>Sanitized route prefix.</returns> | ||
| private string SanitizeRoutePrefix(string routePrefix) | ||
| { | ||
| Debug.Assert(routePrefix != null); | ||
|
|
||
| if (routePrefix.Length > 0 && routePrefix[0] != '/' && routePrefix[^1] != '/') | ||
| { | ||
| return routePrefix; | ||
| } | ||
|
|
||
| return routePrefix.Trim('/'); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.