Skip to content

Commit

Permalink
Retrieve the navigation properties if there's on $expand for delta se…
Browse files Browse the repository at this point in the history
…rializer
  • Loading branch information
xuzhg committed Jan 30, 2025
1 parent bf26422 commit 714f065
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,7 @@ private IEnumerable<KeyValuePair<IEdmNavigationProperty, Type>> GetNavigationPro

if (navigationProperties == null)
{
yield break;
navigationProperties = resourceContext.StructuredType.DeclaredNavigationProperties();
}

if (resourceContext.EdmObject is IDelta changedObject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,69 @@ protected override void UpdateConfiguration(WebRouteConfiguration configuration)
}

#region Update


[Fact]
public async Task PatchEmployee_WithNestedFriends_WithNestedOrders_IsSerializedSuccessfully()
{
//Arrange
string requestUri = this.BaseAddress + "/convention/Employees";
var content = @"{
'@odata.context':'http://host/service/$metadata#Employees/$delta',
'value':[
{'ID':1,'Name':'Employee1','[email protected]':[{'Id':1,'Name':'Friend1','[email protected]':[{'Id':1,'Price': 10},{'Id':2,'Price':20} ]},{'Id':2,'Name':'Friend2'}]},
{'ID':2,'Name':'Employee2','[email protected]':[{'Id':3,'Name':'Friend3','[email protected]' :[{'Id':3,'Price': 30}, {'Id':4,'Price': 40} ]},{'Id':4,'Name':'Friend4'}]}
]}";

var requestForPatch = new HttpRequestMessage(new HttpMethod("PATCH"), requestUri);

string expectedResponse = "{" +
"\"@context\":\"" + this.BaseAddress + "/convention/$metadata#Employees/$delta\"," +
"\"value\":[" +
"{\"ID\":1,\"Name\":\"Employee1\",\"SkillSet\":[],\"Gender\":\"0\",\"AccessLevel\":\"0\",\"FavoriteSports\":null,\"Friends@delta\":[{\"Id\":1,\"Name\":\"Friend1\",\"Age\":0,\"Orders@delta\":[{\"Id\":1,\"Price\":10},{\"Id\":2,\"Price\":20}]},{\"Id\":2,\"Name\":\"Friend2\",\"Age\":0}]}," +
"{\"ID\":2,\"Name\":\"Employee2\",\"SkillSet\":[],\"Gender\":\"0\",\"AccessLevel\":\"0\",\"FavoriteSports\":null,\"Friends@delta\":[{\"Id\":3,\"Name\":\"Friend3\",\"Age\":0,\"Orders@delta\":[{\"Id\":3,\"Price\":30},{\"Id\":4,\"Price\":40}]},{\"Id\":4,\"Name\":\"Friend4\",\"Age\":0}]}]}";
StringContent stringContent = new StringContent(content: content, encoding: Encoding.UTF8, mediaType: "application/json");
requestForPatch.Content = stringContent;

// Act & Assert
using (HttpResponseMessage response = await this.Client.SendAsync(requestForPatch))
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var json = response.Content.ReadAsStringAsync().Result;
Assert.Equal(expectedResponse.ToString().ToLower(), json.ToString().ToLower());
Assert.Contains("Employee1", json);
Assert.Contains("Employee2", json);
}
}

[Fact]
public async Task PatchEmployee_WithDeletedAndODataId_IsSerializedSuccessfully()
{
//Arrange
string requestUri = this.BaseAddress + "/convention/Employees";

var content = @"{
'@odata.context':'http://host/service/$metadata#Employees/$delta',
'value':[
{'ID':1,'Name':'Employee1','[email protected]':[{'@odata.removed':{'reason':'changed'},'Id':1}]},
{'ID':2,'Name':'Employee2','[email protected]':[{'@odata.id':'Friends(1)'}]}
]}";

string expectedResponse = "{\"@context\":\"" + this.BaseAddress + "/convention/$metadata#Employees/$delta\",\"value\":[{\"ID\":1,\"Name\":\"Employee1\",\"SkillSet\":[],\"Gender\":\"0\",\"AccessLevel\":\"0\",\"FavoriteSports\":null,\"Friends@delta\":[{\"@removed\":{\"reason\":\"changed\"},\"@id\":\"http://host/service/Friends(1)\",\"Id\":1,\"Name\":null,\"Age\":0}]},{\"ID\":2,\"Name\":\"Employee2\",\"SkillSet\":[],\"Gender\":\"0\",\"AccessLevel\":\"0\",\"FavoriteSports\":null,\"Friends@delta\":[{\"Id\":0,\"Name\":null,\"Age\":0}]}]}";

var requestForUpdate = new HttpRequestMessage(new HttpMethod("PATCH"), requestUri);

StringContent stringContent = new StringContent(content: content, encoding: Encoding.UTF8, mediaType: "application/json");
requestForUpdate.Content = stringContent;

//Act & Assert
using (HttpResponseMessage response = await this.Client.SendAsync(requestForUpdate))
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var json = response.Content.ReadAsStringAsync().Result;
Assert.Equal(expectedResponse.ToString().ToLower(), json.ToString().ToLower());
}
}

[Fact]
public async Task PatchEmployee_WithUpdates()
{
Expand Down

0 comments on commit 714f065

Please sign in to comment.