Skip to content

Commit eb57eca

Browse files
committed
Fixes when T is not the implementor (i.e. interface) on json serialization
1 parent 6093967 commit eb57eca

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/Commands.Http/Commands.Http/Execution/HttpCommandContext.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,14 @@ public virtual void Respond(IHttpResult result)
121121
}
122122
else
123123
{
124-
var jsonBytes = Response.ContentEncoding.GetBytes(JsonSerializer.Serialize(result.Content, result.Content.GetType(), _services.GetService<JsonSerializerOptions>()));
124+
string serializationResult;
125+
126+
if (result.Content is Tuple<Type, object> objectReferredByT)
127+
serializationResult = JsonSerializer.Serialize(objectReferredByT.Item2, objectReferredByT.Item1, _services.GetService<JsonSerializerOptions>());
128+
else
129+
serializationResult = JsonSerializer.Serialize(result.Content, result.Content.GetType(), _services.GetService<JsonSerializerOptions>());
130+
131+
var jsonBytes = Response.ContentEncoding.GetBytes(serializationResult);
125132

126133
Response.ContentLength64 = jsonBytes.Length;
127134

src/Commands.Http/Commands.Http/Results/HttpResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ public override string ToString()
8787
/// <param name="content">The response to send.</param>
8888
/// <param name="statusCode">The status code of the response.</param>
8989
/// <returns>A new instance of <see cref="HttpResult"/> containing the values to be served to the caller invoking this operation.</returns>
90-
public static HttpResult Json(object content, HttpStatusCode statusCode = HttpStatusCode.OK)
90+
public static HttpResult Json<T>([DisallowNull] T content, HttpStatusCode statusCode = HttpStatusCode.OK)
9191
{
9292
Assert.NotNull(content, nameof(content));
9393

94-
return new(statusCode, content, "application/json");
94+
return new(statusCode, new Tuple<Type, object>(typeof(T), content), "application/json");
9595
}
9696

9797
/// <summary>

0 commit comments

Comments
 (0)