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

Make CreatedODataResult inherit from ObjectResult #906

Merged
merged 4 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
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
KenitoInc marked this conversation as resolved.
Show resolved Hide resolved
{
/// <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,34 @@ 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'}}
}";

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 (HttpClient client = CreateClient())
KenitoInc marked this conversation as resolved.
Show resolved Hide resolved
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