Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to specify the maximum recusion limit #353

Merged
merged 1 commit into from
Oct 3, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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