Skip to content

Commit

Permalink
Merge pull request #763 from leancodepl/fix/response-contenttype
Browse files Browse the repository at this point in the history
Allow specifying content type in ISerializer
  • Loading branch information
jakubfijalkowski authored Feb 21, 2025
2 parents 383836a + a474327 commit 97eeedb
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private async Task SerializeResultAsync(HttpContext httpContext, CQRSObjectMetad

if (result.HasPayload)
{
httpContext.Response.ContentType = "application/json";
httpContext.Response.ContentType = serializer.ContentType;
if (result.Payload is null)
{
await httpContext.Response.Body.WriteAsync(NullString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ namespace LeanCode.CQRS.AspNetCore.Serialization;

public interface ISerializer
{
string ContentType { get; }
Task SerializeAsync(Stream utf8Json, object value, Type inputType, CancellationToken cancellationToken);
ValueTask<object?> DeserializeAsync(Stream utf8Json, Type returnType, CancellationToken cancellationToken);
}

public sealed class Utf8JsonSerializer(IOptions<JsonOptions> options) : ISerializer
{
public string ContentType => "application/json; charset=utf-8";

public ValueTask<object?> DeserializeAsync(Stream utf8Json, Type returnType, CancellationToken cancellationToken)
{
return JsonSerializer.DeserializeAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public sealed class CQRSMiddlewareTests : CQRSMiddlewareTestBase<CQRSMiddleware>

protected override void ConfigureServices(IServiceCollection services)
{
serializer.ContentType.Returns("application/json");
services.AddSingleton(_ => serializer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ private static ServiceProvider BuildProvider(Action<CQRSServicesBuilder>? config

internal sealed class CustomSerializer : ISerializer
{
public string ContentType => throw new NotImplementedException();

public ValueTask<object?> DeserializeAsync(
Stream utf8Json,
Type returnType,
Expand Down

0 comments on commit 97eeedb

Please sign in to comment.