Skip to content

Commit

Permalink
Merge pull request #353 from aaubry/issue-352-max-recursion
Browse files Browse the repository at this point in the history
Allow to specify the maximum recusion limit
  • Loading branch information
aaubry authored Oct 3, 2018
2 parents 34215af + 90ddd4a commit 4bee452
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions YamlDotNet/Serialization/SerializerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public sealed class SerializerBuilder : BuilderSkeleton<SerializerBuilder>
private readonly LazyComponentRegistrationList<EmissionPhaseObjectGraphVisitorArgs, IObjectGraphVisitor<IEmitter>> emissionPhaseObjectGraphVisitorFactories;
private readonly LazyComponentRegistrationList<IEventEmitter, IEventEmitter> eventEmitterFactories;
private readonly IDictionary<Type, string> tagMappings = new Dictionary<Type, string>();
private int maximumRecursion = 50;

public SerializerBuilder()
{
Expand All @@ -69,13 +70,27 @@ public SerializerBuilder()
eventEmitterFactories = new LazyComponentRegistrationList<IEventEmitter, IEventEmitter>();
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; } }

/// <summary>
/// Sets the maximum recursion that is allowed while traversing the object graph. The default value is 50.
/// <summary>
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;
}

/// <summary>
/// Registers an additional <see cref="IEventEmitter" /> to be used by the serializer.
/// </summary>
Expand Down Expand Up @@ -211,7 +226,7 @@ public SerializerBuilder EnsureRoundtrip()
typeConverters,
typeInspector,
typeResolver,
50
maximumRecursion
);
WithEventEmitter(inner => new TypeAssigningEventEmitter(inner, true, tagMappings), loc => loc.InsteadOf<TypeAssigningEventEmitter>());
return WithTypeInspector(inner => new ReadableAndWritablePropertiesTypeInspector(inner), loc => loc.OnBottom());
Expand Down

0 comments on commit 4bee452

Please sign in to comment.