From bc0cebb104b0c90708232d87378e3e6e7c33ab86 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 6 Oct 2023 21:24:13 +1100 Subject: [PATCH 1/2] remove duplicate dictionary lookups in ValidationRuleSet.Add --- src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs b/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs index 6cc4d3a05..8a641da4e 100644 --- a/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs +++ b/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs @@ -123,12 +123,13 @@ public IList Rules /// The rule. public void Add(ValidationRule rule) { - if (!_rules.ContainsKey(rule.ElementType)) + if (!_rules.TryGetValue(rule.ElementType, out var item)) { - _rules[rule.ElementType] = new List(); + _rules[rule.ElementType] = new List {rule}; + return; } - if (_rules[rule.ElementType].Contains(rule)) + if (item.Contains(rule)) { throw new OpenApiException(SRResource.Validation_RuleAddTwice); } From 88bb6b995cd379cc70c17517338635d510525844 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 6 Oct 2023 21:25:30 +1100 Subject: [PATCH 2/2] Update ValidationRuleSet.cs --- src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs b/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs index 8a641da4e..f6bb84806 100644 --- a/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs +++ b/src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs @@ -134,7 +134,7 @@ public void Add(ValidationRule rule) throw new OpenApiException(SRResource.Validation_RuleAddTwice); } - _rules[rule.ElementType].Add(rule); + item.Add(rule); } ///