Skip to content

Instantly share code, notes, and snippets.

@MrTrick
Created March 8, 2016 01:55
Show Gist options
  • Save MrTrick/2a46a65eee763b2f5a9c to your computer and use it in GitHub Desktop.
Save MrTrick/2a46a65eee763b2f5a9c to your computer and use it in GitHub Desktop.
var _=require('lodash');
/** Run the given from/to replace patterns on every reference in the given node **/
function translateRefs(node, from, to) {
if (node.$ref) node.$ref = node.$ref.replace(from, to);
else if (typeof node === 'object') for(var i in node) if (node.hasOwnProperty(i)) translateRefs(node[i], from, to);
}
function importSchema(file, name, root) {
var schema = _.cloneDeep(require(built_schemas+"/"+file)); //Import and copy the schema
translateRefs(schema, /^#\/definitions\//, "#/definitions/"+name+"-"); // Convert every internal reference to the new path
translateRefs(root, new RegExp("^"+file+"#/definitions/"), "#/definitions/"+name+"-"); // Convert every base reference to the new path
//Copy each imported definition into the root schema
for(var d in schema.definitions) if (schema.definitions.hasOwnProperty(d)) {
root.definitions[name+"-"+d] = schema.definitions[d];
//root.definitions[d]["x-imported-from"] = file;
}
}
function dumbDown(node) {
var unsupported = {oneOf:1,anyOf:1,patternProperties:1};
if (typeof node === 'object') for(var i in node) if (node.hasOwnProperty(i)) {
//Remove any unsupported keywords
if (i in unsupported) { node["x-"+i] = node[i]; delete node[i]; } //Remove any unsupported keywords
dumbDown(node[i]);
}
}
/** If the 'allOf' keyword is used, bring any properties inside it to avoid the CHILD_' + code + '_REDECLARES_PROPERTY failure */
function fixAllOf(node) {
if (node.allOf && node.properties) {
node.allOf.push({properties: node.properties});
delete node.properties;
}
if (typeof node === 'object') for(var i in node) if (node.hasOwnProperty(i)) fixAllOf(node[i]);
}
function removeUnused(root) {
var refs = {};
(function find(node) {
if (node.$ref) refs[node.$ref]=true;
else if (typeof node === 'object') for(var i in node) if (node.hasOwnProperty(i)) find(node[i]);
})(root);
for(i in root.definitions) if (root.definitions.hasOwnProperty(i)) {
if (!("#/definitions/"+i in refs)) { delete root.definitions[i]; }
}
}
var swagger = _.cloneDeep(require(built_schemas+"/swagger.json")); //Grab the swagger spec
importSchema("myschema.json", "myschema", swagger);
importSchema("json-api.json", "json-api", swagger);
dumbDown(swagger.definitions);
fixAllOf(swagger);
removeUnused(swagger);
@tmeinlschmidt
Copy link

hey MrTrick, did you somehow manage how to generate swagger out of json:api? Thanks a lot

@aCandidMind
Copy link

aCandidMind commented Oct 26, 2017

It's been a while since your question @tmeinlschmidt, but the excellent json:api solution for Ruby called jsonapi_suite has autogenerated documentation:
https://jsonapi-suite.github.io/jsonapi_suite/how-to-autodocument

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment