From 535e26e333ed3f07ee2f69e1d824a634a25bbb4b Mon Sep 17 00:00:00 2001 From: Norbert Truchsess Date: Thu, 26 Sep 2024 19:45:58 +0200 Subject: [PATCH] fix startup-validation not terminating at circular nested type dependencies --- .../BaseOptionEnumerableValidation.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/framework/Framework.Models/Validation/BaseOptionEnumerableValidation.cs b/src/framework/Framework.Models/Validation/BaseOptionEnumerableValidation.cs index d01d16152e..d401714fa9 100644 --- a/src/framework/Framework.Models/Validation/BaseOptionEnumerableValidation.cs +++ b/src/framework/Framework.Models/Validation/BaseOptionEnumerableValidation.cs @@ -1,5 +1,4 @@ /******************************************************************************** - * Copyright (c) 2023 BMW Group AG * Copyright (c) 2023 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional @@ -31,11 +30,12 @@ namespace Org.Eclipse.TractusX.Portal.Backend.Framework.Models.Validation; public abstract class SharedBaseOptionEnumerableValidation { - protected static readonly ImmutableArray IgnoreTypes = ImmutableArray.Create( + protected static readonly ImmutableArray IgnoreTypes = [ typeof(string), typeof(bool), typeof(decimal), - typeof(Encoding)); + typeof(Encoding) + ]; } public abstract class BaseOptionEnumerableValidation : SharedBaseOptionEnumerableValidation, IValidateOptions where TOptions : class @@ -83,8 +83,15 @@ private IEnumerable GetValidationErrors(Type type, IConfigurat .ExceptBy(IgnoreTypes, prop => prop.PropertyType) .Where(prop => !prop.PropertyType.IsEnum) .SelectMany(property => - ValidateAttribute(configSection, property, property.Name) - .Concat(CheckPropertiesOfProperty(configSection, property, property.Name))); + ValidateAttributeAndDescendants(configSection, property, property.Name)); + + private IEnumerable ValidateAttributeAndDescendants(IConfiguration config, PropertyInfo property, string propertyName) + { + var result = ValidateAttribute(config, property, propertyName); + return config.GetSection(propertyName).Get(property.PropertyType) == null + ? result + : result.Concat(CheckPropertiesOfProperty(config, property, propertyName)); + } protected abstract IEnumerable ValidateAttribute(IConfiguration config, PropertyInfo property, string propertyName); @@ -101,6 +108,6 @@ var x when x.GetInterfaces().Contains(typeof(IEnumerable)) && ?.ToIEnumerable() .Select((_, i) => configSection.GetSection($"{propertyName}:{i}")) .SelectMany(section => GetValidationErrors(genericType, section)) ?? Enumerable.Empty(), - _ => Enumerable.Empty() + _ => [] }; }