Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 7a6e853

Browse files
committed
Fix QueryString serializer of top-level string values
1 parent f7337e6 commit 7a6e853

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/ServiceStack.Text/QueryStringSerializer.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ public static void WriteIDictionary(TextWriter writer, object oMap)
165165
foreach (var key in map.Keys)
166166
{
167167
var dictionaryValue = map[key];
168-
if (dictionaryValue == null) continue;
168+
if (dictionaryValue == null)
169+
continue;
169170

170171
if (writeKeyFn == null)
171172
{
@@ -174,7 +175,11 @@ public static void WriteIDictionary(TextWriter writer, object oMap)
174175
}
175176

176177
if (writeValueFn == null || isObjectDictionary)
177-
writeValueFn = Serializer.GetWriteFn(dictionaryValue.GetType());
178+
{
179+
writeValueFn = dictionaryValue is string
180+
? (w,x) => w.Write(((string)x).UrlEncode())
181+
: Serializer.GetWriteFn(dictionaryValue.GetType());
182+
}
178183

179184
if (ranOnce)
180185
writer.Write("&");

tests/ServiceStack.Text.Tests/QueryStringSerializerTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ public void Can_Serialize_Unicode_Query_String()
4848
Is.EqualTo("A=%e5%b4%91%e2%a8%b9%e5%a0%a1%ea%81%80%e1%a2%96%e3%a4%b9%c3%ac%e3%ad%a1%ec%a4%aa%e9%8a%ac"));
4949
}
5050

51+
[Test]
52+
public void Does_serialize_Poco_and_string_dictionary_with_encoded_data()
53+
{
54+
var msg = "Field with comma, to demo. ";
55+
Assert.That(QueryStringSerializer.SerializeToString(new D { A = msg }),
56+
Is.EqualTo("A=Field+with+comma,+to+demo.+"));
57+
58+
Assert.That(QueryStringSerializer.SerializeToString(new D { A = msg }.ToStringDictionary()),
59+
Is.EqualTo("A=Field+with+comma,+to+demo.+"));
60+
}
61+
5162
class Empty { }
5263

5364
[Test]

0 commit comments

Comments
 (0)