You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I believe @stoplight/json-ref-resolver can only reliably handle circular references, if the cycle includes the top-level definition in the file you are trying to resolve.
In the example PR, if CommunicationErrorResponse is in a different JSON file than CommunicationError (which has a circular reference to CommunicationError), the cycle can be detected immediately. So instead of running forever, it completes on the spec in 3 seconds.
But if CommunicationErrorResponse and CommunicationError are siblings in the same JSON file, json-ref-resolver falls into a different codepath where it can't detect the cycle.
This example doesn't hang, because the cycle is ErrorResponse->ErrorResponse, and ErrorResponse is the top-level object under definitions we are resolving:
However, this example should hang when trying to resolve ErrorResponse, since the cycle is ErrorResponse->Error->Error..., but since the cycle doesn't go through ErrorResponse, it isn't detected by json-ref-resolver.
The text was updated successfully, but these errors were encountered:
mikeharder
changed the title
Infinite loop if circular reference doesn't start with a top-level definition
Infinite loop if circular reference doesn't start with top-level definition
Mar 21, 2025
In this PR, LintDiff was hanging because
@stoplight/json-ref-resolver
was in an infinite loop of circular references.Azure/azure-rest-api-specs#32573 (comment)
I believe
@stoplight/json-ref-resolver
can only reliably handle circular references, if the cycle includes the top-level definition in the file you are trying to resolve.https://github.com/stoplightio/json-ref-resolver/blob/e4e2410e86edcc7a96e01d309039ad319ca40f2c/src/crawler.ts#L115-L118
In the example PR, if
CommunicationErrorResponse
is in a different JSON file thanCommunicationError
(which has a circular reference toCommunicationError
), the cycle can be detected immediately. So instead of running forever, it completes on the spec in 3 seconds.But if
CommunicationErrorResponse
andCommunicationError
are siblings in the same JSON file,json-ref-resolver
falls into a different codepath where it can't detect the cycle.This example doesn't hang, because the cycle is
ErrorResponse
->ErrorResponse
, andErrorResponse
is the top-level object under definitions we are resolving:azure-openapi-validator/packages/rulesets/src/native/tests/resources/references/circular-ref.json
Lines 15 to 25 in 61cc571
However, this example should hang when trying to resolve
ErrorResponse
, since the cycle isErrorResponse->Error->Error...
, but since the cycle doesn't go throughErrorResponse
, it isn't detected byjson-ref-resolver
.The text was updated successfully, but these errors were encountered: