diff --git a/lib/importers/raml08.js b/lib/importers/raml08.js index 092015cb..1efcf496 100644 --- a/lib/importers/raml08.js +++ b/lib/importers/raml08.js @@ -1,7 +1,9 @@ const RAMLImporter = require('./baseraml'), Schema = require('../entities/schema'), jsonHelper = require('../utils/json'), - Text = require('../entities/text'); + ramlHelper = require('../helpers/raml'), + Text = require('../entities/text'), + _ = require('lodash'); class RAML08Importer extends RAMLImporter { constructor() { @@ -54,13 +56,32 @@ class RAML08Importer extends RAMLImporter { const sd = new Schema(schemaName); sd.Name = schemaName; - sd.Definition = jsonHelper.cleanSchema(schemData[i][schemaName]); + let definition = RAML08Importer._mapSchema(schemData[i][schemaName], true); + sd.Definition = jsonHelper.cleanSchema(definition); schemas.push(sd); } } return schemas; } + static _mapSchema(definition, isSchema) { + definition = jsonHelper.parse(definition); + for (const id in definition) { + if (!definition.hasOwnProperty(id)) continue; + let val = definition[id]; + if (id === 'type') { + if (_.isArray(val) && val.length == 1) val = val[0]; + if (typeof val === 'string' && val != 'object' && ramlHelper.getScalarTypes.indexOf(val) < 0) { + definition['x-raml-type'] = val; + delete definition.type; + } + } else if (typeof val === 'object') { + RAML08Importer._mapSchema(val, isSchema); + } + } + return definition; + } + //noinspection JSMethodCanBeStatic getSchemas(data) { return data.schemas; diff --git a/test/data/raml-import/raml/raml08-schemas.yaml b/test/data/raml-import/raml/raml08-schemas.yaml new file mode 100644 index 00000000..c834078f --- /dev/null +++ b/test/data/raml-import/raml/raml08-schemas.yaml @@ -0,0 +1,25 @@ +#%RAML 0.8 +title: Box +version: 2.0 +baseUri: https://api.box.com/{version}/ +mediaType: application/json +schemas: + - NewTask: | + { + "$schema": "http://json-schema.org/draft-03/schema", + "type": "object", + "properties": { + "action": { + "description": "The action the task assignee will be prompted to do. Must be 'review'.", + "type": [ "review" ] + }, + "due_at": { + "description": "The day at which this task is due.", + "type": "timestamp" + } + }, + "required": [ "action" ] + } +documentation: + - title: Headline + content: The Box Content API \ No newline at end of file diff --git a/test/data/raml-import/swagger/raml08-schemas.yaml b/test/data/raml-import/swagger/raml08-schemas.yaml new file mode 100644 index 00000000..31f72484 --- /dev/null +++ b/test/data/raml-import/swagger/raml08-schemas.yaml @@ -0,0 +1,38 @@ +{ + "consumes": [ + "application/json" + ], + "definitions": { + "NewTask": { + "properties": { + "action": { + "description": "The action the task assignee will be prompted to do. Must be 'review'.", + "x-raml-type": "review" + }, + "due_at": { + "description": "The day at which this task is due.", + "x-raml-type": "timestamp" + } + }, + "required": [ + "action" + ], + "type": "object" + } + }, + "host": "api.box.com", + "info": { + "description": "The Box Content API", + "title": "Box", + "version": "2.0" + }, + "paths": {}, + "produces": [ + "application/json" + ], + "schemes": [ + "https" + ], + "swagger": "2.0", + "x-basePath": "/{version}/" +} \ No newline at end of file