Skip to content

Commit 474944d

Browse files
committed
Streamlined rule loading code
1 parent 2e7c33a commit 474944d

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -118,29 +118,23 @@ IEnumerator IEnumerable.GetEnumerator()
118118
private static ValidationRuleSet BuildDefaultRuleSet()
119119
{
120120
ValidationRuleSet ruleSet = new ValidationRuleSet();
121-
122-
IEnumerable<Type> allTypes = typeof(ValidationRuleSet).Assembly.GetTypes().Where(t => t.IsClass && t != typeof(object));
123121
Type validationRuleType = typeof(ValidationRule);
124-
foreach (Type type in allTypes)
125-
{
126-
if (!type.GetCustomAttributes(typeof(OpenApiRuleAttribute), false).Any())
127-
{
128-
continue;
129-
}
130122

131-
var properties = type.GetProperties(BindingFlags.Static | BindingFlags.Public);
132-
foreach (var property in properties)
133-
{
134-
if (validationRuleType.IsAssignableFrom(property.PropertyType))
123+
IEnumerable<PropertyInfo> rules = typeof(ValidationRuleSet).Assembly.GetTypes()
124+
.Where(t => t.IsClass
125+
&& t != typeof(object)
126+
&& t.GetCustomAttributes(typeof(OpenApiRuleAttribute), false).Any())
127+
.SelectMany(t2 => t2.GetProperties(BindingFlags.Static | BindingFlags.Public)
128+
.Where(p => validationRuleType.IsAssignableFrom(p.PropertyType)));
129+
130+
foreach (var property in rules)
131+
{
132+
var propertyValue = property.GetValue(null); // static property
133+
ValidationRule rule = propertyValue as ValidationRule;
134+
if (rule != null)
135135
{
136-
var propertyValue = property.GetValue(null); // static property
137-
ValidationRule rule = propertyValue as ValidationRule;
138-
if (rule != null)
139-
{
140-
ruleSet.Add(rule);
141-
}
136+
ruleSet.Add(rule);
142137
}
143-
}
144138
}
145139

146140
return ruleSet;

0 commit comments

Comments
 (0)