diff --git a/src/Sonarr.Http/REST/RestController.cs b/src/Sonarr.Http/REST/RestController.cs index 1f3ccc4a8..ee32c6a09 100644 --- a/src/Sonarr.Http/REST/RestController.cs +++ b/src/Sonarr.Http/REST/RestController.cs @@ -26,6 +26,7 @@ namespace Sonarr.Http.REST protected ResourceValidator PostValidator { get; private set; } protected ResourceValidator PutValidator { get; private set; } protected ResourceValidator SharedValidator { get; private set; } + private ResourceValidator IdValidator { get; set; } protected void ValidateId(int id) { @@ -42,8 +43,9 @@ namespace Sonarr.Http.REST PostValidator = new ResourceValidator(); PutValidator = new ResourceValidator(); SharedValidator = new ResourceValidator(); + IdValidator = new ResourceValidator(); - PutValidator.RuleFor(r => r.Id).ValidId(); + IdValidator.RuleFor(r => r.Id).ValidId(); } [RestGetById] @@ -61,7 +63,10 @@ namespace Sonarr.Http.REST } #nullable enable - protected abstract TResource? GetResourceById(int id); + protected virtual TResource? GetResourceById(int id) + { + throw new NotImplementedException(); + } #nullable disable public override void OnActionExecuting(ActionExecutingContext context) @@ -72,6 +77,11 @@ namespace Sonarr.Http.REST var skipValidate = skipAttribute?.Skip ?? false; var skipShared = skipAttribute?.SkipShared ?? false; + var attributes = descriptor.MethodInfo.CustomAttributes as IReadOnlyCollection ?? + descriptor.MethodInfo.CustomAttributes.ToArray(); + + var validateId = attributes.Any(x => VALIDATE_ID_ATTRIBUTES.Contains(x.AttributeType)); + if (Request.Method is "POST" or "PUT") { var resourceArgs = context.ActionArguments.Values @@ -90,13 +100,11 @@ namespace Sonarr.Http.REST resource.Id = Convert.ToInt32(routeId); } - ValidateResource(resource, skipValidate, skipShared); + ValidateResource(resource, validateId, skipValidate, skipShared); } } - var attributes = descriptor.MethodInfo.CustomAttributes as IReadOnlyCollection ?? - descriptor.MethodInfo.CustomAttributes.ToArray(); - if (attributes.Any(x => VALIDATE_ID_ATTRIBUTES.Contains(x.AttributeType)) && !skipValidate) + if (validateId && !skipValidate) { if (context.ActionArguments.TryGetValue("id", out var idObj)) { @@ -114,7 +122,7 @@ namespace Sonarr.Http.REST base.OnActionExecuting(context); } - protected void ValidateResource(TResource resource, bool skipValidate = false, bool skipSharedValidate = false) + protected void ValidateResource(TResource resource, bool validateId = false, bool skipValidate = false, bool skipSharedValidate = false) { if (resource == null) { @@ -135,6 +143,11 @@ namespace Sonarr.Http.REST else if (Request.Method.Equals("PUT", StringComparison.InvariantCultureIgnoreCase)) { errors.AddRange(PutValidator.Validate(resource).Errors); + + if (validateId) + { + errors.AddRange(IdValidator.Validate(resource).Errors); + } } if (errors.Any())