public void Read_Roundtrip_ComplexCollection()
        {
            Address[] addresses = new[]
            {
                new Address {
                    City = "Redmond", ZipCode = "1", Street = "A", State = "123"
                },
                new Address {
                    City = "Seattle", ZipCode = "2", Street = "S", State = "321"
                }
            };
            ODataCollectionSerializer   serializer   = new ODataCollectionSerializer(_addressCollectionType, new DefaultODataSerializerProvider(_model));
            ODataCollectionDeserializer deserializer = new ODataCollectionDeserializer(_addressCollectionType, _deserializerProvider);


            MemoryStream        stream  = new MemoryStream();
            ODataMessageWrapper message = new ODataMessageWrapper(stream);

            serializer.WriteObject(addresses, new ODataMessageWriter(message as IODataResponseMessage, new ODataMessageWriterSettings(), _model), new ODataSerializerContext {
                ServiceOperationName = "Property"
            });
            stream.Seek(0, SeekOrigin.Begin);
            IEnumerable readAddresses = deserializer.Read(new ODataMessageReader(message as IODataResponseMessage, new ODataMessageReaderSettings(), _model), new ODataDeserializerContext()) as IEnumerable;

            Assert.Equal(addresses, readAddresses.Cast <Address>(), new AddressComparer());
        }
        public void Read_Roundtrip_PrimitiveCollection()
        {
            int[] numbers = Enumerable.Range(0, 100).ToArray();

            ODataCollectionSerializer   serializer   = new ODataCollectionSerializer(new DefaultODataSerializerProvider());
            ODataCollectionDeserializer deserializer = new ODataCollectionDeserializer(_deserializerProvider);

            MemoryStream           stream        = new MemoryStream();
            ODataMessageWrapper    message       = new ODataMessageWrapper(stream);
            ODataMessageWriter     messageWriter = new ODataMessageWriter(message as IODataResponseMessage, new ODataMessageWriterSettings(), _model);
            ODataMessageReader     messageReader = new ODataMessageReader(message as IODataResponseMessage, new ODataMessageReaderSettings(), _model);
            ODataSerializerContext writeContext  = new ODataSerializerContext {
                RootElementName = "Property", Model = _model
            };
            ODataDeserializerContext readContext = new ODataDeserializerContext()
            {
                Model = _model
            };

            serializer.WriteObject(numbers, numbers.GetType(), messageWriter, writeContext);
            stream.Seek(0, SeekOrigin.Begin);
            IEnumerable readnumbers = deserializer.Read(messageReader, typeof(int[]), readContext) as IEnumerable;

            Assert.Equal(numbers, readnumbers.Cast <int>());
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultODataDeserializerProvider"/> class.
 /// </summary>
 public DefaultODataDeserializerProvider()
 {
     _actionPayloadDeserializer = new ODataActionPayloadDeserializer(this);
     _entityDeserializer = new ODataEntityDeserializer(this);
     _feedDeserializer = new ODataFeedDeserializer(this);
     _collectionDeserializer = new ODataCollectionDeserializer(this);
     _complexDeserializer = new ODataComplexTypeDeserializer(this);
 }
        public void ReadInline_ThrowsArgumentNull_EdmType()
        {
            var deserializer = new ODataCollectionDeserializer(new DefaultODataDeserializerProvider());

            Assert.ThrowsArgumentNull(
                () => deserializer.ReadInline(42, null, new ODataDeserializerContext()),
                "edmType");
        }
        public void ReadInline_ThrowsArgument_ArgumentMustBeOfType()
        {
            var deserializer = new ODataCollectionDeserializer(new DefaultODataDeserializerProvider());

            Assert.Throws <ArgumentException>(
                () => deserializer.ReadInline(42, _intCollectionType, new ODataDeserializerContext()),
                "The argument must be of type 'ODataCollectionValue'.\r\nParameter name: item");
        }
        public void Read_ThrowsArgumentNull_MessageReader()
        {
            var deserializer = new ODataCollectionDeserializer(new DefaultODataDeserializerProvider());

            Assert.ThrowsArgumentNull(
                () => deserializer.Read(messageReader: null, type: typeof(int[]), readContext: new ODataDeserializerContext()),
                "messageReader");
        }
Beispiel #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultODataDeserializerProvider"/> class.
 /// </summary>
 public DefaultODataDeserializerProvider()
 {
     _actionPayloadDeserializer = new ODataActionPayloadDeserializer(this);
     _entityDeserializer        = new ODataEntityDeserializer(this);
     _feedDeserializer          = new ODataFeedDeserializer(this);
     _collectionDeserializer    = new ODataCollectionDeserializer(this);
     _complexDeserializer       = new ODataComplexTypeDeserializer(this);
 }
        public void ReadCollectionValue_ThrowsArgumentNull_CollectionValue()
        {
            var deserializer = new ODataCollectionDeserializer(_intCollectionType, new DefaultODataDeserializerProvider());

            Assert.ThrowsArgumentNull(
                () => deserializer.ReadCollectionValue(collectionValue: null, readContext: new ODataDeserializerContext()).GetEnumerator().MoveNext(),
                "collectionValue");
        }
        public void Read_ThrowsArgumentMustBeOfType_Type()
        {
            var deserializer = new ODataCollectionDeserializer(new DefaultODataDeserializerProvider());

            Assert.ThrowsArgument(
                () => deserializer.Read(messageReader: ODataTestUtil.GetMockODataMessageReader(), type: typeof(int),
                                        readContext: new ODataDeserializerContext {
                Model = _model
            }),
                "type", "The argument must be of type 'Collection'.");
        }
        public void ReadCollectionValue_Throws_IfElementTypeCannotBeDeserialized()
        {
            Mock <ODataDeserializerProvider> deserializerProvider = new Mock <ODataDeserializerProvider>();

            deserializerProvider.Setup(p => p.GetEdmTypeDeserializer(_addressType)).Returns <ODataEntityDeserializer>(null);
            var deserializer = new ODataCollectionDeserializer(_addressCollectionType, deserializerProvider.Object);

            Assert.Throws <SerializationException>(
                () => deserializer.ReadCollectionValue(new ODataCollectionValue()
            {
                Items = new[] { 1, 2, 3 }
            }, new ODataDeserializerContext())
                .GetEnumerator()
                .MoveNext(),
                "'System.Web.Http.OData.Formatter.Serialization.Models.Address' cannot be deserialized using the ODataMediaTypeFormatter.");
        }
        public void Read_Roundtrip_PrimitiveCollection()
        {
            int[] numbers = Enumerable.Range(0, 100).ToArray();

            ODataCollectionSerializer   serializer   = new ODataCollectionSerializer(_intCollectionType, new DefaultODataSerializerProvider(_model));
            ODataCollectionDeserializer deserializer = new ODataCollectionDeserializer(_intCollectionType, _deserializerProvider);

            MemoryStream        stream  = new MemoryStream();
            ODataMessageWrapper message = new ODataMessageWrapper(stream);

            serializer.WriteObject(numbers, new ODataMessageWriter(message as IODataResponseMessage, new ODataMessageWriterSettings(), _model), new ODataSerializerContext {
                ServiceOperationName = "Property"
            });
            stream.Seek(0, SeekOrigin.Begin);
            IEnumerable readnumbers = deserializer.Read(new ODataMessageReader(message as IODataResponseMessage, new ODataMessageReaderSettings(), _model), new ODataDeserializerContext()) as IEnumerable;

            Assert.Equal(numbers, readnumbers.Cast <int>());
        }
        public void Read_ReturnsEdmComplexObjectCollection_TypelessMode()
        {
            // Arrange
            HttpContent content = new StringContent("{ 'value': [ { 'City' : 'Redmond' } ] }");

            content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
            IODataRequestMessage request = new ODataMessageWrapper(content.ReadAsStreamAsync().Result, content.Headers);
            ODataMessageReader   reader  = new ODataMessageReader(request, new ODataMessageReaderSettings(), _model);
            var deserializer             = new ODataCollectionDeserializer(_addressCollectionType, new DefaultODataDeserializerProvider());

            // Act
            var result = deserializer.Read(reader, new ODataDeserializerContext {
                Model = _model, ResourceType = typeof(IEdmObject)
            });

            // Assert
            IEdmObject edmObject = Assert.IsType <EdmComplexObjectCollection>(result);

            Assert.Equal(_addressCollectionType, edmObject.GetEdmType());
        }
        /// <inheritdoc />
        public override object Read(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext)
        {
            if (messageReader == null)
            {
                throw Error.ArgumentNull("messageReader");
            }

            IEdmFunctionImport action = GetFunctionImport(readContext);

            // Create the correct resource type;
            Dictionary <string, object> payload;

            if (type == typeof(ODataActionParameters))
            {
                payload = new ODataActionParameters();
            }
            else
            {
                payload = new ODataUntypedActionParameters(action);
            }

            ODataParameterReader reader = messageReader.CreateODataParameterReader(action);

            while (reader.Read())
            {
                string parameterName            = null;
                IEdmFunctionParameter parameter = null;

                switch (reader.State)
                {
                case ODataParameterReaderState.Value:
                    parameterName = reader.Name;
                    parameter     = action.Parameters.SingleOrDefault(p => p.Name == parameterName);
                    // ODataLib protects against this but asserting just in case.
                    Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName));
                    if (parameter.Type.IsPrimitive())
                    {
                        payload[parameterName] = reader.Value;
                    }
                    else
                    {
                        ODataEdmTypeDeserializer deserializer = DeserializerProvider.GetEdmTypeDeserializer(parameter.Type);
                        payload[parameterName] = deserializer.ReadInline(reader.Value, parameter.Type, readContext);
                    }
                    break;

                case ODataParameterReaderState.Collection:
                    parameterName = reader.Name;
                    parameter     = action.Parameters.SingleOrDefault(p => p.Name == parameterName);
                    // ODataLib protects against this but asserting just in case.
                    Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName));
                    IEdmCollectionTypeReference collectionType = parameter.Type as IEdmCollectionTypeReference;
                    Contract.Assert(collectionType != null);
                    ODataCollectionValue        value = ODataCollectionDeserializer.ReadCollection(reader.CreateCollectionReader());
                    ODataCollectionDeserializer collectionDeserializer = DeserializerProvider.GetEdmTypeDeserializer(collectionType) as ODataCollectionDeserializer;
                    payload[parameterName] = collectionDeserializer.ReadInline(value, collectionType, readContext);
                    break;

                default:
                    break;
                }
            }

            return(payload);
        }
        public void ReadInline_ReturnsNull_IfItemIsNull()
        {
            var deserializer = new ODataCollectionDeserializer(new DefaultODataDeserializerProvider());

            Assert.Null(deserializer.ReadInline(item: null, edmType: _intCollectionType, readContext: new ODataDeserializerContext()));
        }
        public void Ctor_SetsProperty_ElementType()
        {
            var deserializer = new ODataCollectionDeserializer(_intCollectionType, new DefaultODataDeserializerProvider());

            Assert.Equal(deserializer.ElementType, _intCollectionType.ElementType());
        }