Skip to content

Commit 838f496

Browse files
authored
Add GenerateExpandQueryStringInternal nullCheck tests (#2797)
1 parent d5ade79 commit 838f496

4 files changed

Lines changed: 85 additions & 6 deletions

File tree

src/Microsoft.AspNet.OData.Shared/Query/ExpandQueryBuilder.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ public virtual string GenerateExpandQueryParameter(object value, IEdmModel model
3030
throw Error.ArgumentNull(nameof(value));
3131
}
3232

33-
if (model == null)
34-
{
35-
throw Error.ArgumentNull(nameof(model));
36-
}
37-
3833
return GenerateExpandQueryStringInternal(value, model, false);
3934
}
4035

test/E2ETest/Microsoft.Test.E2E.AspNet.OData/BulkOperation/BulkOperationController.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,14 @@ public ITestActionResult Post([FromBody] Employee employee)
265265
return Created(employee);
266266
}
267267

268+
[ODataRoute("UnTypedEmployees")]
269+
[HttpPost]
270+
[EnableQuery]
271+
public ITestActionResult PostUnTypedEmployees([FromBody] string emp)
272+
{
273+
return Ok(emp);
274+
}
275+
268276
[ODataRoute("Employees({key})/Friends")]
269277
[HttpPatch]
270278
public ITestActionResult PatchFriends(int key, [FromBody] DeltaSet<Friend> friendColl)

test/E2ETest/Microsoft.Test.E2E.AspNet.OData/BulkOperation/BulkOperationTest.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,30 @@ public async Task PostEmployee_WithFullMetadata()
14661466
}
14671467
}
14681468

1469-
#endregion
1469+
[Fact]
1470+
public async Task PostUntypedEmployeeAsString()
1471+
{
1472+
//Arrange
1473+
1474+
string requestUri = this.BaseAddress + "/convention/UntypedEmployees";
1475+
1476+
var content = @"{
1477+
'value': 'OData'
1478+
}";
1479+
1480+
var requestForPost = new HttpRequestMessage(new HttpMethod("POST"), requestUri);
1481+
1482+
StringContent stringContent = new StringContent(content: content, encoding: Encoding.UTF8, mediaType: "application/json");
1483+
requestForPost.Content = stringContent;
1484+
1485+
//Act & Assert
1486+
using (HttpResponseMessage response = await this.Client.SendAsync(requestForPost))
1487+
{
1488+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
1489+
}
1490+
}
1491+
1492+
#endregion
14701493

14711494
#region Full Metadata
14721495

test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/ExpandQueryBuilderTest.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,52 @@ public void MultipleUnevenLevelsExpand()
9696
Assert.Equal(expected, actual);
9797
}
9898

99+
[Fact]
100+
public void NoNavigationPropertyReturnsEmptyString()
101+
{
102+
Employee employee = new Employee()
103+
{
104+
ID = 1,
105+
Name= "Employee 1"
106+
};
107+
108+
string expected = "";
109+
string actual = expandQueryBuilder.GenerateExpandQueryParameter(employee, null);
110+
111+
Assert.Equal(expected, actual);
112+
}
113+
114+
[Fact]
115+
public void NullEdmModelReturnsEmptyString()
116+
{
117+
Employee employee = new Employee()
118+
{
119+
ID = 1,
120+
Friends = new List<Friend> { new Friend() { Id = 1001, Orders = new List<Order> { new Order { Id = 10001 } } }, new Friend() { Id = 1002, Orders = new List<Order> { new Order { Id = 10002, OrderLines = new List<OrderLine> { new OrderLine { Id = 222 } } }, new Order { Id = 10003 } } } },
121+
NewFriends = new List<NewFriend> { new NewFriend() { Id = 1001, NewOrders = new List<NewOrder> { new NewOrder { Id = 10001 } } } }
122+
};
123+
124+
string expected = "";
125+
string actual = expandQueryBuilder.GenerateExpandQueryParameter(employee, null);
126+
127+
Assert.Equal(expected, actual);
128+
}
129+
130+
[Fact]
131+
public void UnknownEdmTypeReturnsEmptyString()
132+
{
133+
NonEdmType nonEdmType = new NonEdmType()
134+
{
135+
Id = 1,
136+
Name = "Unknown Type"
137+
};
138+
139+
string expected = "";
140+
string actual = expandQueryBuilder.GenerateExpandQueryParameter(nonEdmType, model);
141+
142+
Assert.Equal(expected, actual);
143+
}
144+
99145
private class Employee
100146
{
101147
[Key]
@@ -147,5 +193,12 @@ private class NewOrder
147193
public int Quantity { get; set; }
148194
public ODataIdContainer Container { get; set; }
149195
}
196+
197+
private class NonEdmType
198+
{
199+
[Key]
200+
public int Id { get; set; }
201+
public string Name { get; set; }
202+
}
150203
}
151204
}

0 commit comments

Comments
 (0)