From 9cf863471d1848cd9484ee7e63483823b230b3c8 Mon Sep 17 00:00:00 2001 From: Oskar Johansson Date: Fri, 26 Feb 2016 13:42:35 +0100 Subject: [PATCH] Guid hanling and explicit null handling String guid to System.Guid fixed, proper handling of explicit null in json body ( {example: null } ) --- Source/MultiPostParameterBinding.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Source/MultiPostParameterBinding.cs b/Source/MultiPostParameterBinding.cs index 797172e..babe08d 100644 --- a/Source/MultiPostParameterBinding.cs +++ b/Source/MultiPostParameterBinding.cs @@ -65,7 +65,7 @@ public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, stringValue = parameters[Descriptor.ParameterName]; // if not found in body, try reading query string - if (stringValue == null) + if (stringValue == null && !parameters.AllKeys.Contains(Descriptor.ParameterName)) { var queryStringPairs = actionContext.Request.GetQueryNameValuePairs(); if (queryStringPairs != null) @@ -84,7 +84,7 @@ public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, else if (Descriptor.ParameterType.IsEnum) paramValue = Enum.Parse(Descriptor.ParameterType, stringValue); else if (Descriptor.ParameterType.IsPrimitive || Descriptor.ParameterType.IsValueType) // TODO: Are these conditions ok? I'd rather not have to check that the type implements IConvertible. - paramValue = Convert.ChangeType(stringValue, Descriptor.ParameterType); + paramValue = TypeDescriptor.GetConverter(Descriptor.ParameterType).ConvertFromInvariantString(stringValue); else // when deserializing an object, pass in the global settings so that custom converters, etc. are honored paramValue = JsonConvert.DeserializeObject(stringValue, Descriptor.ParameterType, GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings); @@ -92,6 +92,10 @@ public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, // Set the binding result here SetValue(actionContext, paramValue); } + else if(parameters.AllKeys.Contains(Descriptor.ParameterName)) + { + SetValue(actionContext, null); + } // now, we can return a completed task with no result TaskCompletionSource tcs = new TaskCompletionSource(); @@ -125,7 +129,7 @@ private NameValueCollection ParseParametersFromBody(HttpRequestMessage request) var values = JsonConvert.DeserializeObject>(content); result = values.Aggregate(new NameValueCollection(), (seed, current) => { - seed.Add(current.Key, current.Value == null ? "" : current.Value.ToString()); + seed.Add(current.Key, current.Value == null ? null : current.Value.ToString()); return seed; }); break; @@ -171,4 +175,4 @@ public static MultiPostParameterBinding CreateBindingForMarkedParameters(HttpPar } } -} \ No newline at end of file +}