-
Notifications
You must be signed in to change notification settings - Fork 351
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
POC for writing instance annotations and odata.count for expanded resource sets #2194
Draft
KenitoInc
wants to merge
5
commits into
OData:release-7.x
Choose a base branch
from
KenitoInc:instance-annotations-poc
base: release-7.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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 |
---|---|---|
|
@@ -8,7 +8,9 @@ namespace Microsoft.OData | |
{ | ||
#region Namespaces | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.OData.Evaluation; | ||
#endregion Namespaces | ||
|
||
|
@@ -33,6 +35,9 @@ public sealed class ODataNestedResourceInfo : ODataItem | |
/// <summary>true if the association link has been set by the user or seen on the wire or computed by the metadata builder, false otherwise.</summary> | ||
private bool hasAssociationUrl; | ||
|
||
/// <summary>The number of items in the resource set.</summary> | ||
private long? count; | ||
|
||
/// <summary>Gets or sets a value that indicates whether the nested resource info represents a collection or a resource.</summary> | ||
/// <returns>true if the nested resource info represents a collection; false if the navigation represents a resource.</returns> | ||
public bool? IsCollection | ||
|
@@ -92,6 +97,36 @@ public Uri AssociationLinkUrl | |
} | ||
} | ||
|
||
/// <summary>Gets or sets the number of items in the resource set.</summary> | ||
/// <returns>The number of items in the resource set.</returns> | ||
public long? Count | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about if the nested resource info is single? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added a validation! |
||
{ | ||
get | ||
{ | ||
return this.count; | ||
} | ||
|
||
set | ||
{ | ||
if (IsCollection != null && (bool)IsCollection == false) | ||
{ | ||
throw new ODataException(Strings.ODataWriterCore_QueryCountInODataNestedResourceInfo); | ||
} | ||
|
||
this.count = value; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Collection of custom instance annotations. | ||
/// </summary> | ||
[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "We want to allow the same instance annotation collection instance to be shared across ODataLib OM instances.")] | ||
public ICollection<ODataInstanceAnnotation> InstanceAnnotations | ||
{ | ||
get { return this.GetInstanceAnnotations(); } | ||
set { this.SetInstanceAnnotations(value); } | ||
} | ||
|
||
/// <summary>Gets or sets the context url for this nested resource info.</summary> | ||
/// <returns>The URI representing the context url of the nested resource info.</returns> | ||
internal Uri ContextUrl { get; set; } | ||
|
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
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 |
---|---|---|
|
@@ -555,6 +555,54 @@ private void WriteAnnotationAtEndExpandedFeedShouldFail(ODataFormat format) | |
testRequestOfSingleton.Throws<ODataException>(Strings.ODataJsonLightWriter_InstanceAnnotationNotSupportedOnExpandedResourceSet); | ||
} | ||
|
||
[Fact] | ||
public void WriteAnnotationAndCountAtStartExpandedFeedShouldPassInJsonLight() | ||
{ | ||
string expectedPayload = | ||
"{" + | ||
"\"@odata.context\":\"http://www.example.com/$metadata#TestEntitySet/$entity\"," + | ||
"\"ID\":1," + | ||
"\"[email protected]\":\"http://service/navLink\"," + | ||
"\"[email protected]\":10," + | ||
"\"[email protected]\":123" + | ||
"}"; | ||
|
||
Action<ODataWriter> action = (odataWriter) => | ||
{ | ||
var entryToWrite = new ODataResource { Properties = new[] { new ODataProperty { Name = "ID", Value = 1 } } }; | ||
odataWriter.WriteStart(entryToWrite); | ||
|
||
ODataNestedResourceInfo navLink = new ODataNestedResourceInfo { Name = "ResourceSetNavigationProperty", Url = new Uri("http://service/navLink", UriKind.RelativeOrAbsolute), IsCollection = true }; | ||
navLink.Count = 10; | ||
navLink.InstanceAnnotations.Add(new ODataInstanceAnnotation("custom.StartFeedAnnotation", PrimitiveValue1)); | ||
odataWriter.WriteStart(navLink); | ||
|
||
odataWriter.WriteEnd(); | ||
odataWriter.WriteEnd(); | ||
}; | ||
|
||
this.WriteAnnotationsAndValidatePayload(action, EntitySet, ODataFormat.Json, expectedPayload, request: false, createFeedWriter: false); | ||
} | ||
|
||
[Fact] | ||
public void WriteCountOnExpandedEntryShouldFail() | ||
{ | ||
Action<ODataWriter> action = (odataWriter) => | ||
{ | ||
var entryToWrite = new ODataResource { Properties = new[] { new ODataProperty { Name = "ID", Value = 1 } } }; | ||
odataWriter.WriteStart(entryToWrite); | ||
|
||
ODataNestedResourceInfo navLink = new ODataNestedResourceInfo { Name = "ResourceNavigationProperty", IsCollection = false }; | ||
navLink.Count = 10; | ||
|
||
odataWriter.WriteStart(navLink); | ||
odataWriter.WriteEnd(); | ||
odataWriter.WriteEnd(); | ||
}; | ||
|
||
Action testResponse = () => this.WriteAnnotationsAndValidatePayload(action, EntitySet, ODataFormat.Json, null, request: false, createFeedWriter: false); | ||
testResponse.Throws<ODataException>(Strings.ODataWriterCore_QueryCountInODataNestedResourceInfo); | ||
} | ||
#endregion Writing instance annotations on expanded feeds | ||
|
||
#region Write Delta Feed | ||
|
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 |
---|---|---|
|
@@ -89,6 +89,20 @@ static CustomInstanceAnnotationAcceptanceTests() | |
"\"@Custom.FeedEndAnnotation\":1" + | ||
"}"; | ||
|
||
const string JsonLightExpandedResourceSetPayloadWithCountAndCustomInstanceAnnotations = | ||
"{" + | ||
"\"@odata.context\":\"http://www.example.com/service.svc/$metadata#TestEntitySet\"," + | ||
"\"value\":[" + | ||
"{" + | ||
"\"ID\":1," + | ||
"\"[email protected]\":\"http://example.com/multiple\"," + | ||
"\"[email protected]\":10," + | ||
"\"[email protected]\":\"123\"," + | ||
"\"[email protected]\":123" + | ||
"}" + | ||
"]" + | ||
"}"; | ||
|
||
[Fact] | ||
public void CustomInstanceAnnotationFromFeedAndEntryInJsonLightShouldBeSkippedByTheReaderByDefault() | ||
{ | ||
|
@@ -121,6 +135,48 @@ public void CustomInstanceAnnotationFromFeedAndEntryInJsonLightShouldBeSkippedBy | |
} | ||
} | ||
|
||
[Fact] | ||
public void ShouldBeAbleToReadCountAndCustomInstanceAnnotationsFromExpandedResourceSetsInJsonLight() | ||
{ | ||
var stream = new MemoryStream(Encoding.UTF8.GetBytes(JsonLightExpandedResourceSetPayloadWithCountAndCustomInstanceAnnotations)); | ||
var readerSettings = new ODataMessageReaderSettings { EnableMessageStreamDisposal = true }; | ||
|
||
IODataResponseMessage messageToRead = new InMemoryMessage { StatusCode = 200, Stream = stream }; | ||
messageToRead.SetHeader("Content-Type", "application/json;odata.streaming=true"); | ||
|
||
// Enable reading custom instance annotations. | ||
//messageToRead.PreferenceAppliedHeader().AnnotationFilter = "Custom.*"; | ||
messageToRead.PreferenceAppliedHeader().AnnotationFilter = "*"; | ||
|
||
using (var messageReader = new ODataMessageReader(messageToRead, readerSettings, Model)) | ||
{ | ||
var odataReader = messageReader.CreateODataResourceSetReader(EntitySet, EntityType); | ||
while (odataReader.Read()) | ||
{ | ||
switch (odataReader.State) | ||
{ | ||
case ODataReaderState.NestedResourceInfoStart: | ||
break; | ||
case ODataReaderState.NestedResourceInfoEnd: | ||
ODataNestedResourceInfo navigationLink = (ODataNestedResourceInfo)odataReader.Item; | ||
|
||
if (navigationLink.Name == "ResourceSetNavigationProperty") | ||
{ | ||
Assert.Equal("ResourceSetNavigationProperty", navigationLink.Name); | ||
Assert.Equal(10, navigationLink.Count); | ||
Assert.Equal(2, navigationLink.InstanceAnnotations.Count); | ||
Assert.Equal("custom.MyAnnotation", navigationLink.InstanceAnnotations.First().Name); | ||
Assert.Equal("custom.StartFeedAnnotation", navigationLink.InstanceAnnotations.Last().Name); | ||
Assert.Equal(true, navigationLink.IsCollection); | ||
Assert.Equal(new Uri("http://example.com/multiple"), navigationLink.Url); | ||
Assert.Equal(new Uri("http://www.example.com/service.svc/TestEntitySet(1)/ResourceSetNavigationProperty/$ref"), navigationLink.AssociationLinkUrl); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ShouldBeAbleToReadCustomInstanceAnnotationFromFeedAndEntryInJsonLight() | ||
{ | ||
|
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.
Noet: you don't really need the section for a property -- you cover what it returns in the summary.