From 274a667ef23505c8dd71e04e28e706f32e423914 Mon Sep 17 00:00:00 2001 From: Daniel Roth Date: Thu, 27 Oct 2016 17:02:57 +0200 Subject: [PATCH 1/2] Fix definitions with inner schemas that are declared with --- src/index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/index.js b/src/index.js index 54ab49e..4c1bde8 100644 --- a/src/index.js +++ b/src/index.js @@ -37,6 +37,20 @@ 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)))); + + var copySchema = _.cloneDeep(parent.items); + delete copySchema['$schema']; + delete parent['items']; + parent['items'] = {}; + parent['items']['$ref'] = '#/definitions/' + copySchema.title; + + swagger.definitions[copySchema.title] = {}; + swagger.definitions[copySchema.title] = copySchema; + }); + if ('mediaType' in raml) { swagger.consumes = [raml.mediaType]; swagger.produces = [raml.mediaType]; From 6bf79e6676e1f9531bc70425c823c432791e5a97 Mon Sep 17 00:00:00 2001 From: Daniel Roth Date: Thu, 27 Oct 2016 17:14:03 +0200 Subject: [PATCH 2/2] Fix for schema names that are anonymous to receive a non-overriding name --- src/index.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index 4c1bde8..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); @@ -41,14 +43,18 @@ exports.convert = function (raml) { _.each(jp.nodes(swagger.definitions, '$..*["$schema"]'), function(innerSchema) { var parent = jp.value(swagger.definitions, jp.stringify(_.dropRight(_.dropRight(innerSchema.path)))); - var copySchema = _.cloneDeep(parent.items); - delete copySchema['$schema']; - delete parent['items']; - parent['items'] = {}; - parent['items']['$ref'] = '#/definitions/' + copySchema.title; - - swagger.definitions[copySchema.title] = {}; - swagger.definitions[copySchema.title] = copySchema; + 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) { @@ -492,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, ''); +}