public override object ReadInline(ODataFeed feed, ODataDeserializerContext readContext) { if (readContext == null) { throw Error.ArgumentNull("readContext"); } if (feed == null) { return(null); } ODataEntryDeserializer deserializer = DeserializerProvider.GetODataDeserializer(_edmEntityType); IList feedValue = CreateNewCollection(EdmLibHelpers.GetClrType(_edmEntityType, EdmModel)); ODataFeedAnnotation feedAnnotation = feed.GetAnnotation <ODataFeedAnnotation>(); Contract.Assert(feedAnnotation != null, "Each feed we create should gave annotation on it."); foreach (ODataEntry entry in feedAnnotation) { ODataEntryAnnotation annotation = entry.GetAnnotation <ODataEntryAnnotation>(); Contract.Assert(annotation != null); feedValue.Add(deserializer.ReadInline(entry, readContext)); } return(feedValue); }
public void ApplyProperty_AppliesKeyProperty_WhenPatchKeyModeIsPatch() { // Arrange ODataProperty property = new ODataProperty { Name = "Key1", Value = "Value1" }; EdmEntityType entityType = new EdmEntityType("namespace", "name"); entityType.AddKeys(new EdmStructuralProperty(entityType, "Key1", EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(string)))); EdmEntityTypeReference entityTypeReference = new EdmEntityTypeReference(entityType, isNullable: false); ODataDeserializerProvider provider = new DefaultODataDeserializerProvider(EdmCoreModel.Instance); var resource = new Mock <IDelta>(MockBehavior.Strict); Type propertyType = typeof(string); resource.Setup(r => r.TryGetPropertyType("Key1", out propertyType)).Returns(true); resource.Setup(r => r.TrySetPropertyValue("Key1", "Value1")).Returns(true).Verifiable(); // Act ODataEntryDeserializer.ApplyProperty(property, entityTypeReference, resource.Object, provider, new ODataDeserializerContext { IsPatchMode = true, PatchKeyMode = PatchKeyMode.Patch }); // Assert resource.Verify(); }
public void GetPropertyType_NonDelta(string propertyName, bool isDelta, Type expectedPropertyType) { object resource = isDelta ? (object)new Delta <GetPropertyType_TestClass>() : new GetPropertyType_TestClass(); Assert.Equal( expectedPropertyType, ODataEntryDeserializer.GetPropertyType(resource, propertyName, isDelta)); }
/// <summary> /// Sets the <see cref="ODataEntryDeserializer"/> for the given edmType in the deserializer cache. /// </summary> /// <param name="edmType">The EDM type.</param> /// <param name="deserializer">The deserializer to use for the given EDM type.</param> public void SetEdmTypeDeserializer(IEdmTypeReference edmType, ODataEntryDeserializer deserializer) { if (edmType == null) { throw Error.ArgumentNull("edmType"); } _deserializerCache.AddOrUpdate(edmType, deserializer, (t, s) => deserializer); }
public void ReadInline_ThrowsArgument_TypeMismatch(Type deserializerType) { ODataEntryDeserializer deserializer = GetType().GetMethod("CreateDeserializer").MakeGenericMethod(deserializerType).Invoke(null, null) as ODataEntryDeserializer; ArgumentException ex = Assert.ThrowsArgument( () => deserializer.ReadInline("type mismatch item", new ODataDeserializerContext()), "item"); Assert.True(ex.Message.StartsWith(String.Format("The argument must be of type '{0}'.", deserializerType.Name))); }
private static object ConvertCollectionValue(ODataCollectionValue collection, IEdmTypeReference propertyType, ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext) { IEdmCollectionTypeReference collectionType = propertyType as IEdmCollectionTypeReference; Contract.Assert(collectionType != null, "The type for collection must be a IEdmCollectionType."); ODataEntryDeserializer deserializer = deserializerProvider.GetODataDeserializer(collectionType); return(deserializer.ReadInline(collection, readContext)); }
private object Convert(object value, IEdmTypeReference parameterType, ODataDeserializerContext readContext) { if (parameterType.IsPrimitive()) { return(value); } else { ODataEntryDeserializer deserializer = _provider.GetODataDeserializer(parameterType); return(deserializer.ReadInline(value, readContext)); } }
private object CreateNestedEntityAndApplyProperties(ODataEntry entry, IEdmEntityTypeReference elementType, ODataDeserializerReadContext readContext) { ODataEntryAnnotation annotation = entry.GetAnnotation <ODataEntryAnnotation>(); Contract.Assert(annotation != null); CreateEntityResource(annotation, elementType, readContext); ODataEntryDeserializer deserializer = DeserializerProvider.GetODataDeserializer(elementType); return(deserializer.ReadInline(entry, readContext)); }
private IEnumerable ReadItems(ODataFeed feed, ODataDeserializerContext readContext) { ODataEntryDeserializer deserializer = DeserializerProvider.GetODataDeserializer(_edmEntityType); ODataFeedAnnotation feedAnnotation = feed.GetAnnotation<ODataFeedAnnotation>(); Contract.Assert(feedAnnotation != null, "Each feed we create should gave annotation on it."); foreach (ODataEntry entry in feedAnnotation) { ODataEntryAnnotation annotation = entry.GetAnnotation<ODataEntryAnnotation>(); Contract.Assert(annotation != null); yield return deserializer.ReadInline(entry, readContext); } }
private void ApplyEntryInNavigationProperty(IEdmNavigationProperty navigationProperty, object entityResource, ODataEntry entry, ODataDeserializerContext readContext) { Contract.Assert(navigationProperty != null && navigationProperty.PropertyKind == EdmPropertyKind.Navigation, "navigationProperty != null && navigationProperty.TypeKind == ResourceTypeKind.EntityType"); Contract.Assert(entityResource != null, "entityResource != null"); ODataEntryDeserializer deserializer = DeserializerProvider.GetODataDeserializer(navigationProperty.Type); object value = deserializer.ReadInline(entry, readContext); if (readContext.IsPatchMode) { throw Error.InvalidOperation(SRResources.CannotPatchNavigationProperties, navigationProperty.Name, navigationProperty.DeclaringEntityType().FullName()); } SetProperty(entityResource, navigationProperty.Name, isDelta: false, value: value); }
private void ApplyFeedInNavigationProperty(IEdmNavigationProperty navigationProperty, object entityResource, ODataFeed feed, ODataDeserializerContext readContext) { ODataFeedAnnotation feedAnnotation = feed.GetAnnotation <ODataFeedAnnotation>(); Contract.Assert(feedAnnotation != null, "Each feed we create should gave annotation on it."); ODataEntryDeserializer deserializer = DeserializerProvider.GetODataDeserializer(navigationProperty.Type); object value = deserializer.ReadInline(feed, readContext); if (readContext.IsPatchMode) { throw Error.InvalidOperation(SRResources.CannotPatchNavigationProperties, navigationProperty.Name, navigationProperty.DeclaringEntityType().FullName()); } SetProperty(entityResource, navigationProperty.Name, isDelta: false, value: value); }
private static object ConvertComplexValue(ODataComplexValue complexValue, ref IEdmTypeReference propertyType, ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext) { IEdmComplexTypeReference edmComplexType; if (propertyType == null) { // open complex property Contract.Assert(!String.IsNullOrEmpty(complexValue.TypeName), "ODataLib should have verified that open complex value has a type name since we provided metadata."); IEdmType edmType = deserializerProvider.EdmModel.FindType(complexValue.TypeName); Contract.Assert(edmType.TypeKind == EdmTypeKind.Complex, "ODataLib should have verified that complex value has a complex resource type."); edmComplexType = new EdmComplexTypeReference(edmType as IEdmComplexType, isNullable: true); } else { edmComplexType = propertyType.AsComplex(); } ODataEntryDeserializer deserializer = deserializerProvider.GetODataDeserializer(edmComplexType); return(deserializer.ReadInline(complexValue, readContext)); }
public void ApplyProperty_ThrowsOnKeyProperty_WhenPatchKeyModeIsThrow() { // Arrange ODataProperty property = new ODataProperty { Name = "Key1", Value = "Value1" }; EdmEntityType entityType = new EdmEntityType("namespace", "name"); entityType.AddKeys(new EdmStructuralProperty(entityType, "Key1", EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(string)))); EdmEntityTypeReference entityTypeReference = new EdmEntityTypeReference(entityType, isNullable: false); ODataDeserializerProvider provider = new DefaultODataDeserializerProvider(EdmCoreModel.Instance); var resource = new Mock <IDelta>(MockBehavior.Strict); // Act && Assert Assert.Throws <InvalidOperationException>( () => ODataEntryDeserializer.ApplyProperty(property, entityTypeReference, resource.Object, provider, new ODataDeserializerContext { IsPatchMode = true, PatchKeyMode = PatchKeyMode.Throw }), "Cannot apply PATCH on key property 'Key1' on entity type 'namespace.name' when 'PatchKeyMode' is 'Throw'."); }
private IEnumerable ReadItems(ODataCollectionValue collection, ODataDeserializerContext readContext) { RecurseEnter(readContext); IEdmTypeReference elementType = _edmCollectionType.ElementType(); ODataEntryDeserializer deserializer = DeserializerProvider.GetODataDeserializer(elementType); Contract.Assert(deserializer != null); foreach (object entry in collection.Items) { if (elementType.IsPrimitive()) { yield return(entry); } else { yield return(deserializer.ReadInline(entry, readContext)); } } RecurseLeave(readContext); }
public override object ReadInline(ODataCollectionValue collection, ODataDeserializerContext readContext) { if (readContext == null) { throw Error.ArgumentNull("readContext"); } if (collection == null) { return(null); } RecurseEnter(readContext); IEdmTypeReference elementType = _edmCollectionType.ElementType(); ODataEntryDeserializer deserializer = DeserializerProvider.GetODataDeserializer(elementType); Contract.Assert(deserializer != null); IList collectionValue = CreateNewCollection(EdmLibHelpers.GetClrType(elementType, EdmModel)); foreach (object entry in collection.Items) { if (elementType.IsPrimitive()) { collectionValue.Add(entry); } else { collectionValue.Add(deserializer.ReadInline(entry, readContext)); } } RecurseLeave(readContext); return(collectionValue); }
public void ConvertPrimitiveValue_NonStandardPrimitives(object valueToConvert, object result, Type conversionType) { Assert.Equal(result.GetType(), ODataEntryDeserializer.ConvertPrimitiveValue(valueToConvert, conversionType, "", "").GetType()); Assert.Equal(result.ToString(), ODataEntryDeserializer.ConvertPrimitiveValue(valueToConvert, conversionType, "", "").ToString()); }
public void ConvertPrimitiveValueToNullableChar_Throws() { Assert.Throws <ValidationException>( () => ODataEntryDeserializer.ConvertPrimitiveValue("123", typeof(char?), "property", "type"), "The property 'property' on type 'type' must be a string with a maximum length of 1."); }
public void ConvertPrimitiveValueToChar_Throws(string input) { Assert.Throws <ValidationException>( () => ODataEntryDeserializer.ConvertPrimitiveValue(input, typeof(char), "property", "type"), "The property 'property' on type 'type' must be a string with a length of 1."); }
public void ConvertPrimitiveValueToXElement_Throws_IfInputIsNotString() { Assert.Throws <ValidationException>( () => ODataEntryDeserializer.ConvertPrimitiveValue(123, typeof(XElement), "property", "type"), "The property 'property' on type 'type' must be a string."); }