Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 40 additions & 2 deletions src/Microsoft.OData.Core/ODataWriterCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2785,9 +2785,14 @@ private void EnterScope(WriterState newState, ODataItem item)
{
case EdmNavigationSourceKind.ContainedEntitySet:
// Containment cannot be written alone without odata uri.
bool pathBuiltFromResourceId = false;
if (!odataPath.Any())
{
throw new ODataException(SRResources.ODataWriterCore_PathInODataUriMustBeSetWhenWritingContainedElement);
pathBuiltFromResourceId = this.TryBuildPathFromResourceId(out odataPath);
if (odataPath == null || !odataPath.Any())
{
Comment thread
xuzhg marked this conversation as resolved.
throw new ODataException(SRResources.ODataWriterCore_PathInODataUriMustBeSetWhenWritingContainedElement);
}
}

ODataPath newPath = null;
Expand All @@ -2796,6 +2801,11 @@ private void EnterScope(WriterState newState, ODataItem item)
// if there's no key, for example in a complex property, just use the existing odata path.
newPath = odataPath;
}
else if (pathBuiltFromResourceId && !odataPath.IsCollection())
{
// A resource ID that targets a single resource already represents its complete path.
newPath = odataPath;
}
else
{
// If there's only non-key properties selected in the request URI, then We don't have the key values to calcuate the key segment.
Expand All @@ -2808,7 +2818,8 @@ private void EnterScope(WriterState newState, ODataItem item)
}
else
{
newPath = null;
ODataPath resourcePath;
newPath = this.TryBuildPathFromResourceId(out resourcePath) ? resourcePath : null;
}
}

Expand Down Expand Up @@ -2903,6 +2914,33 @@ private bool TryBuildKeySegment(out KeySegment keySegment)
return true;
}

private bool TryBuildPathFromResourceId(out ODataPath resourcePath)
{
ODataResourceBase resource = this.CurrentScope.Item as ODataResourceBase;
Uri resourceId = resource != null && resource.HasNonComputedId ? resource.NonComputedId : null;
Uri serviceRoot = this.CurrentScope.ODataUri.ServiceRoot;
if (resourceId == null || serviceRoot == null)
{
resourcePath = null;
return false;
}

try
{
resourcePath = new ODataUriParser(
this.outputContext.Model,
serviceRoot,
resourceId,
this.outputContext.Container).ParsePath();
return resourcePath.Any();
}
catch (ODataException)
{
resourcePath = null;
return false;
}
}

private IList<KeyValuePair<string, object>> GetKeyProperties(IEdmEntityType currentEntityType, bool throwIfFail)
{
ODataResourceBase resource = this.CurrentScope.Item as ODataResourceBase;
Expand Down
Loading