Skip to content

Commit

Permalink
Merge pull request #239 from Cysharp/hadashiA/expose-media-type
Browse files Browse the repository at this point in the history
Expose media-type variable of `application/x-memorypack`
  • Loading branch information
hadashiA authored Mar 14, 2024
2 parents ad5dce5 + 5a924b8 commit e72d0d3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.Net.Http.Headers;

namespace MemoryPack.AspNetCoreMvcFormatter;

public static class MediaTypeHeaderValues
{
public static readonly MediaTypeHeaderValue ApplicationMemoryPack =
MediaTypeHeaderValue.Parse("application/x-memorypack");
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ namespace MemoryPack.AspNetCoreMvcFormatter;

public class MemoryPackInputFormatter : InputFormatter
{
const string ContentType = "application/x-memorypack";

public MemoryPackInputFormatter()
{
SupportedMediaTypes.Add(ContentType);
SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationMemoryPack);
}

public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Microsoft.AspNetCore.Mvc.Formatters;
using System.Net.Mime;
using Microsoft.AspNetCore.Mvc.Formatters;

namespace MemoryPack.AspNetCoreMvcFormatter;

public class MemoryPackOutputFormatter : OutputFormatter
{
const string ContentType = "application/x-memorypack";
readonly MemoryPackSerializerOptions? options;
readonly bool checkContentType = false;

Expand All @@ -17,14 +17,14 @@ public MemoryPackOutputFormatter(bool checkContentType = false)
public MemoryPackOutputFormatter(MemoryPackSerializerOptions options)
{
this.options = options;
SupportedMediaTypes.Add(ContentType);
SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationMemoryPack);
}

public override bool CanWriteResult(OutputFormatterCanWriteContext context)
{
if (checkContentType)
{
return (context.ContentType == ContentType);
return MediaTypeHeaderValues.ApplicationMemoryPack.MatchesMediaType(context.ContentType);
}
else
{
Expand All @@ -34,7 +34,7 @@ public override bool CanWriteResult(OutputFormatterCanWriteContext context)

public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
{
context.HttpContext.Response.ContentType = ContentType;
context.ContentType = MediaTypeHeaderValues.ApplicationMemoryPack.MediaType;

if (context.Object == null)
{
Expand Down

0 comments on commit e72d0d3

Please sign in to comment.