Ejemplo n.º 1
0
            internal static object ConvertTo(ODataParameterValue parameterValue, HttpActionContext actionContext, ModelBindingContext bindingContext)
            {
                Contract.Assert(parameterValue != null && parameterValue.EdmType != null);

                object oDataValue = parameterValue.Value;

                if (oDataValue == null || oDataValue is ODataNullValue)
                {
                    return(null);
                }

                IEdmTypeReference        edmTypeReference = parameterValue.EdmType;
                ODataDeserializerContext readContext      = BuildDeserializerContext(actionContext, bindingContext, edmTypeReference);

                // complex value
                ODataComplexValue complexValue = oDataValue as ODataComplexValue;

                if (complexValue != null)
                {
                    IEdmComplexTypeReference edmComplexType = edmTypeReference.AsComplex();
                    Contract.Assert(edmComplexType != null);

                    ODataComplexTypeDeserializer deserializer =
                        (ODataComplexTypeDeserializer)DeserializerProvider.GetEdmTypeDeserializer(edmComplexType);

                    return(deserializer.ReadInline(complexValue, edmComplexType, readContext));
                }

                // collection of primitive, enum, complex
                ODataCollectionValue collectionValue = oDataValue as ODataCollectionValue;

                if (collectionValue != null)
                {
                    return(ConvertCollection(collectionValue, edmTypeReference, bindingContext, readContext));
                }

                // enum value
                ODataEnumValue enumValue = oDataValue as ODataEnumValue;

                if (enumValue != null)
                {
                    IEdmEnumTypeReference edmEnumType = edmTypeReference.AsEnum();
                    Contract.Assert(edmEnumType != null);

                    ODataEnumDeserializer deserializer =
                        (ODataEnumDeserializer)DeserializerProvider.GetEdmTypeDeserializer(edmEnumType);

                    return(deserializer.ReadInline(enumValue, edmEnumType, readContext));
                }

                // primitive value
                if (edmTypeReference.IsPrimitive())
                {
                    return(EdmPrimitiveHelpers.ConvertPrimitiveValue(oDataValue, bindingContext.ModelType));
                }

                // Entity, Feed, Entity Reference or collection of entity reference
                return(ConvertFeedOrEntry(oDataValue, edmTypeReference, readContext));
            }
Ejemplo n.º 2
0
        public void ConvertDateTimeValue_NonStandardPrimitives_DefaultTimeZoneInfo(DateTimeOffset valueToConvert)
        {
            // Arrange & Act
            object actual = EdmPrimitiveHelpers.ConvertPrimitiveValue(valueToConvert, typeof(DateTime), timeZoneInfo: null);

            // Assert
            DateTime dt = Assert.IsType <DateTime>(actual);

            Assert.Equal(valueToConvert.ToUniversalTime().ToOffset(TimeZoneInfo.Local.BaseUtcOffset).DateTime, dt);
        }
Ejemplo n.º 3
0
        public void ConvertDateTimeValue_NonStandardPrimitives_CustomTimeZoneInfo(DateTimeOffset valueToConvert)
        {
            // Arrange & Act
            TimeZoneInfo tzi    = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
            object       actual = EdmPrimitiveHelpers.ConvertPrimitiveValue(valueToConvert, typeof(DateTime), tzi);

            // Assert
            DateTime dt = Assert.IsType <DateTime>(actual);

            Assert.Equal(valueToConvert.ToUniversalTime().ToOffset(new TimeSpan(-8, 0, 0)).DateTime, dt);
        }
Ejemplo n.º 4
0
        public void ConvertDateTimeValue_NonStandardPrimitives_DefaultTimeZoneInfo(DateTimeOffset valueToConvert)
        {
            // Arrange & Act
            TimeZoneInfoHelper.TimeZone = null;
            object actual = EdmPrimitiveHelpers.ConvertPrimitiveValue(valueToConvert, typeof(DateTime));

            // Assert
            DateTime dt = Assert.IsType <DateTime>(actual);

            Assert.Equal(valueToConvert.LocalDateTime, dt);
        }
