Repository navigation
fix: handle circular references in diff command (fixes #2093) - #2113
armorbreak001 wants to merge 1 commit into
Conversation
|
The diff command crashed with TypeError when documents contained circular references (e.g., schemas referencing themselves via anyOf + $ref cycles). Added safeJson() helper that traverses the document graph and replaces circular refs with [Circular] placeholders, preventing JSON.stringify from throwing. - Add safeJson() and safeValue() internal helpers - Wrap .json() calls in diff command with safeJson() - Add 5 unit tests for circular reference handling Fixes asyncapi#2093
|
|
This pull request has been automatically marked as stale because it has not had recent activity 😴 It will be closed in 120 days if no further activity occurs. To unstale this pull request, add a comment with detailed explanation. There can be many reasons why some specific pull request has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model. Let us figure out together how to push this pull request forward. Connect with us through one of many communication channels we established here. Thank you for your patience ❤️ |



Description
Closes #2093
The diff command crashed with
TypeError: Converting circular structure to JSONwhen AsyncAPI documents contained circular references (e.g., schemas referencing themselves viaanyOf+$refcycles).Root Cause
When parsed documents contain circular object references, calling
.json()and passing the result todiff.diff()eventually triggersJSON.stringify()on the circular structure, which throws a TypeError.Solution
Added internal helper functions
safeJson()andsafeValue()that:WeakSetto detect already-visited objects[Circular]placeholder stringChanges
src/apps/cli/commands/diff.ts
safeJson()/safeValue()helpers (~30 lines).json()calls indiff.diff()withsafeJson()test/unit/commands/diff-circular.test.ts (new)
Test Reproduction
```yaml
circular.yml
asyncapi: 3.1.0
info:
title: Testing
version: 1.0.0
components:
schemas:
filters:
type: array
items:
anyOf:
- $ref: "#/components/schemas/filters"
- type: string
entity-created:
type: object
properties:
filters:
$ref: "#/components/schemas/filters"
```
Before:
TypeError: Converting circular structure to JSONAfter: Diff completes successfully with
[Circular]placeholders in output