Skip to content

Trim forward slashes from route prefix - #395

Merged
habbes merged 4 commits into
OData:masterfrom
habbes:fix/382-trim-route-prefix-slashes
Nov 29, 2021
Merged

Trim forward slashes from route prefix#395
habbes merged 4 commits into
OData:masterfrom
habbes:fix/382-trim-route-prefix-slashes

Conversation

@habbes

@habbes habbes commented Nov 29, 2021

Copy link
Copy Markdown
Contributor

Fix #382

This PR strips leading and trailing forward slashes from the OData path route prefix before registering route components service provider. This avoids the issues mentioned in #382 which result in the route prefix with leading slashes not being routed to OData endpoints. Route prefix with trailing slashes were causing an exception to be thrown because it would be combined with the endpoint path leading to 2 consecutive forward slashes.

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.

Comment thread src/Microsoft.AspNetCore.OData/ODataOptions.cs
Comment on lines +326 to +329
if (!routePrefix.StartsWith('/') && !routePrefix.EndsWith('/'))
{
return routePrefix;
}

@habbes habbes Nov 29, 2021

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.

I add this check to avoid allocating a new string in case the string contains no slashes. But I confirmed that the implementation of string.Trim returns the original string if no actual trimming was performed. But I kept this code here for clarity and also cause it seems to be doing less work (haven't confirmed with actual benchmakrs though) compared to string.Trim when there's nothing to trim.

@gathogojr gathogojr Nov 29, 2021

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.

If string.Trim actually does that then I'm inclined to think that this method is an overkill 'coz I'd expect that string.Trim does it almost same way that you're doing. Another thing... The above logic would throw an exception if routePrefix was null, though I assume that the null check is done elsewhere - you could just add a Debug.Assert. If you choose to retain this, maybe this would give us better performance:

if (routePrefix.Length > 0 && routePrefix[0] != '/' && routePrefix[routePrefix.Length - 1] != '/')

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.

I do check that the prefix is not null on the public methods that call SanitizeRoutePrefix.

I've added the Debug.Assert and used your suggested if statement. I assume the string.StartsWith and string.EndsWith would have been inlined eitherway?

Comment thread src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml
}

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.

@marabooy marabooy left a comment

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.

🕚

Comment thread src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml
@habbes
habbes requested review from gathogojr and marabooy November 29, 2021 07:05

@KenitoInc KenitoInc left a comment

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.

Consider adding tests for these edge cases:

  • Single slash as the route prefix
  • Double slash as the route prefix
    What should be the expected behaviour?

Comment thread src/Microsoft.AspNetCore.OData/ODataOptions.cs
@habbes

habbes commented Nov 29, 2021

Copy link
Copy Markdown
Contributor Author

@KenitoInc

Consider adding tests for these edge cases:

  • Single slash as the route prefix
  • Double slash as the route prefix
    What should be the expected behaviour?

Good point. A single slash route prefix would be handled as an empty string. I think that is the correct behaviour.

A double slash will also be handled as a an empty string, but I think in this case that is invalid and ought to be an error. But I don't know if we should go the extra-length of validating such route prefixes. I personally am not sure it's worth the effort of checking the string contents.

@habbes
habbes requested a review from KenitoInc November 29, 2021 07:34

@gathogojr gathogojr left a comment

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.

:shipit:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"@odata.context" is inconsistently returned

5 participants