ast/schema: add support for recursive json schema elements (#5167)
This adds support for recursive json schemas, such as:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/open-policy-agent/opa/issues/5166",
"type": "object",
"properties": {
"Something": {
"$ref": "#/$defs/X"
}
},
"$defs": {
"X": {
"type": "object",
"properties": {
"Y": {
"$ref": "#/$defs/Y"
}
}
},
"Y": {
"type": "object",
"properties": {
"X": {
"$ref": "#/$defs/X"
}
}
}
}
}
The parseSchema method now hangs from a schemaParser struct. This allows the use of a
cache that can be populated with definitions as they are seen, to prevent them being
reparsed later. They are added to cache before they are completely parsed, otherwise the
recursive crash would still occur. Thus they are added to the cache with all properties
without values, then the values are added in by reference after the subsequent child tree parse.
I've added a couple of tests that cover my use case, as well as a schema parsing test (this
already passed before my changes - the compile time analysis of the schema is the problem)
for good measure.
Fixes #5166.
Signed-off-by: Liam Galvin <liam.galvin@aquasec.com> L
Liam Galvin committed
65d7495a8a3944044ac7c2eb595b8c607abe39df
Parent: d56d3b6
Committed by GitHub <noreply@github.com>
on 9/23/2022, 9:10:53 AM