public void TestGetPayloadTypeName()
        {
            Dictionary <object[], string> payloadTypeToNames = new Dictionary <object[], string>();

            payloadTypeToNames.Add(new object[] { null }, null);
            payloadTypeToNames.Add(new object[] { true }, OData.Metadata.EdmConstants.EdmBooleanTypeName);
            payloadTypeToNames.Add(new object[] { "myPayloadItem" }, OData.Metadata.EdmConstants.EdmStringTypeName);
            payloadTypeToNames.Add(new object[] { Int32.MinValue }, OData.Metadata.EdmConstants.EdmInt32TypeName);
            payloadTypeToNames.Add(new object[] { Double.Epsilon }, OData.Metadata.EdmConstants.EdmDoubleTypeName);

            ODataCollectionValue collectionValue = new ODataCollectionValue();

            payloadTypeToNames.Add(new object[] { collectionValue }, EdmLibraryExtensions.GetCollectionTypeFullName(collectionValue.TypeName));

            ODataResource resourceValue = new ODataResource();

            payloadTypeToNames.Add(new object[] { resourceValue }, resourceValue.TypeName);

            foreach (KeyValuePair <object[], string> pair in payloadTypeToNames)
            {
                object result = typeReaderUtils.GetMethod("GetPayloadTypeName", bindingFlags)
                                .Invoke(null, /*static method*/
                                        pair.Key);
                result.Should().Be(pair.Value);
            }
        }
示例#2
0
        /// <summary>
        /// Gets the type name based on the given odata value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="model">The model used to handle unsigned int conversions.</param>
        /// <returns>The type name for the context URI.</returns>
        private static string GetTypeNameForValue(ODataValue value, IEdmModel model)
        {
            if (value == null)
            {
                return(null);
            }

            // special identifier for null values.
            if (value.IsNullValue)
            {
                return(ODataConstants.ContextUriFragmentNull);
            }

            if (value.TypeAnnotation != null && !string.IsNullOrEmpty(value.TypeAnnotation.TypeName))
            {
                return(value.TypeAnnotation.TypeName);
            }

            var collectionValue = value as ODataCollectionValue;

            if (collectionValue != null)
            {
                return(EdmLibraryExtensions.GetCollectionTypeFullName(collectionValue.TypeName));
            }

            var enumValue = value as ODataEnumValue;

            if (enumValue != null)
            {
                return(enumValue.TypeName);
            }

            var untypedValue = value as ODataUntypedValue;

            if (untypedValue != null)
            {
                return(ODataConstants.ContextUriFragmentUntyped);
            }

            ODataPrimitiveValue primitive = value as ODataPrimitiveValue;

            if (primitive == null)
            {
                Debug.Assert(value is ODataStreamReferenceValue, "value is ODataStreamReferenceValue");
                throw new ODataException(Strings.ODataContextUriBuilder_StreamValueMustBePropertiesOfODataResource);
            }

            // Try convert to underlying type if the primitive value is unsigned int.
            IEdmTypeDefinitionReference typeDefinitionReference = model.ResolveUIntTypeDefinition(primitive.Value);

            if (typeDefinitionReference != null)
            {
                return(typeDefinitionReference.FullName());
            }

            IEdmPrimitiveTypeReference primitiveValueTypeReference = EdmLibraryExtensions.GetPrimitiveTypeReference(primitive.Value.GetType());

            return(primitiveValueTypeReference == null ? null : primitiveValueTypeReference.FullName());
        }
