Implement serialization context for recursive references#37
Merged
IvanMurzak merged 5 commits intomainfrom Dec 10, 2025
Merged
Conversation
Contributor
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a serialization context mechanism to detect and handle recursive references and circular dependencies during object serialization. The implementation adds a SerializationContext class that tracks visited objects and their paths, allowing the serializer to generate JSON references instead of infinitely recursing.
Key Changes:
- Added
SerializationContextclass for tracking object visitation and paths during serialization - Updated
Reflector.Serializeto use the context with proper Enter/Exit lifecycle management - Extended all convertor interfaces and implementations to thread the context parameter through the call chain
- Added comprehensive test coverage for various recursion scenarios
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
ReflectorNet/src/Model/SerializationContext.cs |
New class implementing object visitation tracking with reference path management using a stack-based approach |
ReflectorNet/src/Reflector/Reflector.cs |
Updated Serialize method to initialize context and handle recursive cycles with try-finally pattern |
ReflectorNet/src/Model/SerializedMember.cs |
Added FromReference factory method to create reference members for cyclic objects |
ReflectorNet/src/Utils/Json/JsonSchema.Consts.cs |
Added Reference constant for JSON schema reference type |
ReflectorNet/src/Convertor/IReflectionConvertor.cs |
Added context parameter to Serialize method signature (formatting improvements) |
ReflectorNet/src/Convertor/Reflection/Base/BaseReflectionConvertor.Serialize.cs |
Updated to propagate context through serialization methods |
ReflectorNet/src/Convertor/Reflection/GenericReflectionConvertor.cs |
Updated to pass context when serializing fields and properties |
ReflectorNet/src/Convertor/Reflection/ArrayReflectionConvertor.cs |
Updated to pass context when serializing array elements |
ReflectorNet/src/Convertor/Reflection/PrimitiveReflectionConvertor.cs |
Added context parameter to signature (not used for primitives) |
ReflectorNet/src/Convertor/Reflection/Specialized/TypeReflectionConvertor.cs |
Updated to pass context to base serialization |
ReflectorNet/src/Convertor/Reflection/Specialized/AssemblyReflectionConvertor.cs |
Updated to pass context to base serialization |
ReflectorNet.Tests/src/RecursionTests.cs |
Comprehensive test suite covering self-recursion, mutual recursion, and deep recursion scenarios with both objects and lists |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request adds comprehensive recursion and cycle-handling tests to the ReflectorNet serialization and deserialization logic, and refactors the reflection converter interfaces and implementations to support context-aware (cycle-safe) serialization and deserialization. The changes ensure that recursive object graphs, including cycles and self-references, are correctly serialized with references and accurately restored during deserialization. The interfaces are updated for improved extensibility and clarity.
Recursion and Cycle Handling Tests
RecursionTests.csto verify that serialization and deserialization correctly handle recursive structures, cycles, and references for various object graphs, including self-references and deep recursion in lists and objects.Interface and API Refactoring
IReflectionConvertorinterface to includeSerializationContextandDeserializationContextparameters for both serialization and deserialization methods, supporting context-aware (cycle-safe) processing. Also improved method signatures for readability and extensibility. [1] [2] [3]Implementation Updates for Cycle Safety
ArrayReflectionConvertordeserialization logic to register arrays/lists early in the deserialization context, allowing child references to resolve cycles. Array element names are now set with their indices for accurate reference path tracking. [1] [2] [3] [4]