Skip to content

Commit ac3b41f

Browse files
committed
Drop visited and test triple recursion
1 parent 50b2beb commit ac3b41f

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/json_schema/mod.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub fn build_regex_from_schema(json: &str, whitespace_pattern: Option<&str>) ->
1414
to_regex(&json_value, whitespace_pattern)
1515
}
1616

17-
#[allow(clippy::wrong_self_convention)]
1817
pub fn to_regex(json: &Value, whitespace_pattern: Option<&str>) -> Result<String> {
1918
let mut parser = parsing::Parser::new(json);
2019
if let Some(pattern) = whitespace_pattern {
@@ -1061,4 +1060,42 @@ mod tests {
10611060
regex,
10621061
);
10631062
}
1063+
1064+
#[test]
1065+
fn triple_recursion_doesnt_fail() {
1066+
let json = r##"
1067+
{
1068+
"definitions": {
1069+
"typeA": {
1070+
"type": "object",
1071+
"properties": {
1072+
"name": { "type": "string" },
1073+
"child": { "$ref": "#/definitions/typeB" }
1074+
},
1075+
"required": ["name"]
1076+
},
1077+
"typeB": {
1078+
"type": "object",
1079+
"properties": {
1080+
"value": { "type": "number" },
1081+
"next": { "$ref": "#/definitions/typeC" }
1082+
},
1083+
"required": ["value"]
1084+
},
1085+
"typeC": {
1086+
"type": "object",
1087+
"properties": {
1088+
"flag": { "type": "boolean" },
1089+
"parent": { "$ref": "#/definitions/typeA" }
1090+
},
1091+
"required": ["flag"]
1092+
}
1093+
},
1094+
"$ref": "#/definitions/typeA"
1095+
}"##;
1096+
1097+
let json_value: Value = serde_json::from_str(json).expect("Can't parse json");
1098+
let regex = to_regex(&json_value, None);
1099+
assert!(regex.is_ok(), "{:?}", regex);
1100+
}
10641101
}

src/json_schema/parsing.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::collections::HashSet;
21
use std::num::NonZeroU64;
32

43
use regex::escape;
@@ -13,7 +12,6 @@ type Result<T> = std::result::Result<T, JsonSchemaParserError>;
1312
pub(crate) struct Parser<'a> {
1413
root: &'a Value,
1514
whitespace_pattern: &'a str,
16-
visited: HashSet<usize>,
1715
recursion_depth: usize,
1816
max_recursion_depth: usize,
1917
}
@@ -30,7 +28,6 @@ impl<'a> Parser<'a> {
3028
Self {
3129
root,
3230
whitespace_pattern: types::WHITESPACE,
33-
visited: HashSet::new(),
3431
recursion_depth: 0,
3532
max_recursion_depth: 3,
3633
}
@@ -280,7 +277,6 @@ impl<'a> Parser<'a> {
280277
}
281278

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

0 commit comments

Comments
 (0)