diff --git a/src/index.js b/src/index.js index 54ab49e..c153341 100644 --- a/src/index.js +++ b/src/index.js @@ -7,6 +7,8 @@ var jsonCompat = require('json-schema-compatibility'); var HttpStatus = require('http-status-codes').getStatusText; var jp = require('jsonpath'); +var seqNo = 1; + exports.convert = function (raml) { //FIXME: //console.log(raml.documentation); @@ -37,6 +39,24 @@ exports.convert = function (raml) { return result; }); + // Fix for inner schemas within definitions, that are declared with $schema + _.each(jp.nodes(swagger.definitions, '$..*["$schema"]'), function(innerSchema) { + var parent = jp.value(swagger.definitions, jp.stringify(_.dropRight(_.dropRight(innerSchema.path)))); + + if (!(typeof parent === 'undefined') && !(typeof parent.items === 'undefined')) { + var copySchema = _.cloneDeep(parent.items); + delete copySchema['$schema']; + delete parent['items']; + parent['items'] = {}; + if (typeof copySchema.title === 'undefined') { + copySchema.title = camelize('no name given' + seqNo++); + } + parent['items']['$ref'] = '#/definitions/' + camelize(copySchema.title); + swagger.definitions[copySchema.title] = {}; + swagger.definitions[copySchema.title] = copySchema; + } + }); + if ('mediaType' in raml) { swagger.consumes = [raml.mediaType]; swagger.produces = [raml.mediaType]; @@ -478,3 +498,9 @@ function convertSchema(schema) { return schema; } + +function camelize(str) { + return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(letter, index) { + return index == 0 ? letter.toLowerCase() : letter.toUpperCase(); + }).replace(/\s+/g, ''); +}