Exemplo n.º 1
0
        private IEnumerable <T> ReadCollectionResponse <T>(Stream stream, IEdmModel model)
        {
            ODataMessageWrapper      message       = new ODataMessageWrapper(stream);
            ODataMessageReader       messageReader = new ODataMessageReader(message as IODataResponseMessage, new ODataMessageReaderSettings(), model);
            ODataDeserializerContext readContext   = new ODataDeserializerContext()
            {
                Model = model
            };
            IEnumerable readEntities = _resourceSetDeserializer.Read(messageReader, typeof(T[]), readContext) as IEnumerable;

            return(readEntities.Cast <T>());
        }
Exemplo n.º 2
0
        public void Read_Roundtrip_ComplexCollection()
        {
            // Arrange
            Address[] addresses = new[]
            {
                new Address {
                    City = "Redmond", StreetAddress = "A", State = "123"
                },
                new Address {
                    City = "Seattle", StreetAddress = "S", State = "321"
                }
            };

            ODataResourceSetSerializer   serializer   = new ODataResourceSetSerializer(_serializerProvider);
            ODataResourceSetDeserializer deserializer = new ODataResourceSetDeserializer(_deserializerProvider);

            MemoryStream               stream   = new MemoryStream();
            ODataMessageWrapper        message  = new ODataMessageWrapper(stream);
            ODataMessageWriterSettings settings = new ODataMessageWriterSettings
            {
                ODataUri = new ODataUri {
                    ServiceRoot = new Uri("http://any/")
                }
            };
            ODataMessageWriter     messageWriter = new ODataMessageWriter(message as IODataResponseMessage, settings, _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
            };

            // Act
            serializer.WriteObject(addresses, addresses.GetType(), messageWriter, writeContext);
            stream.Seek(0, SeekOrigin.Begin);
            IEnumerable readAddresses = deserializer.Read(messageReader, typeof(Address[]), readContext) as IEnumerable;

            // Assert
            Assert.Equal(addresses, readAddresses.Cast <Address>(), new AddressComparer());
        }
Exemplo n.º 3
0
        public async Task Read_ReturnsEdmComplexObjectCollection_TypelessMode()
        {
            // Arrange
            IEdmTypeReference           addressType           = _model.GetEdmTypeReference(typeof(Address)).AsComplex();
            IEdmCollectionTypeReference addressCollectionType =
                new EdmCollectionTypeReference(new EdmCollectionType(addressType));

            HttpContent      content    = new StringContent("{ 'value': [ {'@odata.type':'Microsoft.AspNetCore.OData.Tests.Models.Address', 'City' : 'Redmond' } ] }");
            HeaderDictionary headerDict = new HeaderDictionary
            {
                { "Content-Type", "application/json" }
            };

            IODataRequestMessage request         = ODataMessageWrapperHelper.Create(await content.ReadAsStreamAsync(), headerDict);
            ODataMessageReader   reader          = new ODataMessageReader(request, new ODataMessageReaderSettings(), _model);
            var deserializer                     = new ODataResourceSetDeserializer(_deserializerProvider);
            ODataDeserializerContext readContext = new ODataDeserializerContext
            {
                Model           = _model,
                ResourceType    = typeof(IEdmObject),
                ResourceEdmType = addressCollectionType
            };

            // Act
            IEnumerable result = deserializer.Read(reader, typeof(IEdmObject), readContext) as IEnumerable;

            // Assert
            var addresses = result.Cast <EdmComplexObject>();

            Assert.NotNull(addresses);

            EdmComplexObject address = Assert.Single(addresses);

            Assert.Equal(new[] { "City" }, address.GetChangedPropertyNames());

            object city;

            Assert.True(address.TryGetPropertyValue("City", out city));
            Assert.Equal("Redmond", city);
        }