示例#3
0
        /// <summary>
        /// Gets the type name based on the given odata value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The type name for the context URI.</returns>
        private static string GetTypeNameForValue(ODataValue value)
        {
            if (value == null)
            {
                return(null);
            }

            // special identifier for null values.
            if (value.IsNullValue)
            {
                return(ODataConstants.ContextUriFragmentNull);
            }

            var typeAnnotation = value.GetAnnotation <SerializationTypeNameAnnotation>();

            if (typeAnnotation != null && !string.IsNullOrEmpty(typeAnnotation.TypeName))
            {
                return(typeAnnotation.TypeName);
            }

            var complexValue = value as ODataComplexValue;

            if (complexValue != null)
            {
                return(complexValue.TypeName);
            }

            var collectionValue = value as ODataCollectionValue;

            if (collectionValue != null)
            {
                return(EdmLibraryExtensions.GetCollectionTypeFullName(collectionValue.TypeName));
            }

            var enumValue = value as ODataEnumValue;

            if (enumValue != null)
            {
                return(enumValue.TypeName);
            }

            ODataPrimitiveValue primitive = value as ODataPrimitiveValue;

            if (primitive == null)
            {
                Debug.Assert(value is ODataStreamReferenceValue, "value is ODataStreamReferenceValue");
                throw new ODataException(Strings.ODataContextUriBuilder_StreamValueMustBePropertiesOfODataEntry);
            }

            return(EdmLibraryExtensions.GetPrimitiveTypeReference(primitive.Value.GetType()).ODataFullName());
        }
        /// <summary>
        /// Gets the payload type name for an OData OM instance for JsonLight.
        /// </summary>
        /// <param name="payloadItem">The payload item to get the type name for.</param>
        /// <returns>The type name as read from the payload item (or constructed for primitive items).</returns>
        internal static string GetPayloadTypeName(object payloadItem)
        {
            if (payloadItem == null)
            {
                return(null);
            }

            TypeCode typeCode = ODataPlatformHelper.GetTypeCode(payloadItem.GetType());

            switch (typeCode)
            {
            // In JSON only boolean, String, Int32 and Double are recognized as primitive types
            // (without additional type conversion). So only check for those; if not one of these primitive
            // types it must be a complex, entity or collection value.
            case TypeCode.Boolean: return(Metadata.EdmConstants.EdmBooleanTypeName);

            case TypeCode.String: return(Metadata.EdmConstants.EdmStringTypeName);

            case TypeCode.Int32: return(Metadata.EdmConstants.EdmInt32TypeName);

            case TypeCode.Double: return(Metadata.EdmConstants.EdmDoubleTypeName);

            default:
                Debug.Assert(typeCode == TypeCode.Object, "If not one of the primitive types above, it must be an object in JSON.");
                break;
            }

            ODataComplexValue complexValue = payloadItem as ODataComplexValue;

            if (complexValue != null)
            {
                return(complexValue.TypeName);
            }

            ODataCollectionValue collectionValue = payloadItem as ODataCollectionValue;

            if (collectionValue != null)
            {
                return(EdmLibraryExtensions.GetCollectionTypeFullName(collectionValue.TypeName));
            }

            ODataEntry entry = payloadItem as ODataEntry;

            if (entry != null)
            {
                return(entry.TypeName);
            }

            throw new ODataException(ODataErrorStrings.General_InternalError(InternalErrorCodes.ODataJsonLightReader_ReadEntryStart));
        }
示例#5
0
        /// <summary>
        /// Gets the type name from the given <paramref name="value"/>.
        /// </summary>
        /// <param name="value">The value to get the type name from. This can be an ODataPrimitiveValue, an ODataCollectionValue or a Clr primitive object.</param>
        /// <returns>The type name for the given <paramref name="value"/>.</returns>
        protected static string GetTypeNameFromValue(object value)
        {
            Debug.Assert(value != null, "value != null");

            ODataPrimitiveValue primitiveValue = value as ODataPrimitiveValue;

            if (primitiveValue != null)
            {
                // primitiveValueTypeReference == null means: the EDM type of the primitive value cannot be determined.
                // This could possibly be due to value being an unsigned int.
                // In this case, simply return null because:
                //   - If the property is regular property, the type is not needed since service model knows its exact type.
                //   - If the property is dynamic property, ODL does not support dynamic property containing unsigned int value
                //     since we don't know its underlying type as well as how to serialize it.
                IEdmPrimitiveTypeReference primitiveValueTypeReference = EdmLibraryExtensions.GetPrimitiveTypeReference(primitiveValue.Value.GetType());
                return(primitiveValueTypeReference == null ? null : primitiveValueTypeReference.FullName());
            }

            ODataEnumValue enumValue = value as ODataEnumValue;

            if (enumValue != null)
            {
                return(enumValue.TypeName);
            }

            ODataResourceValue resourceValue = value as ODataResourceValue;

            if (resourceValue != null)
            {
                return(resourceValue.TypeName);
            }

            ODataCollectionValue collectionValue = value as ODataCollectionValue;

            if (collectionValue != null)
            {
                return(EdmLibraryExtensions.GetCollectionTypeFullName(collectionValue.TypeName));
            }

            IEdmPrimitiveTypeReference primitiveTypeReference = EdmLibraryExtensions.GetPrimitiveTypeReference(value.GetType());

            if (primitiveTypeReference == null)
            {
                throw new ODataException(Strings.ValidationUtils_UnsupportedPrimitiveType(value.GetType().FullName));
            }

            return(primitiveTypeReference.FullName());
        }
