Feat/topological sort defs#242
Draft
vanshaj2023 wants to merge 2 commits into
Draft
Conversation
Contributor
|
Hey @vanshaj2023, since this falls under the GSoC qualification tasks, I believe it's intended to be part of the proposal rather than a PR—as mentioned in the task description. Opening a PR at this stage might create some unnecessary overhead for the maintainers. Thanks! |
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 topological sorting for
$defsrendering order insortAST.ts, and fixes an existing bug where modern$defsschemas received no sorting benefit at all.Bug fixed:
sortAST.tsonly definedDEF_KEY(legacydefinitionskeyword). The modern$defskey (DEFS_KEY) was never referenced, meaning every schema using$defsgot zero sorting benefit from the existing sort.Problem: When
$defscontains definitions that reference each other via$ref, rendering them out of dependency order produces spurious back-edges and a confusing graph layout. For example, ifpersonreferencesaddressbut appears first in$defs,addressgets rendered as a child ofpersonand then again as a top-level definition.Solution: Kahn's algorithm (BFS-based topological sort) — scans each definition's AST subtree for
$refpointers to sibling definitions, builds a dependency graph, and renders dependencies before dependents. Cycles are handled by appending remaining nodes in their original order —processASTalready handles cycles safely via therenderedNodesMap.What kind of change does this PR introduce
Bug fix + enhancement — fixes missing
DEFS_KEYand adds topological sort for$defsrendering orderIssue Number
Closes #
Screenshots/Video
Before —
personrenders first,addressbecomes a child node then re-appears as top-level (spurious back-edge):After —
country → address → personrender in dependency order, clean left-to-right layout:Schema used for testing:
{ "$schema": "https://json-schema.org/draft/2020-12/schema", "$defs": { "person": { "type": "object", "properties": { "address": { "$ref": "#/$defs/address" } } }, "address": { "type": "object", "properties": { "country": { "$ref": "#/$defs/country" } } }, "country": { "type": "object", "properties": { "name": { "type": "string" } } } } }Does this PR introduce a breaking change?
No
If relevant, did you update the documentation?
No