Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Fix: definition with inner schema and anonymous schemas #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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, '');
}