feat: extend CommaDelimitedArrayModelBinder to support auto generated openAPI spec

This commit is contained in:
github@esslinger.dev 2020-10-01 22:04:53 +02:00
parent ba12ea7f4a
commit 4b4c74bdcd

View File

@ -15,14 +15,30 @@ namespace Jellyfin.Api.ModelBinders
public Task BindModelAsync(ModelBindingContext bindingContext)
{
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
var input = valueProviderResult.FirstValue;
var elementType = bindingContext.ModelType.GetElementType();
if (input != null)
{
var converter = TypeDescriptor.GetConverter(elementType);
if (valueProviderResult.Length > 1)
{
var result = Array.CreateInstance(elementType, valueProviderResult.Length);
for (int i = 0; i < valueProviderResult.Length; i++)
{
var value = converter.ConvertFromString(valueProviderResult.Values[i].Trim());
result.SetValue(value, i);
}
bindingContext.Result = ModelBindingResult.Success(result);
}
else
{
var value = valueProviderResult.FirstValue;
if (value != null)
{
var values = Array.ConvertAll(
input.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries),
value.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries),
x => { return converter.ConvertFromString(x != null ? x.Trim() : x); });
var typedValues = Array.CreateInstance(elementType, values.Length);
@ -35,6 +51,7 @@ namespace Jellyfin.Api.ModelBinders
var emptyResult = Array.CreateInstance(elementType, 0);
bindingContext.Result = ModelBindingResult.Success(emptyResult);
}
}
return Task.CompletedTask;
}