Skip to content

Implement serialization context for recursive references#37

Merged
IvanMurzak merged 5 commits intomainfrom
fix/recursive-reference
Dec 10, 2025
Merged

Implement serialization context for recursive references#37
IvanMurzak merged 5 commits intomainfrom
fix/recursive-reference

Conversation

@IvanMurzak
Copy link
Copy Markdown
Owner

@IvanMurzak IvanMurzak commented Dec 10, 2025

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

  • Added extensive unit tests in RecursionTests.cs to 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

  • Refactored IReflectionConvertor interface to include SerializationContext and DeserializationContext parameters 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

  • Updated ArrayReflectionConvertor deserialization 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]

@IvanMurzak IvanMurzak self-assigned this Dec 10, 2025
@IvanMurzak IvanMurzak requested a review from Copilot December 10, 2025 07:42
@IvanMurzak IvanMurzak added the bug Something isn't working label Dec 10, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Dec 10, 2025

Test Results

    3 files  ± 0      3 suites  ±0   3m 27s ⏱️ +2s
  604 tests + 9    604 ✅ + 9  0 💤 ±0  0 ❌ ±0 
1 774 runs  +27  1 774 ✅ +27  0 💤 ±0  0 ❌ ±0 

Results for commit 89df827. ± Comparison against base commit 4e71e4c.

♻️ This comment has been updated with latest results.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 SerializationContext class for tracking object visitation and paths during serialization
  • Updated Reflector.Serialize to 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.

Comment thread ReflectorNet.Tests/src/RecursionTests.cs Outdated
Comment thread ReflectorNet.Tests/src/RecursionTests.cs Outdated
Comment thread ReflectorNet/src/Model/SerializationContext.cs Outdated
Comment thread ReflectorNet/src/Model/SerializationContext.cs
Comment thread ReflectorNet/src/Model/SerializationContext.cs Outdated
Comment thread ReflectorNet.Tests/src/RecursionTests.cs Outdated
Comment thread ReflectorNet.Tests/src/RecursionTests.cs Outdated
Comment thread ReflectorNet.Tests/src/RecursionTests.cs
Comment thread ReflectorNet/src/Model/SerializationContext.cs Outdated
Comment thread ReflectorNet.Tests/src/RecursionTests.cs
@IvanMurzak IvanMurzak merged commit 356fbc2 into main Dec 10, 2025
2 checks passed
@IvanMurzak IvanMurzak deleted the fix/recursive-reference branch December 10, 2025 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants