2024-08-30 13:29:48 +00:00
|
|
|
using System;
|
2020-11-21 18:58:35 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
2023-01-31 11:18:10 +00:00
|
|
|
namespace Jellyfin.Api.ModelBinders;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Nullable enum model binder provider.
|
|
|
|
/// </summary>
|
|
|
|
public class NullableEnumModelBinderProvider : IModelBinderProvider
|
2020-11-21 18:58:35 +00:00
|
|
|
{
|
2023-01-31 11:18:10 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
public IModelBinder? GetBinder(ModelBinderProviderContext context)
|
2020-11-21 18:58:35 +00:00
|
|
|
{
|
2023-01-31 11:18:10 +00:00
|
|
|
var nullableType = Nullable.GetUnderlyingType(context.Metadata.ModelType);
|
|
|
|
if (nullableType is null || !nullableType.IsEnum)
|
2020-11-21 18:58:35 +00:00
|
|
|
{
|
2023-01-31 11:18:10 +00:00
|
|
|
// Type isn't nullable or isn't an enum.
|
|
|
|
return null;
|
2020-11-21 18:58:35 +00:00
|
|
|
}
|
2023-01-31 11:18:10 +00:00
|
|
|
|
|
|
|
var logger = context.Services.GetRequiredService<ILogger<NullableEnumModelBinder>>();
|
|
|
|
return new NullableEnumModelBinder(logger);
|
2020-11-21 18:58:35 +00:00
|
|
|
}
|
2021-12-24 17:28:27 +00:00
|
|
|
}
|