Fix random seed parsing when non uint where specifed

This commit is contained in:
Zoe Roux 2024-03-24 15:25:03 +01:00
parent 9f42c29714
commit dee3af3016
No known key found for this signature in database
2 changed files with 6 additions and 2 deletions

View File

@ -102,7 +102,11 @@ public record Sort<T> : Sort
return new Conglomerate(sortBy.Split(',').Select(x => From(x, seed)).ToArray());
if (sortBy.StartsWith("random:"))
return new Random(uint.Parse(sortBy["random:".Length..]));
{
if (uint.TryParse(sortBy["random:".Length..], out uint sseed))
return new Random(sseed);
throw new ValidationException("Invalid random seed specified. Expected a number.");
}
string key = sortBy.Contains(':') ? sortBy[..sortBy.IndexOf(':')] : sortBy;
string? order = sortBy.Contains(':') ? sortBy[(sortBy.IndexOf(':') + 1)..] : null;

View File

@ -43,7 +43,7 @@ public class SortBinder : IModelBinder
{
object sort = bindingContext
.ModelType.GetMethod(nameof(Sort<Movie>.From))!
.Invoke(null, new object?[] { sortBy.FirstValue, seed })!;
.Invoke(null, [sortBy.FirstValue, seed])!;
bindingContext.Result = ModelBindingResult.Success(sort);
bindingContext.HttpContext.Items["seed"] = seed;
return Task.CompletedTask;