public void OmitStreamProperty(string streamPropertyNameToOmit, int expectedCount) { var resource = new ODataResource(); ODataMessageWriterSettings settings = new ODataMessageWriterSettings(); TestMetadataSelector selector = new TestMetadataSelector(); selector.PropertyToOmit = streamPropertyNameToOmit; settings.MetadataSelector = selector; ODataResourceMetadataBuilder resourceMetadataBuilder = fullMetadataLevel.CreateResourceMetadataBuilder( resource, personTypeContext, null, HardCodedTestModel.GetDogType(), new SelectedPropertiesNode(SelectedPropertiesNode.SelectionType.EntireSubtree), /*isResponse*/ true, /*keyAsSegment*/ false, /*requestUri*/ null, /*settings*/ settings); fullMetadataLevel.InjectMetadataBuilder(resource, resourceMetadataBuilder); resource.MetadataBuilder.Should().BeSameAs(resourceMetadataBuilder); int count = 0; while (resource.MetadataBuilder.GetNextUnprocessedStreamProperty() != null) { count++; } ; Assert.True(count == expectedCount); }
public void OmitOperations() { var resource = new ODataResource(); ODataMessageWriterSettings settings = new ODataMessageWriterSettings(); settings.MetadataSelector = new TestMetadataSelector(); ODataResourceMetadataBuilder resourceMetadataBuilder = fullMetadataLevel.CreateResourceMetadataBuilder( resource, personTypeContext, null, HardCodedTestModel.GetPersonType(), new SelectedPropertiesNode(SelectedPropertiesNode.SelectionType.EntireSubtree), /*isResponse*/ true, /*keyAsSegment*/ false, /*requestUri*/ null, /*settings*/ settings); fullMetadataLevel.InjectMetadataBuilder(resource, resourceMetadataBuilder); var function = new ODataFunction { Metadata = new Uri(MetadataDocumentUri, "#function1"), }; var action = new ODataAction { Metadata = new Uri(MetadataDocumentUri, "#action2") }; resource.AddFunction(function); resource.AddAction(action); resource.MetadataBuilder.Should().BeSameAs(resourceMetadataBuilder); //metadataselector only allows for two HasHat functions to be written as metadata Assert.True(resource.Functions.Count() == 3); Assert.True(resource.Actions.Count() == 1); }
public void OmitNavigationProperties(string navProperty, int expected) { var resource = new ODataResource(); ODataMessageWriterSettings settings = new ODataMessageWriterSettings(); TestMetadataSelector selector = new TestMetadataSelector(); selector.PropertyToOmit = navProperty; settings.MetadataSelector = selector; ODataResourceMetadataBuilder resourceMetadataBuilder = fullMetadataLevel.CreateResourceMetadataBuilder( resource, personTypeContext, null, HardCodedTestModel.GetPersonType(), new SelectedPropertiesNode(SelectedPropertiesNode.SelectionType.EntireSubtree), /*isResponse*/ true, /*keyAsSegment*/ false, /*requestUri*/ null, /*settings*/ settings); fullMetadataLevel.InjectMetadataBuilder(resource, resourceMetadataBuilder); Assert.Same(resourceMetadataBuilder, resource.MetadataBuilder); int count = 0; while (resource.MetadataBuilder.GetNextUnprocessedNavigationLink() != null) { count++; } ; Assert.True(count == expected); }
/// <summary> /// Sets the metadata builder for this stream reference value. /// </summary> /// <param name="builder">The metadata builder used to compute values from model annotations.</param> /// <param name="propertyName">The property name for the named stream; null for the default media resource.</param> internal void SetMetadataBuilder(ODataResourceMetadataBuilder builder, string propertyName) { this.metadataBuilder = builder; this.edmPropertyName = propertyName; this.computedEditLink = null; this.computedReadLink = null; }
/// <summary> /// Sets the metadata builder for this operation. /// </summary> /// <param name="builder">The metadata builder used to compute values from model annotations.</param> /// <param name="metadataDocumentUri">The metadata document Uri.</param> internal void SetMetadataBuilder(ODataResourceMetadataBuilder builder, Uri metadataDocumentUri) { Debug.Assert(metadataDocumentUri != null, "metadataDocumentUri != null"); Debug.Assert(metadataDocumentUri.IsAbsoluteUri, "metadataDocumentUri.IsAbsoluteUri"); ODataJsonLightValidationUtils.ValidateOperation(metadataDocumentUri, this); this.metadataBuilder = builder; this.operationFullName = ODataJsonLightUtils.GetFullyQualifiedOperationName(metadataDocumentUri, UriUtils.UriToString(this.Metadata), out this.parameterNames); this.computedTitle = null; this.computedTarget = null; }
/// <summary> /// Writes the property information for a property. /// </summary> /// <param name="propertyInfo">The property info to write out.</param> /// <param name="owningType">The owning type for the <paramref name="propertyInfo"/> or null if no metadata is available.</param> /// <param name="isTopLevel">true when writing a top-level property; false for nested properties.</param> /// <param name="duplicatePropertyNameChecker">The DuplicatePropertyNameChecker to use.</param> /// <param name="metadataBuilder">The metadatabuilder for the resource</param> internal void WritePropertyInfo( ODataPropertyInfo propertyInfo, IEdmStructuredType owningType, bool isTopLevel, IDuplicatePropertyNameChecker duplicatePropertyNameChecker, ODataResourceMetadataBuilder metadataBuilder) { WriterValidationUtils.ValidatePropertyNotNull(propertyInfo); string propertyName = propertyInfo.Name; if (this.JsonLightOutputContext.MessageWriterSettings.Validations != ValidationKinds.None) { WriterValidationUtils.ValidatePropertyName(propertyName); } if (!this.JsonLightOutputContext.PropertyCacheHandler.InResourceSetScope()) { this.currentPropertyInfo = new PropertySerializationInfo(this.JsonLightOutputContext.Model, propertyName, owningType) { IsTopLevel = isTopLevel }; } else { this.currentPropertyInfo = this.JsonLightOutputContext.PropertyCacheHandler.GetProperty(this.JsonLightOutputContext.Model, propertyName, owningType); } WriterValidationUtils.ValidatePropertyDefined(this.currentPropertyInfo, this.MessageWriterSettings.ThrowOnUndeclaredPropertyForNonOpenType); duplicatePropertyNameChecker.ValidatePropertyUniqueness(propertyInfo); if (currentPropertyInfo.MetadataType.IsUndeclaredProperty) { WriteODataTypeAnnotation(propertyInfo, isTopLevel); } WriteInstanceAnnotation(propertyInfo, isTopLevel, currentPropertyInfo.MetadataType.IsUndeclaredProperty); ODataStreamPropertyInfo streamInfo = propertyInfo as ODataStreamPropertyInfo; if (streamInfo != null && !(this.JsonLightOutputContext.MetadataLevel is JsonNoMetadataLevel)) { Debug.Assert(!isTopLevel, "Stream properties are not allowed at the top level."); WriteStreamValue(streamInfo, propertyInfo.Name, metadataBuilder); } }
/// <summary> /// Writes property names and value pairs. /// </summary> /// <param name="owningType">The <see cref="IEdmStructuredType"/> of the resource (or null if not metadata is available).</param> /// <param name="properties">The enumeration of properties to write out.</param> /// <param name="isComplexValue"> /// Whether the properties are being written for complex value. Also used for detecting whether stream properties /// are allowed as named stream properties should only be defined on ODataResource instances /// </param> /// <param name="duplicatePropertyNameChecker">The DuplicatePropertyNameChecker to use.</param> /// <param name="metadataBuilder">The metadatabuilder for writing the property.</param> internal void WriteProperties( IEdmStructuredType owningType, IEnumerable <ODataProperty> properties, bool isComplexValue, IDuplicatePropertyNameChecker duplicatePropertyNameChecker, ODataResourceMetadataBuilder metadataBuilder) { if (properties == null) { return; } foreach (ODataProperty property in properties) { this.WriteProperty( property, owningType, false /* isTopLevel */, duplicatePropertyNameChecker, metadataBuilder); } }
public void ReturnSpecifiedNavigationLinkWithFullMetadata() { var resource = new ODataResource(); ODataMessageWriterSettings settings = new ODataMessageWriterSettings(); TestMetadataSelector selector = new TestMetadataSelector(); selector.NavigationPropertyToReturn = new List <IEdmNavigationProperty> { (IEdmNavigationProperty)HardCodedTestModel.GetPersonType().FindProperty("MyFriendsDogs") }; settings.MetadataSelector = selector; ODataResourceMetadataBuilder resourceMetadataBuilder = fullMetadataLevel.CreateResourceMetadataBuilder( resource, personTypeContext, null, HardCodedTestModel.GetPersonType(), new SelectedPropertiesNode(SelectedPropertiesNode.SelectionType.EntireSubtree), /*isResponse*/ true, /*keyAsSegment*/ false, /*requestUri*/ null, /*settings*/ settings); fullMetadataLevel.InjectMetadataBuilder(resource, resourceMetadataBuilder); resource.MetadataBuilder.Should().BeSameAs(resourceMetadataBuilder); int count = 0; while (resource.MetadataBuilder.GetNextUnprocessedNavigationLink() != null) { count++; } ; Assert.True(count == 1); }
/// <summary> /// Injects the appropriate metadata builder based on the metadata level. /// </summary> /// <param name="resource">The resource to inject the builder.</param> /// <param name="builder">The metadata builder to inject.</param> internal override void InjectMetadataBuilder(ODataResourceBase resource, ODataResourceMetadataBuilder builder) { base.InjectMetadataBuilder(resource, builder); // Inject to the Media Resource. var mediaResource = resource.NonComputedMediaResource; if (mediaResource != null) { mediaResource.SetMetadataBuilder(builder, /*propertyName*/ null); } // Inject to named stream property values if (resource.NonComputedProperties != null) { foreach (ODataProperty property in resource.NonComputedProperties) { var streamReferenceValue = property.ODataValue as ODataStreamReferenceValue; if (streamReferenceValue != null) { streamReferenceValue.SetMetadataBuilder(builder, property.Name); } } } // Inject to operations IEnumerable <ODataOperation> operations = ODataUtilsInternal.ConcatEnumerables((IEnumerable <ODataOperation>)resource.NonComputedActions, (IEnumerable <ODataOperation>)resource.NonComputedFunctions); if (operations != null) { foreach (ODataOperation operation in operations) { operation.SetMetadataBuilder(builder, this.NonNullMetadataDocumentUri); } } }
internal static void GetOperationTitleShouldValidateArguments(ODataResourceMetadataBuilder builder) { Action <string> action = (value) => builder.GetOperationTitle(value); action.ShouldThrowOnNullOrEmptyStringArgument("operationName"); }
internal static void GetAssociationLinkUriShouldValidateArguments(ODataResourceMetadataBuilder builder) { Action <string> action = (value) => builder.GetAssociationLinkUri(value, null, false); action.ShouldThrowOnNullOrEmptyStringArgument("navigationPropertyName"); }
internal static void GetStreamReadLinkShouldValidateArguments(ODataResourceMetadataBuilder builder) { Action <string> action = (value) => builder.GetStreamReadLink(value); action.ShouldThrowOnEmptyStringArgument("streamPropertyName"); }
private void WriteStreamValue(IODataStreamReferenceInfo streamInfo, string propertyName, ODataResourceMetadataBuilder metadataBuilder) { WriterValidationUtils.ValidateStreamPropertyInfo(streamInfo, currentPropertyInfo.MetadataType.EdmProperty, propertyName, this.WritingResponse); this.WriteStreamInfo(propertyName, streamInfo); if (metadataBuilder != null) { metadataBuilder.MarkStreamPropertyProcessed(propertyName); } }
internal void WriteProperty( ODataProperty property, IEdmStructuredType owningType, bool isTopLevel, IDuplicatePropertyNameChecker duplicatePropertyNameChecker, ODataResourceMetadataBuilder metadataBuilder) { this.WritePropertyInfo(property, owningType, isTopLevel, duplicatePropertyNameChecker, metadataBuilder); ODataValue value = property.ODataValue; // handle ODataUntypedValue ODataUntypedValue untypedValue = value as ODataUntypedValue; if (untypedValue != null) { WriteUntypedValue(untypedValue); return; } ODataStreamReferenceValue streamReferenceValue = value as ODataStreamReferenceValue; if (streamReferenceValue != null && !(this.JsonLightOutputContext.MetadataLevel is JsonNoMetadataLevel)) { Debug.Assert(!isTopLevel, "Stream properties are not allowed at the top level."); WriteStreamValue(streamReferenceValue, property.Name, metadataBuilder); return; } if (value is ODataNullValue || value == null) { this.WriteNullProperty(property); return; } bool isOpenPropertyType = this.IsOpenProperty(property); ODataPrimitiveValue primitiveValue = value as ODataPrimitiveValue; if (primitiveValue != null) { this.WritePrimitiveProperty(primitiveValue, isOpenPropertyType); return; } ODataEnumValue enumValue = value as ODataEnumValue; if (enumValue != null) { this.WriteEnumProperty(enumValue, isOpenPropertyType); return; } ODataResourceValue resourceValue = value as ODataResourceValue; if (resourceValue != null) { if (isTopLevel) { throw new ODataException(Strings.ODataMessageWriter_NotAllowedWriteTopLevelPropertyWithResourceValue(property.Name)); } this.WriteResourceProperty(property, resourceValue, isOpenPropertyType); return; } ODataCollectionValue collectionValue = value as ODataCollectionValue; if (collectionValue != null) { if (isTopLevel) { if (collectionValue.Items != null && collectionValue.Items.Any(i => i is ODataResourceValue)) { throw new ODataException(Strings.ODataMessageWriter_NotAllowedWriteTopLevelPropertyWithResourceValue(property.Name)); } } this.WriteCollectionProperty(collectionValue, isOpenPropertyType); return; } ODataBinaryStreamValue streamValue = value as ODataBinaryStreamValue; if (streamValue != null) { this.WriteStreamProperty(streamValue, isOpenPropertyType); return; } }
/// <summary> /// Injects the appropriate metadata builder based on the metadata level. /// </summary> /// <param name="resource">The resource to inject the builder.</param> /// <param name="builder">The metadata builder to inject.</param> internal virtual void InjectMetadataBuilder(ODataResourceBase resource, ODataResourceMetadataBuilder builder) { resource.MetadataBuilder = builder; }
/// <summary> /// Injects the appropriate metadata builder based on the metadata level. /// </summary> /// <param name="resource">The resource to inject the builder.</param> /// <param name="builder">The metadata builder to inject.</param> internal override void InjectMetadataBuilder(ODataResourceBase resource, ODataResourceMetadataBuilder builder) { // For minimal metadata we don't want to change the metadata builder that's currently on the resource because the resource might come from a JSON light // reader and it would contain the metadata builder from the reader. Until we give the user the ability to choose whether to write what was reported // by the reader versus what was on the wire, we no-op here so the writer will just write what's on the OM for now. }