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

Commit 89158ba

Browse files
committed
UrlEncode string values in QueryStringSerializer
1 parent f5b715d commit 89158ba

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/ServiceStack.Text/Common/WriteType.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,8 @@ public static void WriteQueryString(TextWriter writer, object value)
443443
writer.Write('&');
444444

445445
var propertyType = propertyValue.GetType();
446-
var isEnumerable = !(propertyValue is string)
446+
var strValue = propertyValue as string;
447+
var isEnumerable = strValue == null
447448
&& !(propertyType.IsValueType())
448449
&& propertyType.HasInterface(typeof(IEnumerable));
449450

@@ -457,7 +458,11 @@ public static void WriteQueryString(TextWriter writer, object value)
457458
Serializer.WritePropertyName(writer, propertyWriter.PropertyName);
458459
writer.Write('=');
459460

460-
if (!isEnumerable)
461+
if (strValue != null)
462+
{
463+
writer.Write(strValue.UrlEncode());
464+
}
465+
else if (!isEnumerable)
461466
{
462467
propertyWriter.WriteFn(writer, propertyValue);
463468
}

tests/ServiceStack.Text.Tests/QueryStringSerializerTests.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ public void Deos_serialize_QueryStrings()
122122
[Test]
123123
public void Can_serialize_quoted_strings()
124124
{
125-
Assert.That(QueryStringSerializer.SerializeToString(new B { Property = "\"quoted content\"" }), Is.EqualTo("Property=%22%22quoted+content%22%22"));
126-
Assert.That(QueryStringSerializer.SerializeToString(new B { Property = "\"quoted content, and with a comma\"" }), Is.EqualTo("Property=%22%22quoted+content,+and+with+a+comma%22%22"));
125+
Assert.That(QueryStringSerializer.SerializeToString(new B { Property = "\"quoted content\"" }), Is.EqualTo("Property=%22quoted+content%22"));
126+
Assert.That(QueryStringSerializer.SerializeToString(new B { Property = "\"quoted content, and with a comma\"" }), Is.EqualTo("Property=%22quoted+content,+and+with+a+comma%22"));
127127
}
128128

129129
private T StringToPoco<T>(string str)
@@ -184,6 +184,24 @@ public void Can_deserialize_with_comma_in_property_in_list_from_static()
184184
Assert.That(poco.ListOfA[0].ListOfB[0].Property3, Is.EqualTo("John"));
185185
}
186186

187+
[Test]
188+
public void Can_serialize_Poco_with_comma_in_string()
189+
{
190+
var dto = new B { Property = "Foo,Bar" };
191+
var qs = QueryStringSerializer.SerializeToString(dto);
192+
193+
Assert.That(qs, Is.EqualTo("Property=Foo,Bar"));
194+
}
195+
196+
[Test]
197+
public void Does_urlencode_Poco_with_escape_char()
198+
{
199+
var dto = new B { Property = "Foo&Bar" };
200+
var qs = QueryStringSerializer.SerializeToString(dto);
201+
202+
Assert.That(qs, Is.EqualTo("Property=Foo%26Bar"));
203+
}
204+
187205
public class TestPocos
188206
{
189207
public List<A> ListOfA { get; set; }

0 commit comments

Comments
 (0)