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

Commit 5e274ee

Browse files
committed
Do not alllow serializing Task's
1 parent c40ecde commit 5e274ee

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/ServiceStack.Text/Common/JsWriter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,15 @@ public void WriteValue(TextWriter writer, object value)
340340
var valueWriter = (IValueWriter)value;
341341
valueWriter.WriteTo(Serializer, writer);
342342
}
343+
344+
void ThrowTaskNotSupported(TextWriter writer, object value) =>
345+
throw new NotSupportedException("Serializing Task's is not supported. Did you forget to await it?");
343346

344347
private WriteObjectDelegate GetCoreWriteFn<T>()
345348
{
349+
if (typeof(T).IsInstanceOf(typeof(System.Threading.Tasks.Task)))
350+
return ThrowTaskNotSupported;
351+
346352
if (typeof(T).IsValueType && !JsConfig.TreatAsRefType(typeof(T)) || JsConfig<T>.HasSerializeFn)
347353
{
348354
return JsConfig<T>.HasSerializeFn

tests/ServiceStack.Text.Tests/SpecialTypesTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.IO;
4+
using System.Threading.Tasks;
45
using NUnit.Framework;
56
using ServiceStack.Web;
67

@@ -122,5 +123,13 @@ public void Does_not_serialize_Streams()
122123
var dto = new RawRequest { Id = 1, RequestStream = new MemoryStream() };
123124
Serialize(dto, includeXml: false);
124125
}
126+
127+
[Test]
128+
public void Serializing_Tasks_throws_NotSupportedException()
129+
{
130+
Assert.Throws<NotSupportedException>(() => Task.FromResult(1).ToJson());
131+
Assert.Throws<NotSupportedException>(() => Task.FromResult(1).ToJsv());
132+
}
133+
125134
}
126135
}

0 commit comments

Comments
 (0)