From 90ddd4ace875db4eabf764d4c692698071ee12b8 Mon Sep 17 00:00:00 2001 From: Antoine Aubry Date: Tue, 2 Oct 2018 22:54:49 +0100 Subject: [PATCH] Allow to specify the maximum recusion limit --- YamlDotNet/Serialization/SerializerBuilder.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/YamlDotNet/Serialization/SerializerBuilder.cs b/YamlDotNet/Serialization/SerializerBuilder.cs index 792293224..831050839 100644 --- a/YamlDotNet/Serialization/SerializerBuilder.cs +++ b/YamlDotNet/Serialization/SerializerBuilder.cs @@ -45,6 +45,7 @@ public sealed class SerializerBuilder : BuilderSkeleton private readonly LazyComponentRegistrationList> emissionPhaseObjectGraphVisitorFactories; private readonly LazyComponentRegistrationList eventEmitterFactories; private readonly IDictionary tagMappings = new Dictionary(); + private int maximumRecursion = 50; public SerializerBuilder() { @@ -69,13 +70,27 @@ public SerializerBuilder() eventEmitterFactories = new LazyComponentRegistrationList(); eventEmitterFactories.Add(typeof(TypeAssigningEventEmitter), inner => new TypeAssigningEventEmitter(inner, false, tagMappings)); - objectGraphTraversalStrategyFactory = (typeInspector, typeResolver, typeConverters) => new FullObjectGraphTraversalStrategy(typeInspector, typeResolver, 50, namingConvention ?? new NullNamingConvention()); + objectGraphTraversalStrategyFactory = (typeInspector, typeResolver, typeConverters) => new FullObjectGraphTraversalStrategy(typeInspector, typeResolver, maximumRecursion, namingConvention ?? new NullNamingConvention()); WithTypeResolver(new DynamicTypeResolver()); } protected override SerializerBuilder Self { get { return this; } } + /// + /// Sets the maximum recursion that is allowed while traversing the object graph. The default value is 50. + /// + public SerializerBuilder WithMaximumRecursion(int maximumRecursion) + { + if (maximumRecursion <= 0) + { + throw new ArgumentOutOfRangeException(nameof(maximumRecursion), $"The maximum recursion specified ({maximumRecursion}) is invalid. It should be a positive integer."); + } + + this.maximumRecursion = maximumRecursion; + return this; + } + /// /// Registers an additional to be used by the serializer. /// @@ -211,7 +226,7 @@ public SerializerBuilder EnsureRoundtrip() typeConverters, typeInspector, typeResolver, - 50 + maximumRecursion ); WithEventEmitter(inner => new TypeAssigningEventEmitter(inner, true, tagMappings), loc => loc.InsteadOf()); return WithTypeInspector(inner => new ReadableAndWritablePropertiesTypeInspector(inner), loc => loc.OnBottom());