Skip to content

Commit

Permalink
fix startup-validation not terminating at circular nested type depend…
Browse files Browse the repository at this point in the history
…encies
  • Loading branch information
ntruchsess committed Sep 26, 2024
1 parent cf40bb6 commit 535e26e
Showing 1 changed file with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,11 +30,12 @@ namespace Org.Eclipse.TractusX.Portal.Backend.Framework.Models.Validation;

public abstract class SharedBaseOptionEnumerableValidation
{
protected static readonly ImmutableArray<Type> IgnoreTypes = ImmutableArray.Create(
protected static readonly ImmutableArray<Type> IgnoreTypes = [
typeof(string),
typeof(bool),
typeof(decimal),
typeof(Encoding));
typeof(Encoding)
];
}

public abstract class BaseOptionEnumerableValidation<TOptions> : SharedBaseOptionEnumerableValidation, IValidateOptions<TOptions> where TOptions : class
Expand Down Expand Up @@ -83,8 +83,15 @@ private IEnumerable<ValidationResult> 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<ValidationResult> 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<ValidationResult> ValidateAttribute(IConfiguration config, PropertyInfo property, string propertyName);

Expand All @@ -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<ValidationResult>(),
_ => Enumerable.Empty<ValidationResult>()
_ => []
};
}

0 comments on commit 535e26e

Please sign in to comment.