public void AddActionShouldAddActionToFeed() { ODataFeed feed = new ODataFeed(); feed.Actions.Count().Should().Be(0); feed.AddAction(new ODataAction()); feed.Actions.Count().Should().Be(1); feed.AddAction(new ODataAction()); feed.Actions.Count().Should().Be(2); }
/// <summary> /// Creates a new instance of ODataAction or ODataFunction for the <paramref name="metadataReferencePropertyName"/>. /// </summary> /// <param name="feed">The feed to add the action or function .</param> /// <param name="metadataReferencePropertyName">The name of the metadata reference property being read.</param> /// <returns>A new instance of ODataAction or ODataFunction for the <paramref name="metadataReferencePropertyName"/>.</returns> private ODataOperation CreateODataOperationAndAddToFeed(ODataFeed feed, string metadataReferencePropertyName) { string fullyQualifiedOperationName = ODataJsonLightUtils.GetUriFragmentFromMetadataReferencePropertyName(this.ContextUriParseResult.MetadataDocumentUri, metadataReferencePropertyName); IEdmOperation firstActionOrFunction = this.JsonLightInputContext.Model.ResolveOperations(fullyQualifiedOperationName).FirstOrDefault(); bool isAction; if (firstActionOrFunction == null) { // Ignore the unknown function/action. return null; } var operation = ODataJsonLightUtils.CreateODataOperation(this.ContextUriParseResult.MetadataDocumentUri, metadataReferencePropertyName, firstActionOrFunction, out isAction); if (isAction) { feed.AddAction((ODataAction)operation); } else { feed.AddFunction((ODataFunction)operation); } return operation; }
/// <summary> /// Create the <see cref="ODataFeed"/> to be written for the given feed instance. /// </summary> /// <param name="feedInstance">The instance representing the feed being written.</param> /// <param name="feedType">The EDM type of the feed being written.</param> /// <param name="writeContext">The serializer context.</param> /// <returns>The created <see cref="ODataFeed"/> object.</returns> public virtual ODataFeed CreateODataFeed(IEnumerable feedInstance, IEdmCollectionTypeReference feedType, ODataSerializerContext writeContext) { ODataFeed feed = new ODataFeed(); if (writeContext.NavigationSource != null) { FeedContext feedContext = new FeedContext { Request = writeContext.Request, RequestContext = writeContext.RequestContext, EntitySetBase = writeContext.NavigationSource as IEdmEntitySetBase, Url = writeContext.Url, FeedInstance = feedInstance }; IEdmEntityType entityType = GetEntityType(feedType).Definition as IEdmEntityType; var operations = writeContext.Model.GetAvailableOperationsBoundToCollection(entityType); var odataOperations = CreateODataOperations(operations, feedContext, writeContext); foreach (var odataOperation in odataOperations) { ODataAction action = odataOperation as ODataAction; if (action != null) { feed.AddAction(action); } else { feed.AddFunction((ODataFunction)odataOperation); } } } if (writeContext.ExpandedEntity == null) { // If we have more OData format specific information apply it now, only if we are the root feed. PageResult odataFeedAnnotations = feedInstance as PageResult; if (odataFeedAnnotations != null) { feed.Count = odataFeedAnnotations.Count; feed.NextPageLink = odataFeedAnnotations.NextPageLink; } else if (writeContext.Request != null) { feed.NextPageLink = writeContext.Request.ODataProperties().NextLink; long?countValue = writeContext.Request.ODataProperties().TotalCount; if (countValue.HasValue) { feed.Count = countValue.Value; } } } else { // nested feed ITruncatedCollection truncatedCollection = feedInstance as ITruncatedCollection; if (truncatedCollection != null && truncatedCollection.IsTruncated) { feed.NextPageLink = GetNestedNextPageLink(writeContext, truncatedCollection.PageSize); } ICountOptionCollection countOptionCollection = feedInstance as ICountOptionCollection; if (countOptionCollection != null && countOptionCollection.TotalCount != null) { feed.Count = countOptionCollection.TotalCount; } } return(feed); }
public void WritingFeedWithFunctionAndAction() { ODataFeed feed = new ODataFeed(); feed.AddAction(new ODataAction { Metadata = new Uri("http://example.org/odata.svc/$metadata#Action"), Target = new Uri("http://example.org/odata.svc/DoAction"), Title = "ActionTitle" }); feed.AddFunction(new ODataFunction() { Metadata = new Uri("http://example.org/odata.svc/$metadata#Function"), Target = new Uri("http://example.org/odata.svc/DoFunction"), Title = "FunctionTitle" }); ODataItem[] itemsToWrite = new ODataItem[] { feed, this.entryWithOnlyData1, }; string result = this.GetWriterOutputForContentTypeAndKnobValue("application/json;odata.metadata=minimal", true, itemsToWrite, Model, EntitySet, EntityType); const string expectedPayload = "{\"" + "@odata.context\":\"http://example.org/odata.svc/$metadata#EntitySet\"," + "\"#Action\":{" + "\"title\":\"ActionTitle\"," + "\"target\":\"http://example.org/odata.svc/DoAction\"" + "}," + "\"#Function\":{" + "\"title\":\"FunctionTitle\"," + "\"target\":\"http://example.org/odata.svc/DoFunction\"" + "}," + "\"value\":[" + "{" + "\"ID\":101,\"Name\":\"Alice\"" + "}" + "]" + "}"; result.Should().Be(expectedPayload); }