From 78c5897e9578040b51daaa0f73e9daa6677405da Mon Sep 17 00:00:00 2001 From: Atsushi Eno Date: Wed, 24 Aug 2011 12:02:38 -0700 Subject: [PATCH] When there was no path parameter, it ignored the whole path part. --- .../System.ServiceModel.Web/System/UriTemplate.cs | 24 +++++++++++++++------- .../Test/System/UriTemplateTest.cs | 9 ++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/mcs/class/System.ServiceModel.Web/System/UriTemplate.cs b/mcs/class/System.ServiceModel.Web/System/UriTemplate.cs index 08e7059b601..5277d5b9618 100644 --- a/mcs/class/System.ServiceModel.Web/System/UriTemplate.cs +++ b/mcs/class/System.ServiceModel.Web/System/UriTemplate.cs @@ -163,6 +163,15 @@ namespace System void BindByName (ref int src, StringBuilder sb, ReadOnlyCollection names, NameValueCollection nvc, IDictionary dic, bool omitDefaults, bool query) { + if (query) { + int idx = template.IndexOf ('?', src); + if (idx > 0) { + sb.Append (template.Substring (src, idx - src)); + src = idx; + // note that it doesn't append '?'. It is added only when there is actual parameter binding. + } + } + foreach (string name in names) { int s = template.IndexOf ('{', src); int e = template.IndexOf ('}', s + 1); @@ -173,18 +182,19 @@ namespace System #endif if (dic != null) dic.TryGetValue (name, out value); + if (query) { if (value != null || (!omitDefaults && Defaults.TryGetValue (name, out value))) { sb.Append (template.Substring (src, s - src)); sb.Append (value); } - } else - if (value == null && (omitDefaults || !Defaults.TryGetValue(name, out value))) - throw new ArgumentException(string.Format("The argument name value collection does not contain non-nul vaalue for '{0}'", name), "parameters"); - else { - sb.Append (template.Substring (src, s - src)); - sb.Append (value); - } + } else { + if (value == null && (omitDefaults || !Defaults.TryGetValue (name, out value))) + throw new ArgumentException (string.Format("The argument name value collection does not contain non-null value for '{0}'", name), "parameters"); + + sb.Append (template.Substring (src, s - src)); + sb.Append (value); + } src = e + 1; } } diff --git a/mcs/class/System.ServiceModel.Web/Test/System/UriTemplateTest.cs b/mcs/class/System.ServiceModel.Web/Test/System/UriTemplateTest.cs index 73893cbd04c..3392c3654a3 100644 --- a/mcs/class/System.ServiceModel.Web/Test/System/UriTemplateTest.cs +++ b/mcs/class/System.ServiceModel.Web/Test/System/UriTemplateTest.cs @@ -229,6 +229,15 @@ namespace MonoTests.System } [Test] + public void BindByName3 () + { + var t = new UriTemplate ("Login?clientLoginData={clientLoginData}&credentials={credentials}"); + var n = new NameValueCollection (); + var u = t.BindByName (new Uri ("http://localhost"), n); + Assert.AreEqual ("http://localhost/Login", u.ToString (), "#1"); + } + + [Test] public void BindByNameManySlashes () { var t = new UriTemplate ("////{foo}/{bar}/"); -- 2.11.4.GIT