Skip to content
Snippets Groups Projects
Commit 74905cdb authored by Justin Ehlert's avatar Justin Ehlert
Browse files

Resolve JSON schema links

This is a naive implementation of resolving JSON schema links,
will want to pull schemas into definitions eventually.
parent 30c40004
No related branches found
No related tags found
No related merge requests found
......@@ -54,7 +54,7 @@ module.exports = function(grunt) {
* Copy swagger to swagger-ui
*/
swaggerUiSchema: {
src: 'build/swagger-flat.json',
src: 'build/swagger-ui.json',
dest: 'build/swagger-ui/swagger.json'
}
},
......@@ -70,6 +70,16 @@ module.exports = function(grunt) {
}
},
/**
* Resolve schema links in the swagger documentation
*/
resolveSchemaLinks: {
core: {
src: 'build/swagger-flat.json',
dest: 'build/swagger-ui.json'
}
},
/**
* Static hosting for swagger-ui docs
*/
......@@ -109,7 +119,8 @@ module.exports = function(grunt) {
*/
grunt.registerTask('build-schema', [
'copy:schema',
'flattenSwagger'
'flattenSwagger',
'resolveSchemaLinks'
]);
/**
......
......@@ -12,7 +12,7 @@ consumes:
paths:
$ref: ./paths/index.yaml
definitions:
$ref: ./definitions/index.yaml
# $ref: ./definitions/index.yaml
......@@ -5,5 +5,5 @@ get:
200:
description: 'OK'
schema:
$ref: "../schemas/output/groups-list.json"
$ref: "./schemas/output/groups-list.json"
......@@ -26,6 +26,10 @@
margin:0;
background: #fafafa;
}
.topbar .download-url-wrapper {
display: none !important;
}
</style>
</head>
......@@ -74,9 +78,10 @@ window.onload = function() {
// Build a system
const ui = SwaggerUIBundle({
url: "./swagger.json",
url: "swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
validatorUrl: null,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
......
'use strict';
module.exports = function(grunt) {
var path = require('path');
var fs = require('fs');
var resolve = require('json-refs').resolveRefs;
// Deep walk obj, replacing 'ref' keys with '$ref'
function replaceRefs(obj) {
var k, v, i;
for( k in obj ) {
v = obj[k];
if( k === 'ref' && typeof v === 'string' ) {
obj['$ref'] = v;
delete obj['ref'];
}
if( typeof v === 'object' ) {
replaceRefs(v);
} else if( typeof v === 'array' ) {
for( i = 0; i < v.length; i++ ) {
if( typeof v[i] === 'object' ) {
replaceRefs(v[i]);
}
}
}
}
}
function resolveContent(res, callback) {
var obj = JSON.parse(res.text);
replaceRefs(obj);
callback(undefined, obj);
}
grunt.registerMultiTask('resolveSchemaLinks', 'Resolve schema references in swagger JSON file', function() {
var src = this.data.src;
var dest = this.data.dest;
if(!fs.existsSync(src)) {
grunt.log.writeln('Could not find:', src);
return false;
}
var root = JSON.parse(fs.readFileSync(src).toString());
replaceRefs(root);
var resolveOpts = {
filter: ['relative'],
location: src,
loaderOptions: {
processContent: resolveContent
}
};
var done = this.async();
resolve(root, resolveOpts).then(function(results) {
var data = JSON.stringify(results.resolved, null, 2);
fs.writeFileSync(dest, data);
done();
});
});
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment