Skip to content

Commit

Permalink
Make CreatedODataResult inherit from ObjectResult (#906)
Browse files Browse the repository at this point in the history
  • Loading branch information
KenitoInc authored May 5, 2023
1 parent f80c8ec commit 1f978d9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Microsoft.AspNetCore.OData/Results/CreatedODataResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ namespace Microsoft.AspNetCore.OData.Results
/// <remarks>This action result handles content negotiation and the HTTP prefer header. It generates a location
/// header containing the edit link of the created entity and, if response has status code: NoContent, also
/// generates an OData-EntityId header.</remarks>
public class CreatedODataResult<T> : ActionResult
public class CreatedODataResult<T> : ObjectResult
{
/// <summary>
/// Initializes a new instance of the <see cref="CreatedODataResult{T}"/> class.
/// </summary>
/// <param name="entity">The created entity.</param>
public CreatedODataResult(T entity)
: base(entity)
{
Entity = entity ?? throw Error.ArgumentNull(nameof(entity));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public IActionResult GetHomeAddress(int key)

return Ok(c.HomeAddress);
}

[EnableQuery]
public IActionResult Post([FromBody] Customer customer)
{
return Created(customer);
}
}

public class PeopleController : ODataController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.OData.E2E.Tests.Extensions;
using Microsoft.AspNetCore.OData.TestCommon;
Expand Down Expand Up @@ -422,5 +423,35 @@ public async Task NonDefaultMaxExpansionDepthAppliesToAutoExpand(string entitySe
"}",
content);
}

[Fact]
public async Task PostCustomer_AutoExpandNavigationProperties()
{
//Arrange
string requestUri = "autoexpand/Customers";

var content = @"{
'Id':88,
'Order':{ 'Id':1, 'Choice' : {'Id': 101, 'Amount': 10}},
'Friend':{'Id': 99, 'HomeAddress': {'Street': 'Street 1', 'City': 'City 1'}}
}";

HttpClient client = CreateClient();

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

var expectedOrder = "Order\":{\"Id\":1,\"Choice\":{\"Id\":101,\"Amount\":10.0}}";
var expectedFriend = "Friend\":{\"Id\":99,\"HomeAddress\":{\"Street\":\"Street 1\",\"City\":\"City 1\",\"CountryOrRegion\":null},\"Order\":null,\"Friend\":null}";

//Act & Assert
using (HttpRequestMessage requestForPost = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = stringContent })
using (HttpResponseMessage response = await client.SendAsync(requestForPost))
{
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
var json = response.Content.ReadAsStringAsync().Result;
Assert.Contains(expectedFriend, json);
Assert.Contains(expectedOrder, json);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,7 @@ public class Microsoft.AspNetCore.OData.Results.ConflictODataResult : Microsoft.
public virtual System.Threading.Tasks.Task ExecuteResultAsync (Microsoft.AspNetCore.Mvc.ActionContext context)
}

public class Microsoft.AspNetCore.OData.Results.CreatedODataResult`1 : Microsoft.AspNetCore.Mvc.ActionResult, IActionResult {
public class Microsoft.AspNetCore.OData.Results.CreatedODataResult`1 : Microsoft.AspNetCore.Mvc.ObjectResult, IActionResult, IStatusCodeActionResult {
public CreatedODataResult`1 (T entity)

T Entity { public virtual get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,7 @@ public class Microsoft.AspNetCore.OData.Results.ConflictODataResult : Microsoft.
public virtual System.Threading.Tasks.Task ExecuteResultAsync (Microsoft.AspNetCore.Mvc.ActionContext context)
}

public class Microsoft.AspNetCore.OData.Results.CreatedODataResult`1 : Microsoft.AspNetCore.Mvc.ActionResult, IActionResult {
public class Microsoft.AspNetCore.OData.Results.CreatedODataResult`1 : Microsoft.AspNetCore.Mvc.ObjectResult, IActionResult, IStatusCodeActionResult {
public CreatedODataResult`1 (T entity)

T Entity { public virtual get; }
Expand Down

0 comments on commit 1f978d9

Please sign in to comment.