From a5a2199820d69971b88b09218b5c74681da57da9 Mon Sep 17 00:00:00 2001 From: 257Byte <257byte@gmail.com> Date: Fri, 12 Jul 2024 14:59:32 +0300 Subject: [PATCH] Minor API validation fixes --- Tzkt.Api/Controllers/OperationsController.cs | 8 ++++---- Tzkt.Api/Parameters/SrCommitmentFilter.cs | 2 +- Tzkt.Api/Parameters/SrGameFilter.cs | 6 +++--- Tzkt.Api/Parameters/SrGameInfoFilter.cs | 4 ++-- Tzkt.Api/Validation/Min64Attribute.cs | 19 +++++++++++++++++++ 5 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 Tzkt.Api/Validation/Min64Attribute.cs diff --git a/Tzkt.Api/Controllers/OperationsController.cs b/Tzkt.Api/Controllers/OperationsController.cs index f2366d108..4276ef53b 100644 --- a/Tzkt.Api/Controllers/OperationsController.cs +++ b/Tzkt.Api/Controllers/OperationsController.cs @@ -5196,7 +5196,7 @@ public async Task>> GetMigrations( /// [HttpGet("migrations/{id:long}")] public async Task> GetMigrationById( - [Required][Min(0)] long id, + [Required][Min64(0)] long id, MichelineFormat micheline = MichelineFormat.Json, Symbols quote = Symbols.None) { @@ -5333,7 +5333,7 @@ public async Task>> GetReve /// [HttpGet("revelation_penalties/{id:long}")] public async Task> GetRevelationPenaltyById( - [Required][Min(0)] long id, + [Required][Min64(0)] long id, Symbols quote = Symbols.None) { var query = ResponseCacheService.BuildKey(Request.Path.Value, @@ -5500,7 +5500,7 @@ public async Task>> GetBaking( /// [HttpGet("baking/{id:long}")] public async Task> GetBakingById( - [Required][Min(0)] long id, + [Required][Min64(0)] long id, Symbols quote = Symbols.None) { var query = ResponseCacheService.BuildKey(Request.Path.Value, @@ -5636,7 +5636,7 @@ public async Task>> GetEndors /// [HttpGet("endorsing_rewards/{id:long}")] public async Task> GetEndorsingRewardById( - [Required][Min(0)] long id, + [Required][Min64(0)] long id, Symbols quote = Symbols.None) { var query = ResponseCacheService.BuildKey(Request.Path.Value, diff --git a/Tzkt.Api/Parameters/SrCommitmentFilter.cs b/Tzkt.Api/Parameters/SrCommitmentFilter.cs index 5bc702da2..ddde61c00 100644 --- a/Tzkt.Api/Parameters/SrCommitmentFilter.cs +++ b/Tzkt.Api/Parameters/SrCommitmentFilter.cs @@ -83,7 +83,7 @@ public class SrCommitmentFilter : INormalizable lastLevel == null && lastTime == null && status == null && - predecessor.Empty; + (predecessor == null || predecessor.Empty); public string Normalize(string name) { diff --git a/Tzkt.Api/Parameters/SrGameFilter.cs b/Tzkt.Api/Parameters/SrGameFilter.cs index b585cbae2..8765537fa 100644 --- a/Tzkt.Api/Parameters/SrGameFilter.cs +++ b/Tzkt.Api/Parameters/SrGameFilter.cs @@ -64,13 +64,13 @@ public class SrGameFilter : INormalizable public TimestampParameter lastTime { get; set; } [OpenApiIgnore] - public bool Empty => + public bool Empty => id == null && rollup == null && initiator == null && - initiatorCommitment.Empty && + (initiatorCommitment == null || initiatorCommitment.Empty) && opponent == null && - opponentCommitment.Empty && + (opponentCommitment == null || opponentCommitment.Empty) && firstLevel == null && firstTime == null && lastLevel == null && diff --git a/Tzkt.Api/Parameters/SrGameInfoFilter.cs b/Tzkt.Api/Parameters/SrGameInfoFilter.cs index df997df6a..d1860953c 100644 --- a/Tzkt.Api/Parameters/SrGameInfoFilter.cs +++ b/Tzkt.Api/Parameters/SrGameInfoFilter.cs @@ -37,9 +37,9 @@ public class SrGameInfoFilter : INormalizable public bool Empty => id == null && initiator == null && - initiatorCommitment.Empty && + (initiatorCommitment == null || initiatorCommitment.Empty) && opponent == null && - opponentCommitment.Empty; + (opponentCommitment == null || opponentCommitment.Empty); public string Normalize(string name) { diff --git a/Tzkt.Api/Validation/Min64Attribute.cs b/Tzkt.Api/Validation/Min64Attribute.cs new file mode 100644 index 000000000..f02052212 --- /dev/null +++ b/Tzkt.Api/Validation/Min64Attribute.cs @@ -0,0 +1,19 @@ +namespace System.ComponentModel.DataAnnotations +{ + public sealed class Min64Attribute : ValidationAttribute + { + readonly long Minimum; + + public Min64Attribute(long minimum) + { + Minimum = minimum; + } + + protected override ValidationResult IsValid(object value, ValidationContext validationContext) + { + return value != null && (long)value < Minimum + ? new ValidationResult($"The value must be greater than or equal to {Minimum}.") + : ValidationResult.Success; + } + } +}