Repository navigation
Description
TL;DR;
Handling of circular refs in spec is broken and we should fix it.
Describe the bug
Currently our parser handle circular refs. In opinion the result is invalid:
-
we handle very simple cases, only
propertiesforobjecttype and we marked every "circular"itemsas circular, what is not a valid. Readme.md says:x-parser-circular property is added to every schema of array type that is circular. To check if schema is circular or not, you should use isCircular() function on a Schema model like document.components().schema('RecursiveSelf').properties()['selfChildren'].isCircular().
-
we don't handle circular refs in combined schemas (see Circular reference causes a "Template render error" error html-template#165),
-
we don't handle very complex cases (see Circular references causes stack overflow html-template#67) - it's probably not related to circular refs, but we should also support
$ref: #case 😄.
Why I have a problem with "circular" items? See the schema, which we use in tests:
RecursiveSelf:
type: object
properties:
selfChildren:
type: array
items:
$ref: '#/components/schemas/RecursiveSelf'
selfObjectChildren:
type: object
properties:
test:
$ref: '#/components/schemas/RecursiveSelf'
nonRecursive:
type: string
selfSomething:
type: object
properties:
test:
$ref: '#/components/schemas/RecursiveAncestor'
RecursiveAncestor:
type: object
properties:
ancestorChildren:
type: array
items:
$ref: '#/components/schemas/RecursiveSelf'
ancestorSomething:
type: stringIn parsing we mark RecursiveAncestor.properties.ancestorChildren as circular. This is due to the fact that when checking cyclic references in RecursiveAncestor we go to RecursiveSelf schema and we see RecursiveAncestor in RecursiveSelf.properties.selfSomething.properties.test. And this is correct only for RecursiveSelf schema, not for RecursiveAncestor! We save x-parser-circular extension to original reference of RecursiveAncestor not to copy of it, so by this non cyclic schema is marked as cyclic. Imagine that we render RecursiveAncestor and by isCircular() function we have information that RecursiveAncestor.properties.ancestorChildren.items is circular. It's true in RecursiveSelf point of view, not in RecursiveAncestor, because here we have relationship:
RecursiveAncestor -> RecursiveSelf -> RecursiveAncestor
so this is exactly circular reference in RecursiveAncestor case, which we should mark in some way. We must think in this problem like exactly circular references works in programming languages. If you will try log the RecursiveAncestor object you will see something like:
<ref *1> RecursiveAncestor
- properties
- ancestorChildren
- items <ref *2> RecursiveSelf
- properties
- selfChildren
- items <ref *2> RecursiveSelf
- selfObjectChildren
- properties
- test <ref *2> RecursiveSelf
- selfSomething
- properties
- test <ref *1> RecursiveAncestorso you have circular references in second deep, not in first as works it currently.
Also mark schemas if items is "circular" (which don't have to be a circular as mentioned above) is not a good idea in the other hand. items can be also described as array (tuple type) of schemas. So having schema like:
RecursiveItems:
items:
- type: string
- $ref: '#/components/schemas/RecursiveItems'as developer, I should know that exactly 1 index of items is circular, not whole schema :)
Using JSON Schema 07-draft (and upper) we can also write this beast:
RecursiveComplex:
type: [object, array]
patternProperties:
^foo:
$ref: '#/components/schemas/RecursiveSelf'
^bar:
type: string
contains:
$ref: '#/components/schemas/RecursiveComplex'
items:
- type: string
- $ref: '#/components/schemas/RecursiveComplex'
if:
$ref: '#/components/schemas/RecursiveAncestor'
then:
$ref: '#/components/schemas/RecursiveComplex'using another keywords reserved for object and array, so also x-parser-circular-props doesn't fully fulfill its task.
What is exactly circular here? :) I see:
RecursiveComplex -> ^foo -> RecursiveSelf -> selfChildren -> items // and another properties from RecursiveSelf
RecursiveComplex -> contains -> RecursiveComplex
RecursiveComplex -> items[1] -> RecursiveComplex
RecursiveComplex -> if -> RecursiveAncestor -> ancestorChildren -> items -> RecursiveSelf -> selfChildren -> items // ...etc
RecursiveComplex -> then -> RecursiveComplex
Some people can say: This is not a logical valid, we shouldn't handle this. If we add possibility (by JSON Schema) to developer to write schemas like this, we should also try handle them.
Task is not trivial and we should try to define how to mark circular refs in another way (yes, breaking change, because currently idea doesn't work as expected) to handle all the possible cases. Probably we should "create" copy of some parts of parsed spec and mark these parts as circular by x-parser-circular extension.
How to Reproduce
Just see the tests -> https://github.com/asyncapi/parser-js/blob/master/test/parse_test.js#L568 RecursiveAncestor references is marked as circular but it's incorrect. There should be marked as circular the path:
RecursiveAncestor -> ancestorChildren -> items -> selfSomething -> test (which is RecursiveAncestor)
Expected behavior
Handling of circular refs should be generic and works with any cases, not only for properties in object and "circular" items, but also for other JSON Schema keywords.
Activity
derberg commentedon Apr 26, 2021
Hey, can you modify this issue a bit as I'm afraid it is touching 2 different topics, in the main description you're focused on explaining the bug, and then in expected behavior section you talk about a new feature.
magicmatatjahu commentedon Apr 26, 2021
@derberg For me, the lack of support for circular references in extensions is a bug (they can also be used in schemes, so it should be supported), but okay, I'll make a separate issue with a description of what it should look like for bindings and extensions, btw. the same way as for the scheme.
derberg commentedon Apr 26, 2021
lack of support != bug 😄
when I worked on it, the intention was to have it for schemas. When we add support for extensions and bindings, it will be
feat:notfix:github-actions commentedon Jun 26, 2021
This issue has been automatically marked as stale because it has not had recent activity 😴
It will be closed in 60 days if no further activity occurs. To unstale this issue, add a comment with detailed explanation.
Thank you for your contributions ❤️
github-actions commentedon Aug 28, 2021
This issue has been automatically marked as stale because it has not had recent activity 😴
It will be closed in 60 days if no further activity occurs. To unstale this issue, add a comment with detailed explanation.
Thank you for your contributions ❤️
magicmatatjahu commentedon Oct 15, 2021
I think I managed to figure out how to solve the problem with circular references throughout our document, and surprisingly it is not that difficult, but it requires a fix in the
Basemodel class.Before I go into the proposal, I'll give an example of a scheme (very complex) and describe what is circular.
What I see what is circular - gives the json pointer notation, the key is the value where the circular occurs and the key value is the object that is circular relative to the key:
I think the first two items are understandable - RecursiveSelf is circular in these areas, but the third item may seem strange. It goes to the
RecursiveAncestorobject and then indicates that the circular object is inancestorChildrenprop.But what about 4-6 elements. They show that we have 3 circular elements. First two is for
RecursiveSelfelement, but relative to the$.RecursiveAncestor.properties.ancestorChildrenobject - here we look at the circularity with respect toRecursiveAncestorobject and notRecursiveSelflike the previous one. The 6th element shows circularity with respect to$.RecursiveAncestor- we start withRecursiveAncestorand end with$.RecursiveAncestor.Here is the whole scheme with all circular items - see that the circularity against root is also checked:
If anyone does not understand that (or something is broken), please write :)
My suggestion:
x-parser-circular-refsin the root of parsed AsyncAPI document.Basemodel class to:Notes:
x-parser-circularextension in prop andx-parser-circular-propsextension in object element are no longer needed.iterators- see issue Circular "allOf" generates Maximum call stack size exceeded generator#666What do you think? The solution is not as complicated as it seems, just parsing and mapping the circular elements takes 30 lines (I have worked POC), more work will be done with passing the parents and paths in the elements, so we will have to update each model constructor.
cc @derberg @jonaslagoni @fmvilas @boyney123 @smoya if you are not interested in the topic, you don't have to write back, but maybe it will interest you :)
magicmatatjahu commentedon Oct 15, 2021
I came up with another way, a simpler one based only on the
parentobject in Schema model that creates the given model:Compared to the first solution is less optimal because we have to check the whole sequence of downward parents, while in the first solution we check the circularity once and use the information.
m-wild commentedon Oct 17, 2021
Hi there, seems this has been partially resolved in the react/ui repo, hope this is the right place to raise the issue :)
It seems that this is not yet supported when the recursive reference is a
oneOfitem.Broken:
Working:
Tenischev commentedon Nov 14, 2021
Hi. I faced an issue during testing fix for asyncapi-archived-repos/java-spring-template#150
if I provide such schema for parser:
It returns three messages instead of one, these two extra messages will be
anonymousand just copy of normal one -lightMeasured:asyncapi-bot commentedon Nov 23, 2021
🎉 This issue has been resolved in version 1.12.0 🎉
The release is available on:
Your semantic-release bot 📦🚀
magicmatatjahu commentedon Jan 3, 2022
@tehmantra Very sorry, I saw these comments only now. We published new generator/html-template/react-component with supporting of circular refs. Could you check if everything works already in your cases? I would appreciate it :)
magicmatatjahu commentedon Jan 3, 2022
@Tenischev Hello! Very sorry, I saw these comments only now. Hmm 🤔 I am not sure if it is related to circular references, but I improved the traverse of the schemas in the parser and maybe the latest version of generator/parser solved the problem. Could you please check?