Beispiel #1
0
        public async Task ReadFromStreamAsync_ModelAlias()
        {
            // Arrange
            string content = "{\"@odata.type\":\"#NS.level\",\"value\":\"veryhigh\"}";

            var builder = new ODataConventionModelBuilder();

            builder.EnumType <Level>().Namespace = "NS";
            IEdmModel model = builder.GetEdmModel();

            ODataEnumDeserializer    deserializer = new ODataEnumDeserializer();
            ODataDeserializerContext readContext  = new ODataDeserializerContext
            {
                Model        = model,
                ResourceType = typeof(Level)
            };

            HttpRequest request = RequestFactory.Create("Post", "http://localhost/", _edmModel);

            // Act
            object value = await deserializer.ReadAsync(ODataTestUtil.GetODataMessageReader(request.GetODataMessage(content), model),
                                                        typeof(Level), readContext);

            // Assert
            Level level = Assert.IsType <Level>(value);

            Assert.Equal(Level.High, level);
        }
        public void ReadFromStreamAsync_ModelAlias()
        {
            // Arrange
            string content = "{\"@odata.type\":\"#NS.level\",\"value\":\"veryhigh\"}";

            var builder = ODataConventionModelBuilderFactory.Create();

            builder.EnumType <Level>().Namespace = "NS";
            IEdmModel model = builder.GetEdmModel();

            ODataEnumDeserializer    deserializer = new ODataEnumDeserializer();
            ODataDeserializerContext readContext  = new ODataDeserializerContext
            {
                Model        = model,
                ResourceType = typeof(Level)
            };

            // Act
            object value = deserializer.Read(ODataDeserializationTestsCommon.GetODataMessageReader(ODataDeserializationTestsCommon.GetODataMessage(content, new HttpRequestMessage(new HttpMethod("Post"), "http://localhost/OData/TestUri")), model),
                                             typeof(Level), readContext);

            // Assert
            Level level = Assert.IsType <Level>(value);

            Assert.Equal(Level.High, level);
        }
Beispiel #3
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));
            }
Beispiel #4
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);
            }

            // 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);

                ODataDeserializerProvider deserializerProvider =
                    requestContainer.GetRequiredService <ODataDeserializerProvider>();

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

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

            // primitive value
            if (edmTypeReference.IsPrimitive())
            {
                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 ReadFromStreamAsync_RawValue()
        {
            // Arrange
            string content = "{\"value\":\"Blue\"}";

            ODataEnumDeserializer    deserializer = new ODataEnumDeserializer();
            ODataDeserializerContext readContext  = new ODataDeserializerContext
            {
                Model        = _edmModel,
                ResourceType = typeof(Color)
            };

            // Act
            object value = deserializer.Read(ODataDeserializationTestsCommon.GetODataMessageReader(ODataDeserializationTestsCommon.GetODataMessage(content, new HttpRequestMessage(new HttpMethod("Post"), "http://localhost/OData/TestUri")), _edmModel),
                                             typeof(Color), readContext);

            // Assert
            Color color = Assert.IsType <Color>(value);

            Assert.Equal(Color.Blue, color);
        }
Beispiel #6
0
        public void ReadFromStreamAsync()
        {
            // Arrange
            string content = "{\"@odata.type\":\"#NS.Color\",\"value\":\"Blue\"}";

            ODataEnumDeserializer    deserializer = new ODataEnumDeserializer();
            ODataDeserializerContext readContext  = new ODataDeserializerContext
            {
                Model        = _edmModel,
                ResourceType = typeof(Color)
            };

            // Act
            object value = deserializer.Read(GetODataMessageReader(GetODataMessage(content), _edmModel),
                                             typeof(Color), readContext);

            // Assert
            Color color = Assert.IsType <Color>(value);

            Assert.Equal(Color.Blue, color);
        }
Beispiel #7
0
        public async Task ReadFromStreamAsync_RawValue()
        {
            // Arrange
            string content = "{\"value\":\"Blue\"}";

            ODataEnumDeserializer    deserializer = new ODataEnumDeserializer();
            ODataDeserializerContext readContext  = new ODataDeserializerContext
            {
                Model        = _edmModel,
                ResourceType = typeof(Color)
            };
            HttpRequest request = RequestFactory.Create("Post", "http://localhost/", _edmModel);

            // Act
            object value = await deserializer.ReadAsync(ODataTestUtil.GetODataMessageReader(request.GetODataMessage(content), _edmModel),
                                                        typeof(Color), readContext);

            // Assert
            Color color = Assert.IsType <Color>(value);

            Assert.Equal(Color.Blue, color);
        }
        public void ReadFromStreamAsync_ForUnType()
        {
            // Arrange
            string content = "{\"@odata.type\":\"#NS.Color\",\"value\":\"Blue\"}";

            ODataEnumDeserializer    deserializer = new ODataEnumDeserializer();
            ODataDeserializerContext readContext  = new ODataDeserializerContext
            {
                Model        = _edmModel,
                ResourceType = typeof(IEdmEnumObject)
            };

            // Act
            object value = deserializer.Read(ODataDeserializationTestsCommon.GetODataMessageReader(ODataDeserializationTestsCommon.GetODataMessage(content, new HttpRequestMessage(new HttpMethod("Post"), "http://localhost/OData/TestUri")), _edmModel),
                                             typeof(Color), readContext);

            // Assert
            EdmEnumObject color = Assert.IsType <EdmEnumObject>(value);

            Assert.NotNull(color);

            Assert.Equal("Blue", color.Value);
        }
        public void ReadFromStreamAsync()
        {
            // Arrange
            string content = "{\"@odata.type\":\"#NS.Color\",\"value\":\"Blue\"}";

            ODataEnumDeserializer    deserializer = new ODataEnumDeserializer();
            ODataDeserializerContext readContext  = new ODataDeserializerContext
            {
                Model        = _edmModel,
                ResourceType = typeof(Color)
            };

            HttpRequest request = RequestFactory.Create("Post", "http://localhost/TestUri", opt => opt.AddModel("odata", _edmModel));

            // Act
            object value = deserializer.Read(ODataTestUtil.GetODataMessageReader(request.GetODataMessage(content), _edmModel),
                                             typeof(Color), readContext);

            // Assert
            Color color = Assert.IsType <Color>(value);

            Assert.Equal(Color.Blue, color);
        }
Beispiel #10
0
        public async Task ReadFromStreamAsync_ForUnType()
        {
            // Arrange
            string content = "{\"@odata.type\":\"#NS.Color\",\"value\":\"Blue\"}";

            ODataEnumDeserializer    deserializer = new ODataEnumDeserializer();
            ODataDeserializerContext readContext  = new ODataDeserializerContext
            {
                Model        = _edmModel,
                ResourceType = typeof(IEdmEnumObject)
            };
            HttpRequest request = RequestFactory.Create("Post", "http://localhost/", _edmModel);

            // Act
            object value = await deserializer.ReadAsync(ODataTestUtil.GetODataMessageReader(request.GetODataMessage(content), _edmModel),
                                                        typeof(Color), readContext);

            // Assert
            EdmEnumObject color = Assert.IsType <EdmEnumObject>(value);

            Assert.NotNull(color);

            Assert.Equal("Blue", color.Value);
        }