Skip to content
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

support writing datamodififcation exception message #2764

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
Original file line number Diff line number Diff line change
Expand Up @@ -137,26 +137,30 @@ private ODataResourceValue CreateODataResourceValue(object graph, IEdmStructured
}
else
{
HashSet<string> structuralPropertyNames = new HashSet<string>();

foreach(IEdmStructuralProperty structuralProperty in expectedType.DeclaredStructuralProperties())
// If a type is an org.OData.Core.V1.DataModificationExceptionType type,
// then we include all it's properties and not just the structural properties.
if (expectedType.Definition.FullTypeName().Equals("Org.OData.Core.V1.DataModificationExceptionType"))
ElizabethOkerio marked this conversation as resolved.
Show resolved Hide resolved
{
structuralPropertyNames.Add(structuralProperty.Name);
foreach (PropertyInfo property in graph.GetType().GetProperties())
{
SetPropertyValueInternal(property, graph, properties, writeContext);
}
}

foreach (PropertyInfo property in graph.GetType().GetProperties())
else
{
if (structuralPropertyNames.Contains(property.Name))
HashSet<string> structuralPropertyNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

foreach (IEdmStructuralProperty structuralProperty in expectedType.DeclaredStructuralProperties())
{
object propertyValue = property.GetValue(graph);
IEdmStructuredTypeReference expectedPropType = null;
structuralPropertyNames.Add(structuralProperty.Name);
}

if (propertyValue == null)
foreach (PropertyInfo property in graph.GetType().GetProperties())
{
if (structuralPropertyNames.Contains(property.Name))
{
expectedPropType = writeContext.GetEdmType(propertyValue, property.PropertyType) as IEdmStructuredTypeReference;
SetPropertyValueInternal(property, graph, properties, writeContext);
}

SetPropertyValue(writeContext, properties, expectedPropType, property.Name, propertyValue);
}
}
}
Expand Down Expand Up @@ -188,6 +192,19 @@ private void SetDeltaPropertyValue(ODataSerializerContext writeContext, List<ODa
}
}

private void SetPropertyValueInternal(PropertyInfo property, object graph, List<ODataProperty> properties, ODataSerializerContext writeContext)
{
object propertyValue = property.GetValue(graph);
IEdmStructuredTypeReference expectedPropType = null;

if (propertyValue == null)
{
expectedPropType = writeContext.GetEdmType(propertyValue, property.PropertyType) as IEdmStructuredTypeReference;
}

SetPropertyValue(writeContext, properties, expectedPropType, property.Name, propertyValue);
}

private void SetPropertyValue(ODataSerializerContext writeContext, List<ODataProperty> properties, IEdmStructuredTypeReference expectedType, string propertyName, object propertyValue)
{
if (propertyValue == null && expectedType == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,7 @@ public async Task PatchEmployee_WithFailedDeletes_Friends()
Client.DefaultRequestHeaders.Add("Prefer", @"odata.include-annotations=""*""");

//Act & Assert
var expected = "$delta\",\"value\":[{\"@NS.Test\":1,\"@Core.DataModificationException\":" +
"{\"@type\":\"#Org.OData.Core.V1.DataModificationExceptionType\"},\"Id\":2,\"Name\":null,\"Age\":15}]}";
var expected = "{\"@context\":\"" + this.BaseAddress + "/convention/$metadata#NewFriends/$delta\",\"value\":[{\"@NS.Test\":1,\"@Core.DataModificationException\":{\"@type\":\"#Org.OData.Core.V1.DataModificationExceptionType\",\"FailedOperation\":\"Delete\",\"ResponseCode\":0,\"MessageType\":{\"Code\":null,\"Message\":\"The method or operation is not implemented.\",\"Severity\":null,\"Target\":null,\"Details\":null}},\"Id\":2,\"Name\":null,\"Age\":15}]}";

using (HttpResponseMessage response = await this.Client.SendAsync(requestForPatch))
{
Expand Down Expand Up @@ -474,8 +473,7 @@ public async Task PatchEmployee_WithFailedOperation_WithAnnotations()
Client.DefaultRequestHeaders.Add("Prefer", @"odata.include-annotations=""*""");

//Act & Assert
var expected = "/convention/$metadata#NewFriends/$delta\",\"value\":[{\"@NS.Test2\":\"testing\",\"@Core.ContentID\":3," +
"\"@Core.DataModificationException\":{\"@type\":\"#Org.OData.Core.V1.DataModificationExceptionType\"},\"Id\":2,\"Name\":null,\"Age\":15}]}";
var expected = "{\"@context\":\"" + this.BaseAddress + "/convention/$metadata#NewFriends/$delta\",\"value\":[{\"@NS.Test2\":\"testing\",\"@Core.ContentID\":3,\"@Core.DataModificationException\":{\"@type\":\"#Org.OData.Core.V1.DataModificationExceptionType\",\"FailedOperation\":\"Delete\",\"ResponseCode\":0,\"MessageType\":{\"Code\":null,\"Message\":\"The method or operation is not implemented.\",\"Severity\":null,\"Target\":null,\"Details\":null}},\"Id\":2,\"Name\":null,\"Age\":15}]}";

using (HttpResponseMessage response = await this.Client.SendAsync(requestForPatch))
{
Expand Down