feat: add support for recursive type schema generation#28
Merged
panesofglass merged 2 commits intomasterfrom Feb 7, 2026
Merged
feat: add support for recursive type schema generation#28panesofglass merged 2 commits intomasterfrom
panesofglass merged 2 commits intomasterfrom
Conversation
Fixed critical bug where nullable self-references (Ref "#") were not properly translated. When a type has a field with nullable self-reference (e.g., LinkedNode with Next: LinkedNode option), the translator was trying to look up "#" in the definitions dictionary instead of referencing rootSchema directly. Added special case handling for SchemaNode.Ref "#" in both translateProp and translateNode functions' Nullable match branches. This allows schemas like LinkedNode to correctly serialize to JSON with proper self-reference structure. All 573 tests now pass across all frameworks: - Core tests: 51 × 3 frameworks - Main tests: 128 × 3 frameworks - OpenApi tests: 18 × 2 frameworks Fixes GitHub issue #15 for self-recursive type support. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
Implements support for generating JSON schemas from self-recursive F# types, resolving GitHub issue #15. This adds the ability to handle discriminated unions, records, and complex recursive structures that reference themselves.
Key Changes
Bug Fix: NJsonSchemaTranslator Self-Reference Handling
LinkedNodewithNext: LinkedNode option)"#"in the definitions dictionary instead of referencingrootSchemaSchemaNode.Ref "#"match cases in bothtranslatePropandtranslateNodeNullable branchesImplementation Details
src/FSharp.Data.JsonSchema.NJsonSchema/NJsonSchemaTranslator.fstranslateProp(lines 107-119)translateNode(lines 180-187)Test Coverage
✅ All 573 tests passing across all frameworks:
Recursive Type Examples
Self-Recursive DU:
Generated schema uses
$ref: "#"for circular references in Branch cases.Self-Recursive Record with Optional:
Generated schema correctly handles nullable self-reference as
oneOf: [null, $ref: "#"].Recursion Through Collections:
Generated schema allows array items to reference root via
$ref: "#".Multi-Case Recursive DU:
Generated schema handles multiple recursive cases with proper definition references.
Verified Snapshots
GeneratorTests.Self-recursive DU generates proper schema.verified.txtGeneratorTests.Self-recursive record generates proper schema.verified.txtGeneratorTests.Recursion through collection generates proper schema.verified.txtGeneratorTests.Multi-case self-recursive DU generates proper schema.verified.txtTesting Strategy
The implementation passes all existing tests plus new comprehensive test coverage:
Test Plan
🤖 Generated with Claude Code