-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
findSchemaDefinition doesn't detect cyclic references #4116
Comments
@MarttiR Good catch. We've dealt with cyclic $refs before by stopping once a recursion has been detected. There is many examples of this in the
|
Sure, I can do it. I think the appropriate fix is to throw an error when a cycle is detected, so that developers are alerted to the actual issue. |
@MarttiR Cyclic dependencies like this with $refs are actually valid. So rather than throwing, you'll want to stop recursing |
@heath-freenome Thanks! To me, it feels these definitions are degenerate, as they are self-referential. Could you give an example of a schema where such definitions would be valid, so that I can understand how to form the test cases? Edit: nevermind, I see there are some example cases in |
@heath-freenome It looks like we are talking about two different cases. It is indeed valid for a property schema to have child properties defined as a reference to itself. For example, a menu structure is a classic example of that pattern: - title: "First level menu item"
children:
- title: "Second level menu item"
- title: "Another second level menu item"
children:
- title: "Third level menu item" and so on. For such a thing, it makes sense to have a schema like {
"definitions": {
"MenuItem": {
"type": "object",
"properties": {
"title": {
"type": "string"
},
"children": {
"$ref": "#/definitions/MenuItem"
}
}
}
}
} However, the issue here is not with schema resolution, but definition resolution. The problematic case is a definition property that refers to itself either directly, or indirectly through a chain of other definitions. {
"definitions": {
"SelfReferential": {
"$ref": "#/definitions/SelfReferential"
}
}
} The crucial difference is that the references are not in child properties. Rather, the definition has become tautological, and is likely either a degenerate result of a bad schema transformation, or simply a user error. In my mind, my current PR is a minimal and sufficient fix for the issue: First, Secondly, |
@MarttiR Ah... You raise a good point, so it's only the immediate self-recursion that you have to solve for? Or does the nested circular reference also pose the same issue? |
@heath-freenome Only self-referential references (as in "ultimately ends up defining itself") are detected, as that's what causes the accidental baseless recursion here. The "nested" case (as in "defines its children using its own definition") fixed earlier in |
Prerequisites
What theme are you using?
core
Version
5.17.1
Current Behavior
Consider a schema that (accidentally!) has a self-referencing definition like so:
During validation,
findSchemaDefinition
enters a recursive loop until the runtime aborts withMaximum call stack size exceeded
(Chrome) orfunction nested too deeply
(Firefox).Expected Behavior
findSchemaDefinition
could detect that the "resolved" $ref is in fact the same $ref and throw, alerting developers to the issue in their schema.Steps To Reproduce
No response
Environment
No response
Anything else?
No response
The text was updated successfully, but these errors were encountered: