Skip to content

Commit 4ca3654

Browse files
author
Chris Santero
committed
safeguards against invalid raw json strings
Conflicts: JSONAPI.Tests/Json/JsonApiMediaFormaterTests.cs
1 parent 2cfe6e5 commit 4ca3654

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"comments": [
3+
{
4+
"id": "5",
5+
"body": null,
6+
"customData": { },
7+
"links": {
8+
"post": null
9+
}
10+
}
11+
]
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"comments": [
3+
{
4+
"id": "5",
5+
"body": null,
6+
"customData": {
7+
"unquotedKey": 5
8+
},
9+
"links": {
10+
"post": null
11+
}
12+
}
13+
]
14+
}

JSONAPI.Tests/JSONAPI.Tests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@
9494
</ItemGroup>
9595
<ItemGroup>
9696
<None Include="app.config" />
97+
<None Include="Data\ReformatsRawJsonStringWithUnquotedKeys.json">
98+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
99+
</None>
100+
<None Include="Data\MalformedRawJsonString.json">
101+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
102+
</None>
97103
<None Include="Data\FormatterErrorSerializationTest.json">
98104
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
99105
</None>

JSONAPI.Tests/Json/JsonApiMediaFormaterTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,44 @@ public void SerializeArrayIntegrationTest()
176176
//Assert.AreEqual("[2,3,4]", sw.ToString());
177177
}
178178

179+
[TestMethod]
180+
[DeploymentItem(@"Data\ReformatsRawJsonStringWithUnquotedKeys.json")]
181+
public void Reformats_raw_json_string_with_unquoted_keys()
182+
{
183+
// Arrange
184+
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
185+
MemoryStream stream = new MemoryStream();
186+
187+
// Act
188+
var payload = new [] { new Comment { Id = 5, CustomData = "{ unquotedKey: 5 }"}};
189+
formatter.WriteToStreamAsync(typeof(Comment), payload, stream, null, null);
190+
191+
// Assert
192+
var minifiedExpectedJson = JsonHelpers.MinifyJson(File.ReadAllText("ReformatsRawJsonStringWithUnquotedKeys.json"));
193+
string output = System.Text.Encoding.ASCII.GetString(stream.ToArray());
194+
Trace.WriteLine(output);
195+
output.Should().Be(minifiedExpectedJson);
196+
}
197+
198+
[TestMethod]
199+
[DeploymentItem(@"Data\MalformedRawJsonString.json")]
200+
public void Does_not_serialize_malformed_raw_json_string()
201+
{
202+
// Arrange
203+
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
204+
MemoryStream stream = new MemoryStream();
205+
206+
// Act
207+
var payload = new[] { new Comment { Id = 5, CustomData = "{ x }" } };
208+
formatter.WriteToStreamAsync(typeof(Comment), payload, stream, null, null);
209+
210+
// Assert
211+
var minifiedExpectedJson = JsonHelpers.MinifyJson(File.ReadAllText("MalformedRawJsonString.json"));
212+
string output = System.Text.Encoding.ASCII.GetString(stream.ToArray());
213+
Trace.WriteLine(output);
214+
output.Should().Be(minifiedExpectedJson);
215+
}
216+
179217
[TestMethod]
180218
[DeploymentItem(@"Data\FormatterErrorSerializationTest.json")]
181219
public void Should_serialize_error()

JSONAPI/Json/JsonApiFormatter.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ internal JsonApiFormatter(IModelManager modelManager, IErrorSerializer errorSeri
4141
_modelManager = modelManager;
4242
_errorSerializer = errorSerializer;
4343
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/vnd.api+json"));
44+
ValidateRawJsonStrings = true;
4445
}
4546

47+
public bool ValidateRawJsonStrings { get; set; }
48+
4649
[Obsolete("Use ModelManager.PluralizationService instead")]
4750
public IPluralizationService PluralizationService //FIXME: Deprecated, will be removed shortly
4851
{
@@ -215,8 +218,21 @@ protected void Serialize(object value, Stream writeStream, JsonWriter writer, Js
215218
}
216219
else
217220
{
218-
var minifiedValue = JsonHelpers.MinifyJson((string) propertyValue);
219-
writer.WriteRawValue(minifiedValue);
221+
var json = (string) propertyValue;
222+
if (ValidateRawJsonStrings)
223+
{
224+
try
225+
{
226+
var token = JToken.Parse(json);
227+
json = token.ToString();
228+
}
229+
catch (Exception)
230+
{
231+
json = "{}";
232+
}
233+
}
234+
var valueToSerialize = JsonHelpers.MinifyJson(json);
235+
writer.WriteRawValue(valueToSerialize);
220236
}
221237
}
222238
else

0 commit comments

Comments
 (0)