/// <summary> /// Write an <see cref="ODataProperty" /> to the given stream. This method creates an /// async buffered stream and writes the property to it. /// </summary> /// <param name="property">The property to write.</param> internal void WriteTopLevelProperty(ODataProperty property) { Debug.Assert(property != null, "property != null"); Debug.Assert(!(property.Value is ODataStreamReferenceValue), "!(property.Value is ODataStreamReferenceValue)"); if (property.ODataValue == null || property.ODataValue.IsNullValue) { // TODO: Enable updating top-level properties to null #645 throw new ODataException("A null top-level property is not allowed to be serialized."); } this.WriteTopLevelPayload( () => { this.JsonWriter.StartObjectScope(); ODataPayloadKind kind = this.JsonLightOutputContext.MessageWriterSettings.IsIndividualProperty ? ODataPayloadKind.IndividualProperty : ODataPayloadKind.Property; ODataContextUrlInfo contextInfo = ODataContextUrlInfo.Create(property.ODataValue, this.JsonLightOutputContext.MessageWriterSettings.ODataUri, this.Model); this.WriteContextUriProperty(kind, () => contextInfo); // Note we do not allow named stream properties to be written as top level property. this.JsonLightValueSerializer.AssertRecursionDepthIsZero(); this.WriteProperty( property, null /*owningType*/, true /* isTopLevel */, false /* allowStreamProperty */, this.CreateDuplicatePropertyNameChecker()); this.JsonLightValueSerializer.AssertRecursionDepthIsZero(); this.JsonWriter.EndObjectScope(); }); }
/// <summary> /// Start writing a navigation link with content. /// </summary> /// <param name="navigationLink">The navigation link to write.</param> protected override void StartNavigationLinkWithContent(ODataNavigationLink navigationLink) { Debug.Assert(navigationLink != null, "navigationLink != null"); Debug.Assert(!string.IsNullOrEmpty(navigationLink.Name), "The navigation link name should have been verified by now."); if (this.jsonLightOutputContext.WritingResponse) { // Write @odata.context annotation for navigation property var containedEntitySet = this.CurrentScope.NavigationSource as IEdmContainedEntitySet; if (containedEntitySet != null) { ODataContextUrlInfo info = ODataContextUrlInfo.Create( this.CurrentScope.NavigationSource, this.CurrentScope.EntityType.FullName(), containedEntitySet.NavigationProperty.Type.TypeKind() != EdmTypeKind.Collection, this.CurrentScope.ODataUri); this.jsonLightEntryAndFeedSerializer.WriteNavigationLinkContextUrl(navigationLink, info); } // Write the navigation link metadata first. The rest is written by the content entry or feed. this.jsonLightEntryAndFeedSerializer.WriteNavigationLinkMetadata(navigationLink, this.DuplicatePropertyNamesChecker); } else { WriterValidationUtils.ValidateNavigationLinkHasCardinality(navigationLink); } }
public void ValueContextUriShouldNotBeWrittenIfNotProvided() { foreach (ODataVersion version in Versions) { var contextInfo = ODataContextUrlInfo.Create(new ODataProperty().ODataValue, version); this.builderWithNoMetadataDocumentUri.BuildContextUri(ODataPayloadKind.Property, contextInfo).Should().BeNull(); } }
private Uri CreateDeltaResponseContextUri(ODataResourceTypeContext typeContext, ODataDeltaKind kind, ODataVersion version) { ODataContextUrlInfo info = ODataContextUrlInfo.Create(typeContext, version, kind); Uri contextUrl = this.responseContextUriBuilder.BuildContextUri(ODataPayloadKind.Delta, info); contextUrl.Should().NotBeNull(); return(contextUrl); }
private Uri CreateEntryContextUri(ODataResourceTypeContext typeContext, ODataVersion version, bool isResponse = true) { ODataContextUrlInfo info = ODataContextUrlInfo.Create(typeContext, version, true); Uri contextUrl = isResponse ? this.responseContextUriBuilder.BuildContextUri(ODataPayloadKind.Resource, info) : this.requestContextUriBuilder.BuildContextUri(ODataPayloadKind.Resource, info); return(contextUrl); }
private Uri CreateFeedContextUri(ODataResourceTypeContext typeContext, bool isResponse = true) { ODataContextUrlInfo info = ODataContextUrlInfo.Create(typeContext, false); Uri contextUrl = isResponse ? this.responseContextUriBuilder.BuildContextUri(ODataPayloadKind.ResourceSet, info) : this.requestContextUriBuilder.BuildContextUri(ODataPayloadKind.ResourceSet, info); return(contextUrl); }
/// <summary> /// Asynchronously writes the start of a collection. /// </summary> /// <param name="collectionStart">The collection start to write.</param> /// <param name="itemTypeReference">The item type of the collection or null if no metadata is available.</param> internal async Task WriteCollectionStartAsync(ODataCollectionStart collectionStart, IEdmTypeReference itemTypeReference) { Debug.Assert(collectionStart != null, "collectionStart != null"); if (this.writingTopLevelCollection) { // "{" await this.AsynchronousJsonWriter.StartObjectScopeAsync() .ConfigureAwait(false); // "@odata.context":... await this.WriteContextUriPropertyAsync( ODataPayloadKind.Collection, () => ODataContextUrlInfo.Create(collectionStart.SerializationInfo, itemTypeReference)) .ConfigureAwait(false); // "@odata.count":... if (collectionStart.Count.HasValue) { await this.AsynchronousODataAnnotationWriter.WriteInstanceAnnotationNameAsync(ODataAnnotationNames.ODataCount) .ConfigureAwait(false); await this.AsynchronousJsonWriter.WriteValueAsync(collectionStart.Count.Value) .ConfigureAwait(false); } // "@odata.nextlink":... if (collectionStart.NextPageLink != null) { await this.AsynchronousODataAnnotationWriter.WriteInstanceAnnotationNameAsync(ODataAnnotationNames.ODataNextLink) .ConfigureAwait(false); await this.AsynchronousJsonWriter.WriteValueAsync(this.UriToString(collectionStart.NextPageLink)) .ConfigureAwait(false); } // "value": await this.AsynchronousJsonWriter.WriteValuePropertyNameAsync() .ConfigureAwait(false); } // Write the start of the array for the collection items // "[" await this.AsynchronousJsonWriter.StartArrayScopeAsync() .ConfigureAwait(false); }
public void ShouldWriteEntryContextUriWithOperationSegment() { var entitySetSegment = new EntitySetSegment(this.citySet); var keys = new[] { new KeyValuePair <string, object>("Id", 123) }; var keySegment = new KeySegment(keys, cityType, citySet); var operation = edmModel.SchemaElements.OfType <IEdmFunction>().First(f => f.Name == "GetOneDistrict"); OperationSegment operationSegment = new OperationSegment(operation, districtSet); ODataPath path = new ODataPath(entitySetSegment, keySegment, operationSegment); ODataUri odataUri = new ODataUri { Path = path }; ODataContextUrlInfo info = ODataContextUrlInfo.Create(this.districtSet, "TestModel.District", true, odataUri, ODataVersion.V4); Uri uri = this.responseContextUriBuilder.BuildContextUri(ODataPayloadKind.Resource, info); uri.OriginalString.Should().Be(BuildExpectedContextUri("#Districts/$entity", false)); }
public async Task WriteContextUriPropertyAsync_WritesPropertyAnnotationContextUri() { var result = await SetupSerializerAndRunTestAsync( null, async (jsonLightSerializer) => { var property = new ODataProperty { Name = "Prop", Value = 13 }; var contextUrlInfo = ODataContextUrlInfo.Create( property.ODataValue, ODataVersion.V4, jsonLightSerializer.JsonLightOutputContext.MessageWriterSettings.ODataUri, jsonLightSerializer.Model); await jsonLightSerializer.AsynchronousJsonWriter.StartObjectScopeAsync(); await jsonLightSerializer.WriteContextUriPropertyAsync(ODataPayloadKind.Resource, () => contextUrlInfo, /* parentContextUrlInfo */ null, propertyName: "Prop"); }); Assert.Equal("{\"[email protected]\":\"http://example.com/$metadata#Edm.Int32\"", result); }
/// <summary> /// Writes the navigation link metadata. /// </summary> /// <param name="nestedResourceInfo">The navigation link to write the metadata for.</param> /// <param name="contextUrlInfo">The contextUrl information for current element.</param> internal void WriteNestedResourceInfoContextUrl(ODataNestedResourceInfo nestedResourceInfo, ODataContextUrlInfo contextUrlInfo) { this.WriteContextUriProperty(ODataPayloadKind.Resource, () => contextUrlInfo, /* parentContextUrlInfo*/ null, nestedResourceInfo.Name); }
/// <summary> /// Tries to writes the context URI property for a feed into the payload if one is available. /// </summary> /// <param name="typeContext">The context object to answer basic questions regarding the type of the feed.</param> /// <returns>The contextUrlInfo, if the context URI was successfully written.</returns> internal ODataContextUrlInfo WriteFeedContextUri(ODataFeedAndEntryTypeContext typeContext) { ODataUri odataUri = this.JsonLightOutputContext.MessageWriterSettings.ODataUri; return(this.WriteContextUriProperty(ODataPayloadKind.Feed, () => ODataContextUrlInfo.Create(typeContext, /* isSingle */ false, odataUri))); }
/// <summary> /// Tries to writes the context URI property for an entry into the payload if one is available. /// </summary> /// <param name="typeContext">The context object to answer basic questions regarding the type of the entry.</param> /// <param name="parentContextUrlInfo">The parent contextUrlInfo.</param> internal void WriteEntryContextUri(ODataFeedAndEntryTypeContext typeContext, ODataContextUrlInfo parentContextUrlInfo = null) { ODataUri odataUri = this.JsonLightOutputContext.MessageWriterSettings.ODataUri; this.WriteContextUriProperty(ODataPayloadKind.Entry, () => ODataContextUrlInfo.Create(typeContext, /* isSingle */ true, odataUri), parentContextUrlInfo); }
/// <summary> /// Tries to writes the context URI property for delta entry/feed/link into the payload if one is available. /// </summary> /// <param name="typeContext">The context object to answer basic questions regarding the type of the entry.</param> /// <param name="kind">The delta kind to write.</param> /// <param name="parentContextUrlInfo">The parent contextUrlInfo.</param> /// <returns>The created context uri info.</returns> internal ODataContextUrlInfo WriteDeltaContextUri(ODataFeedAndEntryTypeContext typeContext, ODataDeltaKind kind, ODataContextUrlInfo parentContextUrlInfo = null) { ODataUri odataUri = this.JsonLightOutputContext.MessageWriterSettings.ODataUri; return this.WriteContextUriProperty(ODataPayloadKind.Delta, () => ODataContextUrlInfo.Create(typeContext, kind, odataUri), parentContextUrlInfo); }
/// <summary> /// Writes the navigation link metadata. /// </summary> /// <param name="navigationLink">The navigation link to write the metadata for.</param> /// <param name="contextUrlInfo">The contextUrl information for current element.</param> internal void WriteNavigationLinkContextUrl(ODataNavigationLink navigationLink, ODataContextUrlInfo contextUrlInfo) { this.WriteContextUriProperty(ODataPayloadKind.Entry, () => contextUrlInfo, /* parentContextUrlInfo*/ null, navigationLink.Name); }
/// <summary> /// Writes the context URI property and the specified value into the payload. /// </summary> /// <param name="payloadKind">The ODataPayloadKind for the context URI.</param> /// <param name="contextUrlInfoGen">Function to generate contextUrlInfo.</param> /// <param name="parentContextUrlInfo">The parent contextUrlInfo.</param> /// <param name="propertyName">Property name to write contextUri on.</param> /// <returns>The contextUrlInfo, if the context URI was successfully written.</returns> internal ODataContextUrlInfo WriteContextUriProperty(ODataPayloadKind payloadKind, Func<ODataContextUrlInfo> contextUrlInfoGen = null, ODataContextUrlInfo parentContextUrlInfo = null, string propertyName = null) { if (this.jsonLightOutputContext.ContextUrlLevel == ODataContextUrlLevel.None) { return null; } Uri contextUri = null; ODataContextUrlInfo contextUrlInfo = null; if (contextUrlInfoGen != null) { contextUrlInfo = contextUrlInfoGen(); } if (this.jsonLightOutputContext.ContextUrlLevel == ODataContextUrlLevel.OnDemand && contextUrlInfo != null && contextUrlInfo.IsHiddenBy(parentContextUrlInfo)) { return null; } contextUri = this.ContextUriBuilder.BuildContextUri(payloadKind, contextUrlInfo); if (contextUri != null) { if (string.IsNullOrEmpty(propertyName)) { this.JsonWriter.WriteInstanceAnnotationName(ODataAnnotationNames.ODataContext); } else { this.JsonWriter.WritePropertyAnnotationName(propertyName, ODataAnnotationNames.ODataContext); } this.JsonWriter.WritePrimitiveValue(contextUri.AbsoluteUri); this.allowRelativeUri = true; return contextUrlInfo; } return null; }