Repository: codekie/openapi-examples-validator
Pull request #137 (2021-03-05 11:42:28)

remove outputting entire JSON in warning for using schema combiners
Implements #136

coveralls: 
[![Coverage Status](https://coveralls.io/builds/37684675/badge)](https://coveralls.io/builds/37684675)

Coverage increased (+0.006%) to 97.79% when pulling **2637281712f54cea6bdb96cdc0c450fb4442b403 on DerkSchooltink:DerkSchooltink/remove-json-output-warning** into **dfb587aaa0994c315910dec702d3fc6886d1342b on codekie:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/37684675/badge)](https://coveralls.io/builds/37684675)

Coverage increased (+0.006%) to 97.79% when pulling **2637281712f54cea6bdb96cdc0c450fb4442b403 on DerkSchooltink:DerkSchooltink/remove-json-output-warning** into **dfb587aaa0994c315910dec702d3fc6886d1342b on codekie:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/37684675/badge)](https://coveralls.io/builds/37684675)

Coverage increased (+0.006%) to 97.79% when pulling **2637281712f54cea6bdb96cdc0c450fb4442b403 on DerkSchooltink:DerkSchooltink/remove-json-output-warning** into **dfb587aaa0994c315910dec702d3fc6886d1342b on codekie:master**.


Patch:
diff --git a/src/impl/service/disallow-additional-properties.js b/src/impl/service/disallow-additional-properties.js
index a9ecf47..2a2206e 100644
--- a/src/impl/service/disallow-additional-properties.js
+++ b/src/impl/service/disallow-additional-properties.js
@@ -39,12 +39,12 @@ module.exports = {
  * Sets the flag to indicate that it doesn't allow properties that are not described in the schema
  * @param {Object}                  openApiSpec         The to-be-modified schema
  * @param {Array.<String>}          [examplePaths=[]]   The paths to the examples, which's content must not be modified
- * @param {JsonPathMatchCallback}   [callback=_callbackObjectTypeForNoAdditionalProperties] Function to be called on a
- *                                                      match
+ * @param {JsonPathMatchCallback}   [createCallback=_createCallbackObjectTypeForNoAdditionalProperties] Function that
+ *                                                      creates a callback to be called on a match
  * @private
  */
 function setNoAdditionalProperties(openApiSpec, examplePaths = [],
-    callback = _callbackObjectTypeForNoAdditionalProperties
+    createCallback = _createCallbackObjectTypeForNoAdditionalProperties
 ) {
     // Find all matches
     const paths = new Set();
@@ -64,7 +64,7 @@ function setNoAdditionalProperties(openApiSpec, examplePaths = [],
     _excludeExamples(openApiSpec, paths, examplePaths);
     // Set flag
     for (const jsPath of paths) {
-        _find(openApiSpec, jsPath, ResultType.value, callback);
+        _find(openApiSpec, jsPath, ResultType.value, createCallback(jsPath));
     }
 }
 
@@ -73,15 +73,17 @@ function setNoAdditionalProperties(openApiSpec, examplePaths = [],
  * @type JsonPathMatchCallback
  * @private
  */
-function _callbackObjectTypeForNoAdditionalProperties(value) {
-    const asString = JSON.stringify(value);
-    // any schema's that use JSON schema combiners should also be excluded
-    if (!JSON_SCHEMA_COMBINERS.some((combiner) => asString.includes(`"${combiner}"`))) {
-        value.additionalProperties = false;
-    } else {
-        console.warn('"additionalProperties" flag not set'
-            + `for ${asString} because it contains JSON-schema combiner keywords.`);
-    }
+function _createCallbackObjectTypeForNoAdditionalProperties(path) {
+    return (value) => {
+        const asString = JSON.stringify(value);
+        // any schema's that use JSON schema combiners should also be excluded
+        if (!JSON_SCHEMA_COMBINERS.some((combiner) => asString.includes(`"${combiner}"`))) {
+            value.additionalProperties = false;
+        } else {
+            console.warn('"additionalProperties" flag not set '
+                + `for ${path} because it contains JSON-schema combiner keywords.`);
+        }
+    };
 }
 
 /**


Test patch:
diff --git a/test/specs/impl/service/schema-extender.js b/test/specs/impl/service/schema-extender.js
index 8cb7b92..8cae3dd 100644
--- a/test/specs/impl/service/schema-extender.js
+++ b/test/specs/impl/service/schema-extender.js
@@ -7,7 +7,7 @@ const SCHEMA = _getSchema(),
 describe('The schema extender', function() {
     it('should find the right matches', function() {
         const paths = [];
-        setNoAdditionalProperties(SCHEMA, getJsonPathsToExamples(), (value, resultType, data) => {
+        setNoAdditionalProperties(SCHEMA, getJsonPathsToExamples(), () => (value, resultType, data) => {
             paths.push(data.path);
         });
         paths.sort().should.deep.equal(JSON_PATHS__OBJECTS.sort());