public override object Read(ODataMessageReader messageReader, ODataDeserializerContext readContext) { // Create the correct resource type; ODataActionParameters payload = CreateNewPayload(); IEdmFunctionImport action = payload.GetFunctionImport(readContext); 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)); payload[parameterName] = Convert(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); payload[parameterName] = Convert(reader.CreateCollectionReader(), collectionType, readContext); break; default: break; } } return(payload); }
public void Can_deserialize_payload_with_complex_parameters() { string actionName = "Complex"; string body = @"{ ""Quantity"": 1 , ""Address"": { ""StreetAddress"":""1 Microsoft Way"", ""City"": ""Redmond"", ""State"": ""WA"", ""ZipCode"": 98052 } }"; ODataMessageWrapper message = new ODataMessageWrapper(GetStringAsStream(body)); message.SetHeader("Content-Type", "application/json;odata=verbose"); IEdmModel model = GetModel(); ODataMessageReader reader = new ODataMessageReader(message as IODataRequestMessage, new ODataMessageReaderSettings(), model); ODataActionPayloadDeserializer deserializer = new ODataActionPayloadDeserializer(typeof(ODataActionParameters), new DefaultODataDeserializerProvider(model)); string url = "http://server/service/EntitySet(key)/" + actionName; HttpRequestMessage request = GetPostRequest(url); ODataDeserializerContext context = new ODataDeserializerContext { Request = request, Model = model }; ODataActionParameters payload = deserializer.Read(reader, context) as ODataActionParameters; Assert.NotNull(payload); Assert.Same(model.EntityContainers().Single().FunctionImports().SingleOrDefault(f => f.Name == "Complex"), payload.GetFunctionImport(context)); Assert.True(payload.ContainsKey("Quantity")); Assert.Equal(1, payload["Quantity"]); Assert.True(payload.ContainsKey("Address")); MyAddress address = payload["Address"] as MyAddress; Assert.NotNull(address); Assert.Equal("1 Microsoft Way", address.StreetAddress); Assert.Equal("Redmond", address.City); Assert.Equal("WA", address.State); Assert.Equal(98052, address.ZipCode); }
public void Can_deserialize_payload_with_primitive_collection_parameters() { string actionName = "PrimitiveCollection"; string body = @"{ ""Name"": ""Avatar"", ""Ratings"": [ 5, 5, 3, 4, 5, 5, 4, 5, 5, 4 ] }"; int[] expectedRatings = new int[] { 5, 5, 3, 4, 5, 5, 4, 5, 5, 4 }; ODataMessageWrapper message = new ODataMessageWrapper(GetStringAsStream(body)); message.SetHeader("Content-Type", "application/json;odata=verbose"); IEdmModel model = GetModel(); ODataMessageReader reader = new ODataMessageReader(message as IODataRequestMessage, new ODataMessageReaderSettings(), model); ODataActionPayloadDeserializer deserializer = new ODataActionPayloadDeserializer(typeof(ODataActionParameters), new DefaultODataDeserializerProvider(model)); string url = "http://server/service/EntitySet(key)/" + actionName; HttpRequestMessage request = GetPostRequest(url); ODataDeserializerContext context = new ODataDeserializerContext { Request = request, Model = model }; ODataActionParameters payload = deserializer.Read(reader, context) as ODataActionParameters; Assert.NotNull(payload); Assert.Same(model.EntityContainers().Single().FunctionImports().SingleOrDefault(f => f.Name == "PrimitiveCollection"), payload.GetFunctionImport(context)); Assert.True(payload.ContainsKey("Name")); Assert.Equal("Avatar", payload["Name"]); Assert.True(payload.ContainsKey("Ratings")); IList <int> ratings = payload["Ratings"] as IList <int>; Assert.Equal(10, ratings.Count); Assert.True(expectedRatings.Zip(ratings, (expected, actual) => expected - actual).All(diff => diff == 0)); }
public void Can_deserialize_payload_with_primitive_parameters() { string actionName = "Primitive"; int quantity = 1; string productCode = "PCode"; string body = "{" + string.Format(@" ""Quantity"": {0} , ""ProductCode"": ""{1}"" ", quantity, productCode) + "}"; ODataMessageWrapper message = new ODataMessageWrapper(GetStringAsStream(body)); message.SetHeader("Content-Type", "application/json;odata=verbose"); IEdmModel model = GetModel(); ODataMessageReader reader = new ODataMessageReader(message as IODataRequestMessage, new ODataMessageReaderSettings(), model); ODataActionPayloadDeserializer deserializer = new ODataActionPayloadDeserializer(typeof(ODataActionParameters), new DefaultODataDeserializerProvider(model)); string url = "http://server/service/EntitySet(key)/" + actionName; HttpRequestMessage request = GetPostRequest(url); ODataDeserializerContext context = new ODataDeserializerContext { Request = request, Model = model }; ODataActionParameters payload = deserializer.Read(reader, context) as ODataActionParameters; Assert.NotNull(payload); Assert.Same(model.EntityContainers().Single().FunctionImports().SingleOrDefault(f => f.Name == "Primitive"), payload.GetFunctionImport(context)); Assert.True(payload.ContainsKey("Quantity")); Assert.Equal(quantity, payload["Quantity"]); Assert.True(payload.ContainsKey("ProductCode")); Assert.Equal(productCode, payload["ProductCode"]); }