Skip to content

Commit 13f530e

Browse files
encode string param (#30) +semver: minor
1 parent e9516f2 commit 13f530e

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

logo.pdn

9.47 KB
Binary file not shown.

logo.png

3.13 KB
Loading

src/GraphQL.Query.Builder/QueryStringBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public void Clear()
4545
/// - Key value pair: `foo:"bar"` or `foo:10` ...
4646
/// - List: `["foo","bar"]` or `[1,2]` ...
4747
/// - Dictionary: `{foo:"bar",b:10}`
48+
/// - Object: `{foo:"bar",b:10}`
4849
/// </summary>
4950
/// <param name="value"></param>
5051
/// <returns>The formatted query param.</returns>
@@ -54,7 +55,8 @@ internal protected virtual string FormatQueryParam(object value)
5455
switch (value)
5556
{
5657
case string strValue:
57-
return "\"" + strValue + "\"";
58+
string encoded = strValue.Replace("\"", "\\\"");
59+
return $"\"{encoded}\"";
5860

5961
case byte byteValue:
6062
return byteValue.ToString();

tests/GraphQL.Query.Builder.UnitTests/QueryStringBuilderTests.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ public void TestFormatQueryParam_string()
2121
Assert.Equal("\"value\"", new QueryStringBuilder().FormatQueryParam(value));
2222
}
2323

24+
[Fact]
25+
public void TestFormatQueryParam_string_json()
26+
{
27+
string value = "{\"foo\":\"bar\",\"array\":[1,2]}";
28+
Assert.Equal(
29+
"\"{\\\"foo\\\":\\\"bar\\\",\\\"array\\\":[1,2]}\"",
30+
new QueryStringBuilder().FormatQueryParam(value));
31+
}
32+
2433
[Fact]
2534
public void TestFormatQueryParam_byte()
2635
{
@@ -265,7 +274,7 @@ public void TestFormatQueryParam_Object()
265274
};
266275

267276
Assert.Equal("{Age:10,Name:\"Test\",Orders:[{Product:{load:{weight:45},name:\"Truck 1\",wheelsNumber:6}}]}", new QueryStringBuilder().FormatQueryParam(@object));
268-
277+
269278
// with inner object with null property
270279
@object = new Customer
271280
{

0 commit comments

Comments
 (0)