-
Notifications
You must be signed in to change notification settings - Fork 161
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
Resolve ContentID in odata.bind annotation #643
Merged
gathogojr
merged 4 commits into
OData:main
from
gathogojr:fix/2442-resolve-ContentID-odatabind-annotation
Jul 27, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
test/Microsoft.AspNetCore.OData.E2E.Tests/Batch/ContentIdToLocationMappingTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
//----------------------------------------------------------------------------- | ||
// <copyright file="ContentIdToLocationMappingTests.cs" company=".NET Foundation"> | ||
// Copyright (c) .NET Foundation and Contributors. All rights reserved. | ||
// See License.txt in the project root for license information. | ||
// </copyright> | ||
//------------------------------------------------------------------------------ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.OData.Batch; | ||
using Microsoft.AspNetCore.OData.E2E.Tests.Commons; | ||
using Microsoft.AspNetCore.OData.Routing.Controllers; | ||
using Microsoft.AspNetCore.OData.TestCommon; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.OData; | ||
using Microsoft.OData.Edm; | ||
using Microsoft.OData.ModelBuilder; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.OData.E2E.Tests.Batch | ||
{ | ||
public class ContentIdToLocationMappingTests : WebApiTestBase<ContentIdToLocationMappingTests> | ||
{ | ||
private static IEdmModel edmModel; | ||
|
||
public ContentIdToLocationMappingTests(WebApiTestFixture<ContentIdToLocationMappingTests> fixture) | ||
: base(fixture) | ||
{ | ||
} | ||
|
||
protected static void UpdateConfigureServices(IServiceCollection services) | ||
{ | ||
services.ConfigureControllers( | ||
typeof(ContentIdToLocationMappingParentsController), | ||
typeof(ContentIdToLocationMappingChildrenController)); | ||
|
||
edmModel = GetEdmModel(); | ||
services.AddControllers().AddOData(opt => | ||
{ | ||
opt.EnableQueryFeatures(); | ||
opt.EnableContinueOnErrorHeader = true; | ||
opt.AddRouteComponents("ContentIdToLocationMapping", edmModel, new DefaultODataBatchHandler()); | ||
}); | ||
} | ||
|
||
protected static void UpdateConfigure(IApplicationBuilder app) | ||
{ | ||
app.UseODataBatching(); | ||
app.UseRouting(); | ||
app.UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapControllers(); | ||
}); | ||
} | ||
|
||
protected static IEdmModel GetEdmModel() | ||
{ | ||
ODataModelBuilder builder = new ODataConventionModelBuilder(); | ||
builder.EntitySet<ContentIdToLocationMappingParent>("ContentIdToLocationMappingParents"); | ||
builder.EntitySet<ContentIdToLocationMappingChild>("ContentIdToLocationMappingChildren"); | ||
builder.Namespace = typeof(ContentIdToLocationMappingParent).Namespace; | ||
|
||
return builder.GetEdmModel(); | ||
} | ||
|
||
[Fact] | ||
public async Task CanResolveContentIdInODataBindAnnotationAsync() | ||
{ | ||
// Arrange | ||
HttpClient client = CreateClient(); | ||
string serviceBase = $"{client.BaseAddress}ContentIdToLocationMapping"; | ||
string requestUri = $"{serviceBase}/$batch"; | ||
string parentsUri = $"{serviceBase}/ContentIdToLocationMappingParents"; | ||
string childrenUri = $"{serviceBase}/ContentIdToLocationMappingChildren"; | ||
string payload = "{" + | ||
" \"requests\": [" + | ||
" {" + | ||
" \"id\": \"1\"," + | ||
" \"method\": \"POST\"," + | ||
$" \"url\": \"{parentsUri}\"," + | ||
" \"headers\": {" + | ||
" \"OData-Version\": \"4.0\"," + | ||
" \"Content-Type\": \"application/json;odata.metadata=minimal\"," + | ||
" \"Accept\": \"application/json;odata.metadata=minimal\"" + | ||
" }," + | ||
" \"body\": {\"ParentId\":123}" + | ||
" }," + | ||
" {" + | ||
" \"id\": \"2\"," + | ||
" \"method\": \"POST\"," + | ||
$" \"url\": \"{childrenUri}\"," + | ||
" \"headers\": {" + | ||
" \"OData-Version\": \"4.0\"," + | ||
" \"Content-Type\": \"application/json;odata.metadata=minimal\"," + | ||
" \"Accept\": \"application/json;odata.metadata=minimal\"" + | ||
" }," + | ||
" \"body\": {" + | ||
" \"Parent@odata.bind\": \"$1\"" + | ||
" }" + | ||
" }" + | ||
" ]" + | ||
"}"; | ||
|
||
// Act | ||
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUri); | ||
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); | ||
request.Content = new StringContent(payload); | ||
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); | ||
HttpResponseMessage response = await client.SendAsync(request); | ||
|
||
// Assert | ||
Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
|
||
var stream = await response.Content.ReadAsStreamAsync(); | ||
IODataResponseMessage odataResponseMessage = new ODataMessageWrapper(stream, response.Content.Headers); | ||
int subResponseCount = 0; | ||
using (var messageReader = new ODataMessageReader(odataResponseMessage, new ODataMessageReaderSettings(), edmModel)) | ||
{ | ||
var batchReader = messageReader.CreateODataBatchReader(); | ||
while (batchReader.Read()) | ||
{ | ||
switch (batchReader.State) | ||
{ | ||
case ODataBatchReaderState.Operation: | ||
var operationMessage = batchReader.CreateOperationResponseMessage(); | ||
subResponseCount++; | ||
Assert.Equal(201, operationMessage.StatusCode); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// NOTE: We assert that $1 is successfully resolved from the controller action | ||
Assert.Equal(2, subResponseCount); | ||
} | ||
} | ||
|
||
public class ContentIdToLocationMappingParentsController : ODataController | ||
{ | ||
public ActionResult Post([FromBody] ContentIdToLocationMappingParent parent) | ||
{ | ||
return Created(new Uri($"{Request.Scheme}://{Request.Host}{Request.Path}/{parent.ParentId}"), parent); | ||
} | ||
} | ||
|
||
public class ContentIdToLocationMappingChildrenController : ODataController | ||
{ | ||
public ActionResult Post([FromBody] ContentIdToLocationMappingChild child) | ||
{ | ||
Assert.Equal(123, child.Parent.ParentId); | ||
habbes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return Created(new Uri($"{Request.Scheme}://{Request.Host}{Request.Path}/{child.ChildId}"), child); | ||
} | ||
} | ||
|
||
public class ContentIdToLocationMappingParent | ||
{ | ||
public ContentIdToLocationMappingParent() | ||
{ | ||
Children = new HashSet<ContentIdToLocationMappingChild>(); | ||
} | ||
|
||
[Key] | ||
public int ParentId | ||
{ | ||
get; set; | ||
} | ||
|
||
public virtual ICollection<ContentIdToLocationMappingChild> Children | ||
{ | ||
get; set; | ||
} | ||
} | ||
|
||
public class ContentIdToLocationMappingChild | ||
{ | ||
[Key] | ||
public int ChildId | ||
{ | ||
get; set; | ||
} | ||
|
||
public int? ParentId | ||
{ | ||
get; set; | ||
} | ||
|
||
public virtual ContentIdToLocationMappingParent Parent | ||
{ | ||
get; set; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this Regex Matching? Is it possible to use
substring
orindexOf
to do the matching instead ofRegex
. I remember some discussions where thisRegex
was being discouraged because of performance reasons.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ElizabethOkerio We use similar logic in
ODataMessageWrapper
class. We're looking for a very particular pattern here - a$
followed by an integer. I think usingSubstring
andIndex
would result into more complex logicThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On an unrelated note, let's avoid substring in favor
Span<char>.Slice()
where possible, to avoid string allocations.