Ejemplo n.º 5
0
            internal static object ConvertTo(string valueString, Type type)
            {
                if (valueString == null)
                {
                    return(null);
                }

                // TODO 1668: ODL beta1's ODataUriUtils.ConvertFromUriLiteral does not support converting uri literal
                // to ODataEnumValue, but beta1's ODataUriUtils.ConvertToUriLiteral supports converting ODataEnumValue
                // to uri literal.
                if (TypeHelper.IsEnum(type))
                {
                    string[] values = valueString.Split(new[] { '\'' }, StringSplitOptions.None);
                    if (values.Length == 3 && String.IsNullOrEmpty(values[2]))
                    {
                        // Remove the type name if the enum value is a fully qualified literal.
                        valueString = values[1];
                    }

                    if (type.IsNullable() && String.Equals(valueString, "null", StringComparison.Ordinal))
                    {
                        return(null);
                    }

                    Type     enumType     = TypeHelper.GetUnderlyingTypeOrSelf(type);
                    object[] parameters   = new[] { valueString, Enum.ToObject(enumType, 0) };
                    bool     isSuccessful = (bool)enumTryParseMethod.MakeGenericMethod(enumType).Invoke(null, parameters);

                    if (!isSuccessful)
                    {
                        throw Error.InvalidOperation(SRResources.ModelBinderUtil_ValueCannotBeEnum, valueString, type.Name);
                    }

                    return(parameters[1]);
                }

                object value = ODataUriUtils.ConvertFromUriLiteral(valueString, ODataVersion.V4);

                bool isNonStandardEdmPrimitive;

                EdmLibHelpers.IsNonstandardEdmPrimitive(type, out isNonStandardEdmPrimitive);

                if (isNonStandardEdmPrimitive)
                {
                    return(EdmPrimitiveHelpers.ConvertPrimitiveValue(value, type));
                }
                else
                {
                    type = Nullable.GetUnderlyingType(type) ?? type;
                    return(Convert.ChangeType(value, type, CultureInfo.InvariantCulture));
                }
            }
Ejemplo n.º 6
0
        public void ConvertDateTimeValue_NonStandardPrimitives_CustomTimeZoneInfo(DateTimeOffset valueToConvert)
        {
            // Arrange & Act
            var timeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");

            TimeZoneInfoHelper.TimeZone = timeZone;
            object actual = EdmPrimitiveHelpers.ConvertPrimitiveValue(valueToConvert, typeof(DateTime));

            // Assert
            DateTime dt = Assert.IsType <DateTime>(actual);

            Assert.Equal(TimeZoneInfo.ConvertTime(valueToConvert, timeZone).DateTime, dt);
        }
Ejemplo n.º 7
0
        /// <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);
            }

            ODataDeserializerProvider deserializerProvider =
                requestContainer.GetRequiredService <ODataDeserializerProvider>();
            // 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);

                ODataEnumDeserializer deserializer =
                    (ODataEnumDeserializer)deserializerProvider.GetEdmTypeDeserializer(edmEnumType);

                return(deserializer.ReadInline(enumValue, edmEnumType, readContext));
            }

            // primitive value
            if (edmTypeReference.IsPrimitive())
            {
                return(EdmPrimitiveHelpers.ConvertPrimitiveValue(graph, clrType));
            }

            // Resource, ResourceSet, Entity Reference or collection of entity reference
            return(ConvertResourceOrResourceSet(graph, edmTypeReference, readContext));
        }
