Skip to content

Commit

Permalink
fix(traverseschema.js): skip generating files for certain keywords in…
Browse files Browse the repository at this point in the history
… the schema

When traversing the schemas, some objects and arrays are misinterpreted as schemas to be documented
rather than as generic structures used to define the schemas. Namely: 'examples', 'required', and
'properties'. Including these structures in the loadedschemas list generates a number of extranious
files with little value to the generated documentation.

BREAKING CHANGE: The extranious files, if they have some other value, will not be generated and
there is no mechanism to continue to g
  • Loading branch information
togmund committed Jan 6, 2021
1 parent 36d42ad commit fc50969
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions lib/traverseSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,45 @@
* governing permissions and limitations under the License.
*/

const { list } = require('ferrum');
const { list, eq } = require('ferrum');
const { parent } = require('./symbols');

function isSkippable(schema, keyword) {
if (typeof schema[parent] !== 'object') {
return false;
}
if (!Object.keys(schema[parent]).includes(keyword)) {
return false;
}
const skippableSchema = JSON.parse(JSON.stringify(schema[parent][keyword]));
const currentSchema = JSON.parse(JSON.stringify(schema));

return eq(skippableSchema, currentSchema);
}

function reducer({ seen, ids }, schema) {
if (schema
&& schema[parent]
&& (seen.has(schema) || (schema.$id && ids.indexOf(schema.$id) >= 0))) {
return { seen, ids };
} else if (Array.isArray(schema)) {
if (isSkippable(schema, 'examples')) {
return { seen, ids };
}
if (isSkippable(schema, 'required')) {
return { seen, ids };
}
return schema.reduce(reducer, { seen, ids });
} else if (schema && typeof schema === 'object') {
if (isSkippable(schema, 'properties')) {
return [...Object.values(schema)].reduce(reducer, { seen, ids });
}
seen.add(schema);
if (schema.$id) {
ids.push(schema.$id);
}
return [...Object.values(schema)].reduce(reducer, { seen, ids });
}

return { seen, ids };
}

Expand Down

0 comments on commit fc50969

Please sign in to comment.