From e5e578a5197ade970841fb8c7ade50db8602b009 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alvaro=20Cabrera=20Dur=C3=A1n?= <pateketrueke@gmail.com>
Date: Thu, 10 Oct 2019 00:22:07 -0500
Subject: [PATCH] Handle recursion and self-references; fixes #258

---
 src/core/run.js                         | 7 +++----
 tests/schema/core/issues/issue-258.json | 1 -
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/src/core/run.js b/src/core/run.js
index f672b180..f7fda592 100644
--- a/src/core/run.js
+++ b/src/core/run.js
@@ -116,11 +116,10 @@ function resolve(obj, data, values, property) {
 
 // TODO provide types
 function run(refs, schema, container) {
+  let depth = 0;
+
   try {
     const result = traverse(utils.clone(schema), [], function reduce(sub, parentSchemaPath) {
-      // FIXME: handle recursive/self-references, see #258
-      // also, a depth-limit should be taken into account...
-
       if (typeof sub.generate === 'function') {
         return sub;
       }
@@ -135,7 +134,7 @@ function run(refs, schema, container) {
       }
 
       if (typeof sub.$ref === 'string') {
-        if (sub.$ref === '#') {
+        if (sub.$ref === '#' || ++depth > random.number(0, 2)) {
           delete sub.$ref;
           return sub;
         }
diff --git a/tests/schema/core/issues/issue-258.json b/tests/schema/core/issues/issue-258.json
index 8da1ff1b..c54c4079 100644
--- a/tests/schema/core/issues/issue-258.json
+++ b/tests/schema/core/issues/issue-258.json
@@ -53,7 +53,6 @@
     {
       "description": "should work as expected",
       "schema": "schemas.0",
-      "skip": true,
       "valid": true
     }
   ]
-- 
GitLab