示例#6
0
        /// <summary>
        /// Gets the payload type name for an OData OM instance for JsonLight.
        /// </summary>
        /// <param name="payloadItem">The payload item to get the type name for.</param>
        /// <returns>The type name as read from the payload item (or constructed for primitive items).</returns>
        internal static string GetPayloadTypeName(object payloadItem)
        {
            if (payloadItem == null)
            {
                return(null);
            }

            // In JSON only boolean, String, Int32 and Double are recognized as primitive types
            // (without additional type conversion). So only check for those; if not one of these primitive
            // types it must be a complex, entity or collection value.
            if (payloadItem is Boolean)
            {
                return(Metadata.EdmConstants.EdmBooleanTypeName);
            }

            if (payloadItem is String)
            {
                return(Metadata.EdmConstants.EdmStringTypeName);
            }

            if (payloadItem is Int32)
            {
                return(Metadata.EdmConstants.EdmInt32TypeName);
            }

            if (payloadItem is Double)
            {
                return(Metadata.EdmConstants.EdmDoubleTypeName);
            }

            ODataCollectionValue collectionValue = payloadItem as ODataCollectionValue;

            if (collectionValue != null)
            {
                return(EdmLibraryExtensions.GetCollectionTypeFullName(collectionValue.TypeName));
            }

            ODataResource resource = payloadItem as ODataResource;

            if (resource != null)
            {
                return(resource.TypeName);
            }

            throw new ODataException(ODataErrorStrings.General_InternalError(InternalErrorCodes.ODataJsonLightReader_ReadResourceStart));
        }
示例#7
0
        /// <summary>
        /// Gets the type name from the given <paramref name="value"/>.
        /// </summary>
        /// <param name="value">The value to get the type name from. This can be an ODataPrimitiveValue, an ODataComplexValue, an ODataCollectionValue or a Clr primitive object.</param>
        /// <returns>The type name for the given <paramref name="value"/>.</returns>
        protected static string GetTypeNameFromValue(object value)
        {
            Debug.Assert(value != null, "value != null");

            ODataPrimitiveValue primitiveValue = value as ODataPrimitiveValue;

            if (primitiveValue != null)
            {
                return(EdmLibraryExtensions.GetPrimitiveTypeReference(primitiveValue.Value.GetType()).ODataFullName());
            }

            ODataComplexValue complexValue = value as ODataComplexValue;

            if (complexValue != null)
            {
                return(complexValue.TypeName);
            }

            ODataEnumValue enumValue = value as ODataEnumValue;

            if (enumValue != null)
            {
                return(enumValue.TypeName);
            }

            ODataCollectionValue collectionValue = value as ODataCollectionValue;

            if (collectionValue != null)
            {
                return(EdmLibraryExtensions.GetCollectionTypeFullName(collectionValue.TypeName));
            }

            IEdmPrimitiveTypeReference primitiveTypeReference = EdmLibraryExtensions.GetPrimitiveTypeReference(value.GetType());

            if (primitiveTypeReference == null)
            {
                throw new ODataException(Strings.ValidationUtils_UnsupportedPrimitiveType(value.GetType().FullName));
            }

            return(primitiveTypeReference.ODataFullName());
        }