From e1f8e855b22b8b6982936e01b4b6f73ae5dd7121 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alvaro=20Cabrera=20Dur=C3=A1n?= <pateketrueke@gmail.com>
Date: Wed, 9 Oct 2019 23:58:08 -0500
Subject: [PATCH] Handle unwanted properties from oneOf mixins; fix

---
 src/core/run.js      | 30 ++++++++++++++++--------------
 src/core/traverse.js |  2 +-
 2 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/src/core/run.js b/src/core/run.js
index 68244643..25577433 100644
--- a/src/core/run.js
+++ b/src/core/run.js
@@ -185,7 +185,7 @@ function run(refs, schema, container) {
 
           // call given thunks if present
           utils.merge(sub, typeof _sub.thunk === 'function'
-            ? _sub.thunk()
+            ? _sub.thunk(sub)
             : _sub);
         });
       }
@@ -200,24 +200,26 @@ function run(refs, schema, container) {
         }
 
         return {
-          thunk() {
+          thunk(rootSchema) {
             const copy = utils.omitProps(sub, ['anyOf', 'oneOf']);
             const fixed = random.pick(mix);
 
             utils.merge(copy, fixed);
 
-            if (sub.oneOf && copy.properties) {
-              mix.forEach(omit => {
-                if (omit !== fixed && omit.required) {
-                  omit.required.forEach(key => {
-                    // remove additional properties from merged schemas
-                    if (!copy.required.includes(key)) {
-                      delete copy.properties[key];
-                    }
-                  });
-                }
-              });
-            }
+            // remove additional properties from merged schemas
+            mix.forEach(omit => {
+              if (omit.required && omit !== fixed) {
+                omit.required.forEach(key => {
+                  if (copy.properties && !copy.required.includes(key)) {
+                    delete copy.properties[key];
+                  }
+
+                  if (rootSchema && rootSchema.properties) {
+                    delete rootSchema.properties[key];
+                  }
+                });
+              }
+            });
 
             return copy;
           },
diff --git a/src/core/traverse.js b/src/core/traverse.js
index 32135b12..6258f075 100644
--- a/src/core/traverse.js
+++ b/src/core/traverse.js
@@ -54,7 +54,7 @@ function traverse(schema, path, resolve, rootSchema) {
 
   // thunks can return sub-schemas
   if (typeof schema.thunk === 'function') {
-    return traverse(schema.thunk(), path, resolve);
+    return traverse(schema.thunk(rootSchema), path, resolve);
   }
 
   if (typeof schema.generate === 'function') {
-- 
GitLab