Skip to content
This repository has been archived by the owner on Sep 6, 2019. It is now read-only.

Commit

Permalink
Converts to x-raml-type on raml08 importer
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Mateus committed Feb 7, 2017
1 parent a4c982c commit 8c90082
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
25 changes: 23 additions & 2 deletions lib/importers/raml08.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions test/data/raml-import/raml/raml08-schemas.yaml
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions test/data/raml-import/swagger/raml08-schemas.yaml
Original file line number Diff line number Diff line change
@@ -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}/"
}

0 comments on commit 8c90082

Please sign in to comment.