Skip to content

Commit

Permalink
Drop visited and test triple recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
torymur committed Dec 6, 2024
1 parent 50b2beb commit ac3b41f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
39 changes: 38 additions & 1 deletion src/json_schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub fn build_regex_from_schema(json: &str, whitespace_pattern: Option<&str>) ->
to_regex(&json_value, whitespace_pattern)
}

#[allow(clippy::wrong_self_convention)]
pub fn to_regex(json: &Value, whitespace_pattern: Option<&str>) -> Result<String> {
let mut parser = parsing::Parser::new(json);
if let Some(pattern) = whitespace_pattern {
Expand Down Expand Up @@ -1061,4 +1060,42 @@ mod tests {
regex,
);
}

#[test]
fn triple_recursion_doesnt_fail() {
let json = r##"
{
"definitions": {
"typeA": {
"type": "object",
"properties": {
"name": { "type": "string" },
"child": { "$ref": "#/definitions/typeB" }
},
"required": ["name"]
},
"typeB": {
"type": "object",
"properties": {
"value": { "type": "number" },
"next": { "$ref": "#/definitions/typeC" }
},
"required": ["value"]
},
"typeC": {
"type": "object",
"properties": {
"flag": { "type": "boolean" },
"parent": { "$ref": "#/definitions/typeA" }
},
"required": ["flag"]
}
},
"$ref": "#/definitions/typeA"
}"##;

let json_value: Value = serde_json::from_str(json).expect("Can't parse json");
let regex = to_regex(&json_value, None);
assert!(regex.is_ok(), "{:?}", regex);
}
}
4 changes: 0 additions & 4 deletions src/json_schema/parsing.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashSet;
use std::num::NonZeroU64;

use regex::escape;
Expand All @@ -13,7 +12,6 @@ type Result<T> = std::result::Result<T, JsonSchemaParserError>;
pub(crate) struct Parser<'a> {
root: &'a Value,
whitespace_pattern: &'a str,
visited: HashSet<usize>,
recursion_depth: usize,
max_recursion_depth: usize,
}
Expand All @@ -30,7 +28,6 @@ impl<'a> Parser<'a> {
Self {
root,
whitespace_pattern: types::WHITESPACE,
visited: HashSet::new(),
recursion_depth: 0,
max_recursion_depth: 3,
}
Expand Down Expand Up @@ -280,7 +277,6 @@ impl<'a> Parser<'a> {
}

fn parse_ref(&mut self, obj: &serde_json::Map<String, Value>) -> Result<String> {
self.visited.insert(obj as *const _ as usize);
if self.recursion_depth > self.max_recursion_depth {
return Err(JsonSchemaParserError::RefRecursionLimitReached(
self.max_recursion_depth,
Expand Down

0 comments on commit ac3b41f

Please sign in to comment.