mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-11-16 02:18:54 -07:00
feat: extend CommaDelimitedArrayModelBinder to support auto generated openAPI spec
This commit is contained in:
parent
ba12ea7f4a
commit
4b4c74bdcd
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user