Skip to content

Treat framework types as opaque beyond depth 1 in object tree renderer - #88

Merged
TorreyBetts merged 2 commits into
DataficationSDK:mainfrom
JoeJoeflyn:fix/verso-007-framework-type-opacity
Jul 24, 2026
Merged

TorreyBetts merged 2 commits into
DataficationSDK:mainfrom
JoeJoeflyn:fix/verso-007-framework-type-opacity

Conversation

@JoeJoeflyn

Copy link
Copy Markdown
Contributor

Summary

Fixes VERSO-007 — object tree view producing oversized output for complex framework types.

  • Add IsFrameworkType helper to ObjectTreeRenderer (checks if a type's namespace is System, System.*, Microsoft, or Microsoft.*)
  • Render framework types via ToString() at depth 2+ instead of expanding their property graphs, cutting off the Type → Assembly → DefinedTypes explosion path that wasted the 512 KB output budget on reflection internals
  • Framework types at depth 0–1 still expand so users can inspect immediate properties; user types expand at all depths
  • Collections (IEnumerable) still expand at all depths — the collection check runs before the framework-type opacity check so nested List<string>, arrays, etc. are not hidden

Background

System.Type exposes ~40 public properties. One of them, Assembly, exposes DefinedTypes — potentially thousands of types, each with their own ~40-property graphs. Across 6 recursion levels this produced hundreds of MB of HTML, causing the 512 KB cap to truncate the user's actual data and, in worst cases, a System.ArgumentException: The JSON value of length N is too large crash during auto-save.

This implements the planned improvement from KNOWN-ISSUES.md:

Consider treating types from runtime assemblies (System., System.Reflection.) as opaque beyond depth 1, rendering them via .ToString() rather than expanding their full property graphs.

Note: This is a re-open of #87 (same commits + review feedback applied). The previous PR was accidentally auto-closed when the head branch was renamed from fix/verso-003-fsharp-anonymous-records to fix/verso-007-framework-type-opacity. The branch name initially referenced VERSO-003 because it was first created for a different issue (VERSO-003, which turned out to already be fixed in the current FSharp.Compiler.Service version) before being repurposed for VERSO-007.

Test plan

  • FormatAsync_FrameworkTypeAtDepth2_DoesNotExpandReflectionInternals — asserts DefinedTypes does not appear in output when a System.Type property is at depth 2
  • FormatAsync_FrameworkTypeAtDepth1_StillExpandable — asserts framework types at depth 1 still expand (not fully opaque)
  • FormatAsync_NestedCollectionAtDepth1_StillShowsItems — asserts nested collections still show their items at depth 1
  • FormatAsync_NestedCollectionAtDepth2_StillShowsItems — asserts nested collections still show their items at depth 2+
  • Full Verso.Tests suite: 1216 passed, 0 failed
  • Manual verification in verso serve browser editor

- Add IsFrameworkType helper (namespace starts with System or Microsoft)
- Render framework types via ToString() at depth 2+ instead of expanding
  their property graphs, cutting off the Type → Assembly → DefinedTypes
  explosion path that wasted the 512 KB output budget on reflection
  internals
- Framework types at depth 0-1 still expand so users can inspect
  immediate properties; user types expand at all depths
- Add tests: framework type at depth 2 does not expand reflection
  internals; framework type at depth 1 remains expandable

Fixes VERSO-007.

Signed-off-by: JoeJoeflyn <[email protected]>

@TorreyBetts TorreyBetts left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for the quick update, and for the clear explanation of the branch name. Both changes from the last review are applied well, and the new collection tests are good.

I found one new problem that came from moving the framework-type check. Please see the comment below. It is a one-line fix, but it needs to be fixed before we can merge. After that, this PR looks good.

Comment on lines +109 to +113
if (depth > 1 && IsFrameworkType(type))
{
sb.Append(WebUtility.HtmlEncode(value.ToString() ?? ""));
return;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Moving this check below the cycle check created a small new bug.

The cycle check above (line 86) adds the object to seen. The other branches remove it again before they return (see lines 99, 119, and 124). But this branch returns without removing it. So the object stays in seen forever.

This is a problem because some framework objects are shared. For example, typeof(double) is always the same object in memory. When two properties hold the same Type object, the first one is shown correctly, but the second one shows [circular reference]. That is wrong output.

Example: a list of columns where every column has DataType = typeof(double). The first column shows System.Double. All the other columns show [circular reference].

The fix is one line:

Suggested change
if (depth > 1 && IsFrameworkType(type))
{
sb.Append(WebUtility.HtmlEncode(value.ToString() ?? ""));
return;
}
if (depth > 1 && IsFrameworkType(type))
{
if (!type.IsValueType) seen.Remove(value);
sb.Append(WebUtility.HtmlEncode(value.ToString() ?? ""));
return;
}

Could you also add a small test for this? For example: an object with two properties that both hold typeof(int). The output should show System.Int32 two times, and no [circular reference].

… shared types

- Move IEnumerable check before the framework-type opacity check so
  nested collections (List<string>, arrays, etc.) still expand at all
  depths instead of being hidden by IsFrameworkType
- Match exact namespaces (System / System.* / Microsoft / Microsoft.*)
  instead of StartsWith("System") which also matched user namespaces
  like Systematic or SystemTools
- Move the framework-type opacity check before cycle detection so shared
  framework instances (e.g. typeof(int) is always the same object in
  memory) render via ToString() instead of triggering false
  [circular reference] output when they appear in sibling properties
- Add tests: nested collections at depth 1 and 2 still show their items;
  two properties holding typeof(int) both render without circular refs

Signed-off-by: JoeJoeflyn <[email protected]>
@JoeJoeflyn
JoeJoeflyn force-pushed the fix/verso-007-framework-type-opacity branch from 5e7fc0f to 38c3641 Compare July 24, 2026 16:37
@JoeJoeflyn

Copy link
Copy Markdown
Contributor Author

Thanks for catching this. Your suggested seen.Remove(value) line alone wasn't enough — the cycle check at line 86 runs before the opacity branch, so for a shared instance like typeof(int) (always the same object), seen.Add returns false and it returns [circular reference] without ever reaching the opacity branch. The fix was to reorder: run the framework-type opacity check before cycle detection, so shared instances render via ToString() without ever entering seen. Collections got their own inline cycle check since they were moved above the shared block. Added test FormatAsync_SharedFrameworkTypeInstance_NoCircularReference — two properties holding typeof(int), no circular ref. 1217 passed.

@TorreyBetts

Copy link
Copy Markdown
Contributor

Verified locally, all 1217 tests pass, and you were right that the reorder was needed instead of my one line idea. Thank you.

@TorreyBetts
TorreyBetts merged commit 8942a31 into DataficationSDK:main Jul 24, 2026
6 checks passed
@JoeJoeflyn

Copy link
Copy Markdown
Contributor Author

Happy to help! Glad it worked out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants