Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Microsoft.AspNetCore.OData/ODataOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,22 @@ public ODataOptions AddRouteComponents(string routePrefix, IEdmModel model, Acti
throw Error.ArgumentNull(nameof(model));
}

if (RouteComponents.ContainsKey(routePrefix))
if (routePrefix == null)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have cases where the prefix is null?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 null key to a dictionary. So I think throwing on null here is the right thing to do.

{
throw Error.InvalidOperation(SRResources.ModelPrefixAlreadyUsed, routePrefix);
throw Error.ArgumentNull(nameof(routePrefix));
}

string sanitizedRoutePrefix = routePrefix.Trim('/');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe Trim does both a TrimStart and a TrimeEnd. Are we okay with that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. If you don't trim the end, an exception is thrown by AspNetCore as mentioned in the description.


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);
Comment thread
gathogojr marked this conversation as resolved.
return this;
}

Expand Down
29 changes: 29 additions & 0 deletions test/Microsoft.AspNetCore.OData.Tests/ODataOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ public void AddRouteComponents_WithDependencyInjection_SetModelAndServices()
Assert.IsType<ODataFeature>(actual);
}

[Theory]
[InlineData("/odata")]
[InlineData("/odata/")]
[InlineData("odata/")]
public void AddRouteComponents_Strips_RoutePrefix_Leading_And_Trailing_Slashes(string routePrefix)
{
// Arrange
string expectedRoutePrefix = "odata";
ODataOptions options = new ODataOptions();
IEdmModel edmModel = EdmCoreModel.Instance;

// Act
options.AddRouteComponents(routePrefix, edmModel, services => services.AddSingleton<IODataFeature, ODataFeature>());

// Assert
Assert.False(options.RouteComponents.ContainsKey(routePrefix));
Assert.True(options.RouteComponents.ContainsKey(expectedRoutePrefix));
}

[Fact]
public void AddRouteComponents_Throws_IfModelNull()
{
Expand All @@ -150,6 +169,16 @@ public void AddRouteComponents_Throws_IfModelNull()
ExceptionAssert.ThrowsArgumentNull(() => options.AddRouteComponents("odata", null, builder => { }), "model");
}

[Fact]
public void AddRouteComponents_Throws_IfRoutePrefixNull()
{
// Arrange
ODataOptions options = new ODataOptions();

// Act & Assert
ExceptionAssert.ThrowsArgumentNull(() => options.AddRouteComponents(null, EdmCoreModel.Instance, builder => { }), "routePrefix");
}

[Fact]
public void AddRouteComponents_Throws_IfPrefixExisted()
{
Expand Down