Ejemplo n.º 8
0
        internal static object ConvertTo(string valueString, Type type)
        {
            if (valueString == null)
            {
                return(null);
            }

            if (type.IsNullable() && String.Equals(valueString, "null", StringComparison.Ordinal))
            {
                return(null);
            }

            // TODO 1668: ODL beta1's ODataUriUtils.ConvertFromUriLiteral does not support converting uri literal
            // to ODataEnumValue, but beta1's ODataUriUtils.ConvertToUriLiteral supports converting ODataEnumValue
            // to uri literal.
            if (TypeHelper.IsEnum(type))
            {
                string[] values = valueString.Split(new[] { '\'' }, StringSplitOptions.None);
                if (values.Length == 3 && String.IsNullOrEmpty(values[2]))
                {
                    // Remove the type name if the enum value is a fully qualified literal.
                    valueString = values[1];
                }

                Type     enumType     = TypeHelper.GetUnderlyingTypeOrSelf(type);
                object[] parameters   = new[] { valueString, Enum.ToObject(enumType, 0) };
                bool     isSuccessful = (bool)EnumTryParseMethod.MakeGenericMethod(enumType).Invoke(null, parameters);

                if (!isSuccessful)
                {
                    throw Error.InvalidOperation(SRResources.ModelBinderUtil_ValueCannotBeEnum, valueString, type.Name);
                }

                return(parameters[1]);
            }

            // The logic of "public static object ConvertFromUriLiteral(string value, ODataVersion version);" treats
            // the date value string (for example: 2015-01-02) as DateTimeOffset literal, and return a DateTimeOffset
            // object. However, the logic of
            // "object ConvertFromUriLiteral(string value, ODataVersion version, IEdmModel model, IEdmTypeReference typeReference);"
            // can return the correct Date object.
            if (type == typeof(Date) || type == typeof(Date?))
            {
                EdmCoreModel model = EdmCoreModel.Instance;
                IEdmPrimitiveTypeReference dateTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(type);
                return(ODataUriUtils.ConvertFromUriLiteral(valueString, ODataVersion.V4, model, dateTypeReference));
            }

            object value;

            try
            {
                value = ODataUriUtils.ConvertFromUriLiteral(valueString, ODataVersion.V4);
            }
            catch
            {
                if (type == typeof(string))
                {
                    return(valueString);
                }

                throw;
            }

            bool isNonStandardEdmPrimitive;

            EdmLibHelpers.IsNonstandardEdmPrimitive(type, out isNonStandardEdmPrimitive);

            if (isNonStandardEdmPrimitive)
            {
                return(EdmPrimitiveHelpers.ConvertPrimitiveValue(value, type));
            }
            else
            {
                type = Nullable.GetUnderlyingType(type) ?? type;
                return(System.Convert.ChangeType(value, type, CultureInfo.InvariantCulture));
            }
        }
Ejemplo n.º 9
0
 public void ConvertPrimitiveValueToNullableChar_Throws()
 {
     Assert.Throws <ValidationException>(
         () => EdmPrimitiveHelpers.ConvertPrimitiveValue("123", typeof(char?), timeZoneInfo: null),
         "The value must be a string with a maximum length of 1.");
 }
Ejemplo n.º 10
0
 public void ConvertPrimitiveValueToChar_Throws(string input)
 {
     Assert.Throws <ValidationException>(
         () => EdmPrimitiveHelpers.ConvertPrimitiveValue(input, typeof(char), timeZoneInfo: null),
         "The value must be a string with a length of 1.");
 }
Ejemplo n.º 11
0
 public void ConvertPrimitiveValue_NonStandardPrimitives(object valueToConvert, object result, Type conversionType)
 {
     Assert.Equal(result.GetType(), EdmPrimitiveHelpers.ConvertPrimitiveValue(valueToConvert, conversionType, timeZoneInfo: null).GetType());
     Assert.Equal(result.ToString(), EdmPrimitiveHelpers.ConvertPrimitiveValue(valueToConvert, conversionType, timeZoneInfo: null).ToString());
 }
Ejemplo n.º 12
0
 public void ConvertPrimitiveValueToXElement_Throws_IfInputIsNotString()
 {
     Assert.Throws <ValidationException>(
         () => EdmPrimitiveHelpers.ConvertPrimitiveValue(123, typeof(XElement), timeZoneInfo: null),
         "The value must be a string.");
 }