Skip to content

Commit

Permalink
Merge pull request #776 from mikepizzo/ODataV4-7.x-ContextUrl
Browse files Browse the repository at this point in the history
Fixes for invalid ContextUrls
  • Loading branch information
mikepizzo authored Mar 21, 2017
2 parents bbd9ede + 537bf75 commit 751e047
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.OData.Core/ODataContextUrlInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ private static string ComputeNavigationPath(EdmNavigationSourceKind kind, ODataU
if (isContained && odataUri != null && odataUri.Path != null)
{
ODataPath odataPath = odataUri.Path.TrimEndingTypeSegment().TrimEndingKeySegment();
if (!(odataPath.LastSegment is NavigationPropertySegment))
if (!(odataPath.LastSegment is NavigationPropertySegment) && !(odataPath.LastSegment is OperationSegment))
{
throw new ODataException(Strings.ODataContextUriBuilder_ODataPathInvalidForContainedElement(odataPath.ToContextUrlPathString()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

namespace Microsoft.OData.UriParser
{
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.OData.Edm;
using Microsoft.OData.Metadata;

/// <summary>
/// Extension methods for <see cref="ODataPath"/>. These method provide convenince functions.
/// TODO: Implement this class and it's visitors. These are stubs.
Expand Down Expand Up @@ -130,7 +131,6 @@ public static ODataPath TrimEndingKeySegment(this ODataPath path)
return newPath;
}


/// <summary>
/// Remove the type-cast segment in the end of ODataPath, the method does not modify current ODataPath instance,
/// it returns a new ODataPath without ending type segment.
Expand Down Expand Up @@ -174,7 +174,60 @@ public static bool IsUndeclared(this ODataPath path)
/// <returns>The string representation of the Context Url path.</returns>
public static string ToContextUrlPathString(this ODataPath path)
{
return string.Concat(path.WalkWith(PathSegmentToContextUrlPathTranslator.DefaultInstance).ToArray()).TrimStart('/');
StringBuilder pathString = new StringBuilder();
PathSegmentToContextUrlPathTranslator pathTranslator = PathSegmentToContextUrlPathTranslator.DefaultInstance;
ODataPathSegment priorSegment = null;
foreach (ODataPathSegment segment in path)
{
OperationSegment operationSegment = segment as OperationSegment;
if (operationSegment != null)
{
bool isBoundPath = false;

if (priorSegment != null)
{
foreach (IEdmOperation operation in operationSegment.Operations)
{
if (operation.IsBound && operation.Parameters.First().Type.Definition == priorSegment.EdmType)
{
if (operation.EntitySetPath != null)
{
foreach (string pathSegment in operation.EntitySetPath.PathSegments.Skip(1))
{
pathString.Append('/');
pathString.Append(pathSegment);
}

isBoundPath = true;
}

break;
}
}
}

if (!isBoundPath)
{
if (operationSegment.EntitySet != null)
{
pathString.Clear();
pathString.Append(operationSegment.EntitySet.Name);
}
else
{
return null;
}
}
}
else
{
pathString.Append(segment.TranslateWith(pathTranslator));
}

priorSegment = segment;
}

return pathString.ToString().TrimStart('/');
}

/// <summary>
Expand Down

0 comments on commit 751e047

Please sign in to comment.