private static object ConvertResource(ODataMessageReader oDataMessageReader, IEdmTypeReference edmTypeReference, ODataDeserializerContext readContext) { EdmEntitySet tempEntitySet = null; if (edmTypeReference.IsEntity()) { IEdmEntityTypeReference entityType = edmTypeReference.AsEntity(); tempEntitySet = new EdmEntitySet(readContext.Model.EntityContainer, "temp", entityType.EntityDefinition()); } // TODO: Sam xu, can we use the parameter-less overload ODataReader resourceReader = oDataMessageReader.CreateODataUriParameterResourceReader(tempEntitySet, edmTypeReference.ToStructuredType()); object item = resourceReader.ReadResourceOrResourceSet(); ODataResourceWrapper topLevelResource = item as ODataResourceWrapper; Contract.Assert(topLevelResource != null); IODataDeserializerProvider deserializerProvider = readContext.Request.GetDeserializerProvider(); ODataResourceDeserializer entityDeserializer = (ODataResourceDeserializer)deserializerProvider.GetEdmTypeDeserializer(edmTypeReference); return(entityDeserializer.ReadInline(topLevelResource, edmTypeReference, readContext)); }
public ODataSingletonDeserializerTest() { EdmModel model = new EdmModel(); var employeeType = new EdmEntityType("NS", "Employee"); employeeType.AddStructuralProperty("EmployeeId", EdmPrimitiveTypeKind.Int32); employeeType.AddStructuralProperty("EmployeeName", EdmPrimitiveTypeKind.String); model.AddElement(employeeType); EdmEntityContainer defaultContainer = new EdmEntityContainer("NS", "Default"); model.AddElement(defaultContainer); _singleton = new EdmSingleton(defaultContainer, "CEO", employeeType); defaultContainer.AddElement(_singleton); model.SetAnnotationValue <ClrTypeAnnotation>(employeeType, new ClrTypeAnnotation(typeof(EmployeeModel))); _edmModel = model; _readContext = new ODataDeserializerContext { Path = new ODataPath(new SingletonSegment(_singleton)), Model = _edmModel, ResourceType = typeof(EmployeeModel) }; _deserializerProvider = DeserializationServiceProviderHelper.GetServiceProvider().GetRequiredService <IODataDeserializerProvider>(); }
private static object ConvertEnumValue(ODataEnumValue enumValue, ref IEdmTypeReference propertyType, IODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext) { IEdmEnumTypeReference edmEnumType; if (propertyType == null) { // dynamic enum property Contract.Assert(!String.IsNullOrEmpty(enumValue.TypeName), "ODataLib should have verified that dynamic enum value has a type name since we provided metadata."); IEdmModel model = readContext.Model; IEdmType edmType = model.FindType(enumValue.TypeName); Contract.Assert(edmType.TypeKind == EdmTypeKind.Enum, "ODataLib should have verified that enum value has a enum resource type."); edmEnumType = new EdmEnumTypeReference(edmType as IEdmEnumType, isNullable: true); propertyType = edmEnumType; } else { edmEnumType = propertyType.AsEnum(); } IODataEdmTypeDeserializer deserializer = deserializerProvider.GetEdmTypeDeserializer(edmEnumType); return(deserializer.ReadInline(enumValue, propertyType, readContext)); }
static ODataActionPayloadDeserializerTest() { _model = GetModel(); _container = _model.EntityContainer; _deserializerProvider = DeserializationServiceProviderHelper.GetServiceProvider().GetRequiredService <IODataDeserializerProvider>(); _deserializer = new ODataActionPayloadDeserializer(_deserializerProvider); }
public ODataResourceSetDeserializerTests() { _model = GetEdmModel(); _customerType = _model.GetEdmTypeReference(typeof(Customer)).AsEntity(); _customersType = new EdmCollectionTypeReference(new EdmCollectionType(_customerType)); _serializerProvider = ODataFormatterHelpers.GetSerializerProvider(); _deserializerProvider = ODataFormatterHelpers.GetDeserializerProvider(); }
/// <summary> /// Initializes a new instance of the <see cref="ODataEdmTypeDeserializer"/> class. /// </summary> /// <param name="payloadKind">The kind of OData payload this deserializer handles.</param> /// <param name="deserializerProvider">The <see cref="IODataDeserializerProvider"/>.</param> protected ODataEdmTypeDeserializer(ODataPayloadKind payloadKind, IODataDeserializerProvider deserializerProvider) : this(payloadKind) { if (deserializerProvider == null) { throw Error.ArgumentNull("deserializerProvider"); } DeserializerProvider = deserializerProvider; }
/// <summary> /// Initializes a new instance of the <see cref="ODataActionPayloadDeserializer"/> class. /// </summary> /// <param name="deserializerProvider">The deserializer provider to use to read inner objects.</param> public ODataActionPayloadDeserializer(IODataDeserializerProvider deserializerProvider) : base(ODataPayloadKind.Parameter) { if (deserializerProvider == null) { throw Error.ArgumentNull("deserializerProvider"); } DeserializerProvider = deserializerProvider; }
private static object ConvertCollection(ODataCollectionValue collectionValue, IEdmTypeReference edmTypeReference, Type clrType, string parameterName, ODataDeserializerContext readContext, IServiceProvider requestContainer) { Contract.Assert(collectionValue != null); IEdmCollectionTypeReference collectionType = edmTypeReference as IEdmCollectionTypeReference; Contract.Assert(collectionType != null); IODataDeserializerProvider deserializerProvider = requestContainer.GetRequiredService <IODataDeserializerProvider>(); ODataCollectionDeserializer deserializer = (ODataCollectionDeserializer)deserializerProvider.GetEdmTypeDeserializer(collectionType); object value = deserializer.ReadInline(collectionValue, collectionType, readContext); if (value == null) { return(null); } IEnumerable collection = value as IEnumerable; Contract.Assert(collection != null); Type elementType; if (!TypeHelper.IsCollection(clrType, out elementType)) { // EdmEntityCollectionObject and EdmComplexCollectionObject are collection types. throw new ODataException(String.Format(CultureInfo.InvariantCulture, SRResources.ParameterTypeIsNotCollection, parameterName, clrType)); } IEnumerable newCollection; if (CollectionDeserializationHelpers.TryCreateInstance(clrType, collectionType, elementType, out newCollection)) { collection.AddToCollection(newCollection, elementType, parameterName, clrType); if (clrType.IsArray) { newCollection = CollectionDeserializationHelpers.ToArray(newCollection, elementType); } return(newCollection); } return(null); }
private static object ConvertResourceSet(ODataMessageReader oDataMessageReader, IEdmTypeReference edmTypeReference, ODataDeserializerContext readContext) { IEdmCollectionTypeReference collectionType = edmTypeReference.AsCollection(); EdmEntitySet tempEntitySet = null; if (collectionType.ElementType().IsEntity()) { tempEntitySet = new EdmEntitySet(readContext.Model.EntityContainer, "temp", collectionType.ElementType().AsEntity().EntityDefinition()); } // TODO: Sam xu, can we use the parameter-less overload ODataReader odataReader = oDataMessageReader.CreateODataUriParameterResourceSetReader(tempEntitySet, collectionType.ElementType().AsStructured().StructuredDefinition()); ODataResourceSetWrapper resourceSet = odataReader.ReadResourceOrResourceSet() as ODataResourceSetWrapper; IODataDeserializerProvider deserializerProvider = readContext.Request.GetDeserializerProvider(); ODataResourceSetDeserializer resourceSetDeserializer = (ODataResourceSetDeserializer)deserializerProvider.GetEdmTypeDeserializer(collectionType); object result = resourceSetDeserializer.ReadInline(resourceSet, collectionType, readContext); IEnumerable enumerable = result as IEnumerable; if (enumerable != null) { if (readContext.IsNoClrType) { return(enumerable.ConvertToEdmObject(collectionType)); } else { IEdmTypeReference elementTypeReference = collectionType.ElementType(); Type elementClrType = readContext.Model.GetClrType(elementTypeReference); IEnumerable castedResult = CastMethodInfo.MakeGenericMethod(elementClrType) .Invoke(null, new object[] { enumerable }) as IEnumerable; return(castedResult); } } return(null); }
/// <summary> /// Convert an OData value into a CLR object. /// </summary> /// <param name="graph">The given object.</param> /// <param name="edmTypeReference">The EDM type of the given object.</param> /// <param name="clrType">The CLR type of the given object.</param> /// <param name="parameterName">The parameter name of the given object.</param> /// <param name="readContext">The <see cref="ODataDeserializerContext"/> use to convert.</param> /// <param name="requestContainer">The dependency injection container for the request.</param> /// <returns>The converted object.</returns> public static object Convert(object graph, IEdmTypeReference edmTypeReference, Type clrType, string parameterName, ODataDeserializerContext readContext, IServiceProvider requestContainer) { if (graph == null || graph is ODataNullValue) { return(null); } // collection of primitive, enum ODataCollectionValue collectionValue = graph as ODataCollectionValue; if (collectionValue != null) { return(ConvertCollection(collectionValue, edmTypeReference, clrType, parameterName, readContext, requestContainer)); } // enum value ODataEnumValue enumValue = graph as ODataEnumValue; if (enumValue != null) { IEdmEnumTypeReference edmEnumType = edmTypeReference.AsEnum(); Contract.Assert(edmEnumType != null); IODataDeserializerProvider deserializerProvider = requestContainer.GetRequiredService <IODataDeserializerProvider>(); ODataEnumDeserializer deserializer = (ODataEnumDeserializer)deserializerProvider.GetEdmTypeDeserializer(edmEnumType); return(deserializer.ReadInline(enumValue, edmEnumType, readContext)); } // primitive value if (edmTypeReference.IsPrimitive() || edmTypeReference.IsTypeDefinition()) { ConstantNode node = graph as ConstantNode; return(EdmPrimitiveHelper.ConvertPrimitiveValue(node != null ? node.Value : graph, clrType, readContext?.TimeZone)); } // Resource, ResourceSet, Entity Reference or collection of entity reference return(ConvertResourceOrResourceSet(graph, edmTypeReference, readContext)); }
public void ReadInline_Calls_ReadResourceSet() { // Arrange IODataDeserializerProvider deserializerProvider = _deserializerProvider; Mock <ODataResourceSetDeserializer> deserializer = new Mock <ODataResourceSetDeserializer>(deserializerProvider); ODataResourceSetWrapper feedWrapper = new ODataResourceSetWrapper(new ODataResourceSet()); ODataDeserializerContext readContext = new ODataDeserializerContext(); IEnumerable expectedResult = new object[0]; deserializer.CallBase = true; deserializer.Setup(f => f.ReadResourceSet(feedWrapper, _customerType, readContext)).Returns(expectedResult).Verifiable(); // Act var result = deserializer.Object.ReadInline(feedWrapper, _customersType, readContext); // Assert deserializer.Verify(); Assert.Same(expectedResult, result); }
internal static void ApplyProperty(ODataProperty property, IEdmStructuredTypeReference resourceType, object resource, IODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext) { IEdmStructuredType structuredType = resourceType.StructuredDefinition(); IEdmProperty edmProperty = structuredType == null ? null : structuredType.ResolveProperty(property.Name); bool isDynamicProperty = false; string propertyName = property.Name; if (edmProperty != null) { propertyName = readContext.Model.GetClrPropertyName(edmProperty); } else { isDynamicProperty = structuredType != null && structuredType.IsOpen; } if (!isDynamicProperty && edmProperty == null) { throw new ODataException( Error.Format(SRResources.CannotDeserializeUnknownProperty, property.Name, resourceType.Definition)); } // dynamic properties have null values IEdmTypeReference propertyType = edmProperty != null ? edmProperty.Type : null; EdmTypeKind propertyKind; object value = ConvertValue(property.Value, ref propertyType, deserializerProvider, readContext, out propertyKind); if (isDynamicProperty) { SetDynamicProperty(resource, resourceType, propertyKind, propertyName, value, propertyType, readContext.Model); } else { SetDeclaredProperty(resource, propertyKind, propertyName, value, edmProperty, readContext); } }
internal static void ApplyProperty(ODataProperty property, IEdmStructuredTypeReference resourceType, object resource, IODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext) { IEdmProperty edmProperty = resourceType.FindProperty(property.Name); bool isDynamicProperty = false; string propertyName = property.Name; if (edmProperty != null) { propertyName = EdmLibHelpers.GetClrPropertyName(edmProperty, readContext.Model); } else { IEdmStructuredType structuredType = resourceType.StructuredDefinition(); isDynamicProperty = structuredType != null && structuredType.IsOpen; } if (!isDynamicProperty && edmProperty == null) { throw new ODataException("Does not support untyped value in non-open type."); } // dynamic properties have null values IEdmTypeReference propertyType = edmProperty != null ? edmProperty.Type : null; EdmTypeKind propertyKind; object value = ConvertValue(property.Value, ref propertyType, deserializerProvider, readContext, out propertyKind); if (isDynamicProperty) { SetDynamicProperty(resource, resourceType, propertyKind, propertyName, value, propertyType, readContext.Model); } else { SetDeclaredProperty(resource, propertyKind, propertyName, value, edmProperty, readContext); } }
internal static object ConvertValue(object oDataValue, ref IEdmTypeReference propertyType, IODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext, out EdmTypeKind typeKind) { if (oDataValue == null) { typeKind = EdmTypeKind.None; return(null); } ODataEnumValue enumValue = oDataValue as ODataEnumValue; if (enumValue != null) { typeKind = EdmTypeKind.Enum; return(ConvertEnumValue(enumValue, ref propertyType, deserializerProvider, readContext)); } ODataCollectionValue collection = oDataValue as ODataCollectionValue; if (collection != null) { typeKind = EdmTypeKind.Collection; return(ConvertCollectionValue(collection, ref propertyType, deserializerProvider, readContext)); } ODataUntypedValue untypedValue = oDataValue as ODataUntypedValue; if (untypedValue != null) { Contract.Assert(!String.IsNullOrEmpty(untypedValue.RawValue)); if (untypedValue.RawValue.StartsWith("[", StringComparison.Ordinal) || untypedValue.RawValue.StartsWith("{", StringComparison.Ordinal)) { throw new ODataException(Error.Format(SRResources.InvalidODataUntypedValue, untypedValue.RawValue)); } oDataValue = ConvertPrimitiveValue(untypedValue.RawValue); } typeKind = EdmTypeKind.Primitive; return(oDataValue); }
public void ReadInline_Calls_ReadInlineForEachDeltaItem() { IODataDeserializerProvider provider = ODataFormatterHelpers.GetDeserializerProvider(); }
/// <summary> /// Initializes a new instance of the <see cref="ODataCollectionDeserializer"/> class. /// </summary> /// <param name="deserializerProvider">The deserializer provider to use to read inner objects.</param> public ODataCollectionDeserializer(IODataDeserializerProvider deserializerProvider) : base(ODataPayloadKind.Collection, deserializerProvider) { }
/// <summary> /// Initializes a new instance of the <see cref="ODataDeltaResourceSetDeserializer"/> class. /// </summary> /// <param name="deserializerProvider">The deserializer provider to use to read inner objects.</param> public ODataDeltaResourceSetDeserializer(IODataDeserializerProvider deserializerProvider) : base(ODataPayloadKind.Delta, deserializerProvider) { }
public ETagResourceDeserializer(IODataDeserializerProvider serializerProvider) : base(serializerProvider) { }
/// <summary> /// Initializes a new instance of the <see cref="ODataResourceDeserializer"/> class. /// </summary> /// <param name="deserializerProvider">The deserializer provider to use to read inner objects.</param> public ODataResourceDeserializer(IODataDeserializerProvider deserializerProvider) : base(ODataPayloadKind.Resource, deserializerProvider) { }
private static object ConvertCollectionValue(ODataCollectionValue collection, ref IEdmTypeReference propertyType, IODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext) { IEdmCollectionTypeReference collectionType; if (propertyType == null) { // dynamic collection property Contract.Assert(!String.IsNullOrEmpty(collection.TypeName), "ODataLib should have verified that dynamic collection value has a type name " + "since we provided metadata."); string elementTypeName = GetCollectionElementTypeName(collection.TypeName, isNested: false); IEdmModel model = readContext.Model; IEdmSchemaType elementType = model.FindType(elementTypeName); Contract.Assert(elementType != null); collectionType = new EdmCollectionTypeReference( new EdmCollectionType(elementType.ToEdmTypeReference(isNullable: false))); propertyType = collectionType; } else { collectionType = propertyType as IEdmCollectionTypeReference; Contract.Assert(collectionType != null, "The type for collection must be a IEdmCollectionType."); } IODataEdmTypeDeserializer deserializer = deserializerProvider.GetEdmTypeDeserializer(collectionType); return(deserializer.ReadInline(collection, collectionType, readContext)); }
/// <summary> /// Initializes a new instance of the <see cref="ODataActionPayloadDeserializer"/> class. /// </summary> /// <param name="deserializerProvider">The deserializer provider to use to read inner objects.</param> public ODataActionPayloadDeserializer(IODataDeserializerProvider deserializerProvider) : base(ODataPayloadKind.Parameter) { DeserializerProvider = deserializerProvider ?? throw new ArgumentNullException(nameof(deserializerProvider)); }
internal static object ConvertValue(object oDataValue, ref IEdmTypeReference propertyType, IODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext, out EdmTypeKind typeKind) { if (oDataValue == null) { typeKind = EdmTypeKind.None; return(null); } ODataEnumValue enumValue = oDataValue as ODataEnumValue; if (enumValue != null) { typeKind = EdmTypeKind.Enum; return(ConvertEnumValue(enumValue, ref propertyType, deserializerProvider, readContext)); } ODataCollectionValue collection = oDataValue as ODataCollectionValue; if (collection != null) { typeKind = EdmTypeKind.Collection; return(ConvertCollectionValue(collection, ref propertyType, deserializerProvider, readContext)); } ODataUntypedValue untypedValue = oDataValue as ODataUntypedValue; if (untypedValue != null) { Contract.Assert(!String.IsNullOrEmpty(untypedValue.RawValue)); if (untypedValue.RawValue.StartsWith("[", StringComparison.Ordinal) || untypedValue.RawValue.StartsWith("{", StringComparison.Ordinal)) { throw new ODataException("TODO: " /*Error.Format(SRResources.InvalidODataUntypedValue, untypedValue.RawValue)*/); } ODataCollectionValue collectionValue = (ODataCollectionValue)ODataUriUtils.ConvertFromUriLiteral( String.Format(CultureInfo.InvariantCulture, "[{0}]", untypedValue.RawValue), ODataVersion.V4); foreach (object item in collectionValue.Items) { oDataValue = item; break; } } typeKind = EdmTypeKind.Primitive; return(oDataValue); }