app/BTCPayServer.Plugins.App/API/ProtobufFormatterAttribute.cs
2025-01-20 18:22:46 +01:00

42 lines
1.3 KiB
C#

using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace BTCPayServer.Plugins.App.API;
public class ProtobufFormatterAttribute : ActionFilterAttribute, IControllerModelConvention, IActionModelConvention
{
public void Apply(ControllerModel controller)
{
foreach (var action in controller.Actions)
{
Apply(action);
}
}
public void Apply(ActionModel action)
{
// Set the model binder to NewtonsoftJsonBodyModelBinder for parameters that are bound to the request body.
var parameters = action.Parameters.Where(p => p.BindingInfo?.BindingSource == BindingSource.Body);
foreach (var p in parameters)
{
if (p.BindingInfo != null) p.BindingInfo.BinderType = typeof(ProtobufFormatterModelBinder);
}
}
public override void OnActionExecuted(ActionExecutedContext context)
{
if (context.Result is ObjectResult objectResult)
{
objectResult.Formatters.Clear();
objectResult.Formatters.Add(new ProtobufOutputFormatter());
}
else
{
base.OnActionExecuted(context);
}
}
}