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

Commit

Permalink
Merge pull request #39 from stoplightio/raml08-schema-ref
Browse files Browse the repository at this point in the history
Raml08 schema ref
  • Loading branch information
Marc MacLeod authored Oct 16, 2016
2 parents 709ac61 + 59d3c4a commit a20a807
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 65 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ temp.json
.idea
.DS_Store
*.iml
/npm-debug.log
29 changes: 18 additions & 11 deletions lib/exporters/baseraml.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ RAML.prototype._mapSecurityScheme = function(slSecuritySchemes) {
content.headers = {};
for (var i in slSecuritySchemes.apiKey.headers) {
if (!slSecuritySchemes.apiKey.headers.hasOwnProperty(i)) continue;

var q = slSecuritySchemes.apiKey.headers[i];
var keyName = q.name;
content.headers[keyName] = {
Expand All @@ -133,7 +133,7 @@ RAML.prototype._mapSecurityScheme = function(slSecuritySchemes) {
content.queryParameters = {};
for (var i in slSecuritySchemes.apiKey.queryString) {
if (!slSecuritySchemes.apiKey.queryString.hasOwnProperty(i)) continue;

var q = slSecuritySchemes.apiKey.queryString[i];
var keyName = q.name;
content.queryParameters[keyName] = {
Expand Down Expand Up @@ -208,7 +208,7 @@ RAML.prototype._validateParam = function(params) {
RAML.prototype._mapRequestBody = function(bodyData, mimeType){
var body = {};
if (!bodyData.body || mimeType === '') return body;

switch(mimeType) {
case 'application/json':
body[mimeType] = this.mapBody(bodyData);
Expand All @@ -226,13 +226,13 @@ RAML.prototype._mapRequestBody = function(bodyData, mimeType){
if (bodyData.description) {
body[mimeType].description = bodyData.description;
}

return body;
};

RAML.prototype._mapNamedParams = function(params){
if (!params || _.isEmpty(params.properties)) return;

var newParams = {};
for (var key in params.properties) {
if (!params.properties.hasOwnProperty(key)) continue;
Expand Down Expand Up @@ -322,7 +322,7 @@ function mapProtocols(protocols) {
RAML.prototype._mapTextSections = function(slTexts) {
var results = [];
if (!slTexts) return resilts;

for (var i in slTexts) {
if (!slTexts.hasOwnProperty(i)) continue;
var text = slTexts[i];
Expand Down Expand Up @@ -397,21 +397,22 @@ RAML.prototype.convertRefFromModel = function(object) {

RAML.prototype._mapTraits = function(slTraits, mimeType) {
var traits = [];
var traitMap = {};

for (var i in slTraits) {
if (!slTraits.hasOwnProperty(i)) continue;
var slTrait = slTraits[i],
trait = {};

try {
var queryString = JSON.parse(slTrait.request.queryString);
var queryString = jsonHelper.parse(slTrait.request.queryString);
if (!jsonHelper.isEmptySchema(queryString)) {
trait.queryParameters = this._mapNamedParams(queryString);
}
} catch(e) {}

try {
var headers = JSON.parse(slTrait.request.headers);
var headers = jsonHelper.parse(slTrait.request.headers);
if (!jsonHelper.isEmptySchema(headers)) {
trait.headers = this._mapNamedParams(headers);
}
Expand All @@ -423,9 +424,15 @@ RAML.prototype._mapTraits = function(slTraits, mimeType) {
}
} catch(e) {}

var traitKey = _.camelCase(slTrait.name);
var newTrait = {};
newTrait[_.camelCase(slTrait.name)] = trait;
newTrait[traitKey] = trait;
traits.push(newTrait);
traitMap[traitKey] = trait;
}

if (this.version() === '1.0') {
return traitMap;
}

return traits;
Expand Down Expand Up @@ -637,7 +644,7 @@ RAML.prototype._export = function () {
}
};
}

if (this.hasInfo) {
ramlDef.annotationTypes.info = {
properties: {
Expand Down Expand Up @@ -689,7 +696,7 @@ RAML.prototype._unescapeYamlIncludes = function(yaml) {
RAML.prototype._getData = function(format) {
switch (format) {
case 'yaml':
var yaml = this._unescapeYamlIncludes(YAML.dump(JSON.parse(JSON.stringify(this.Data)), {lineWidth: -1}));
var yaml = this._unescapeYamlIncludes(YAML.dump(jsonHelper.parse(JSON.stringify(this.Data)), {lineWidth: -1}));
return '#%RAML ' + this.version() + '\n'+yaml;
default:
throw Error('RAML doesn not support '+format+' format');
Expand Down
36 changes: 20 additions & 16 deletions lib/exporters/raml10.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ RAML10.prototype.mapAuthorizationGrants = function(flow) {
RAML10.prototype.mapBody = function(bodyData) {
var body = jsonHelper.parse(bodyData.body);
var result = this.convertAllOfToModel(this.convertRefFromModel(body));
if (bodyData.example) {
result.example = jsonHelper.format(bodyData.example);
}

var example = jsonHelper.format(result.example);
if (!_.isEmpty(example)) {
result.example = example;
}

return result;
};

Expand Down Expand Up @@ -94,22 +97,22 @@ RAML10.prototype.convertAllOfToModel = function(object) {

var val = object[id];
if (!val) continue;

if (id == 'allOf') {
object = this.convertAllOfAttribute(object);
} else if (typeof val === 'object') {
object[id] = this.convertAllOfToModel(val);
}
}

return object;
};


RAML10.prototype.convertAllOfAttribute = function(definition) {
var allOfTypes = [];
if (!definition.allOf) return definition;

for (var j in definition.allOf){
if (!definition.allOf.hasOwnProperty(j)) continue;
var allOf = definition.allOf[j];
Expand All @@ -122,7 +125,7 @@ RAML10.prototype.convertAllOfAttribute = function(definition) {
}
}
definition.type = allOfTypes.length > 1 ? allOfTypes : allOfTypes[0];

delete definition.allOf;

return definition;
Expand All @@ -134,19 +137,20 @@ RAML10.prototype.mapSchema = function(slSchemas) {
if (!slSchemas.hasOwnProperty(i)) continue;
var schema = slSchemas[i];
var definition = this.convertRefFromModel(jsonHelper.parse(schema.Definition));

if (definition.allOf) {
definition = this.convertAllOfToModel(definition);
} else {
if (definition.properties) {
definition = this.mapSchemaProperties(definition);
}
}

if (schema.example) {
definition.example = jsonHelper.parse(schema.example);

var example = jsonHelper.parse(schema.example);
if (!_.isEmpty(example)) {
definition.example = example;
}

results[schema.NameSpace] = definition;
}
return results;
Expand All @@ -158,7 +162,7 @@ RAML10.prototype.mapSchemaProperties = function(definition) {
var property = definition.properties[k];
property.required = false;
}

if (definition.required && definition.required.length > 0) {
for(var j in definition.required) {
if (!definition.required.hasOwnProperty(j)) continue;
Expand All @@ -170,16 +174,16 @@ RAML10.prototype.mapSchemaProperties = function(definition) {
}
delete definition.required;
}

if (definition.additionalProperties) {
definition.properties['//'] = definition.additionalProperties;
delete definition.additionalProperties;
}

if (definition.properties && definition.type == 'object') {
delete definition.type;
}

return definition;
};

Expand Down
22 changes: 16 additions & 6 deletions lib/importers/baseraml.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,26 @@ RAML.prototype.isValidRefValue = function (value) {
return typeof value === 'string' && ramlHelper.getScalarTypes.indexOf(value) < 0 && value !== 'object';
};

// from type=type1 to ref=type1
// from type=type1 & schema=type1 to ref=type1
RAML.prototype.convertRefToModel = function(object) {
// if the object is a string, that means it's a direct ref/type
if (typeof object === 'string') {
return {
$ref: '#/definitions/' + object
};
}

for (var id in object) {
var isType = id == 'type';

for (var id in object) {
if (!object.hasOwnProperty(id)) continue;
if (id == 'type' && _.isArray(object[id]) && object[id].length == 1) {
if (isType && _.isArray(object[id]) && object[id].length == 1) {
object[id] = object[id][0];
}
var val = object[id];
if (!val) continue;

if (id == 'type' && this.isValidRefValues(val)) {
if (isType && this.isValidRefValues(val)) {
object.ref = val;
delete object[id];
}
Expand Down Expand Up @@ -481,8 +489,10 @@ RAML.prototype._import = function() {
this.project.Environment.SecuritySchemes = this._mapSecuritySchemes(this.data.securitySchemes);

var resources = this.data.resources;
for (var i = 0; i < resources.length; i++) {
this._mapEndpoint(resources[i], '', {});
if (!_.isEmpty(resources)) {
for (var i = 0; i < resources.length; i++) {
this._mapEndpoint(resources[i], '', {});
}
}

var schemas = this._mapSchema(this.getSchema(this.data));
Expand Down
22 changes: 22 additions & 0 deletions test/data/raml-import/raml/raml08-ref.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#%RAML 0.8
baseUri: https://api.example.com
title: Filesystem API
version: 0.1
schemas:
- Admin User: |
{
"type": "object",
"properties": {
"input": {
"required": false,
"type": "string"
}
}
}
/files:
get:
responses:
200:
body:
application/json:
schema: Admin User
41 changes: 41 additions & 0 deletions test/data/raml-import/swagger/raml08-ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"swagger": "2.0",
"info": {
"title": "Filesystem API",
"version": "0.1",
"description": ""
},
"host": "api.example.com",
"basePath": "/",
"schemes": [
"https"
],
"paths": {
"/files": {
"get": {
"operationId": "GET_files",
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "",
"schema": {
"$ref": "#/definitions/Admin User"
}
}
}
}
}
},
"definitions": {
"Admin User": {
"type": "object",
"properties": {
"input": {
"type": "string"
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"description": "Error details",
"type": "object"
},
"Foo": "can not resolve Person.xyz"
"Foo": {
"$ref": "#/definitions/can not resolve Person.xyz"
}
},
"host": "rest-api.baeldung.com",
"info": {
Expand Down Expand Up @@ -208,4 +210,4 @@
}
},
"swagger": "2.0"
}
}
6 changes: 4 additions & 2 deletions test/data/raml-import/swagger/raml10-include-type.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"description": "Error details",
"type": "object"
},
"Foo": "can not resolve types/Foo.json"
"Foo": {
"$ref": "#/definitions/can not resolve types/Foo.json"
}
},
"host": "rest-api.baeldung.com",
"info": {
Expand Down Expand Up @@ -208,4 +210,4 @@
}
},
"swagger": "2.0"
}
}
16 changes: 16 additions & 0 deletions test/data/swagger-import/raml/swagger-trait-to-raml08.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#%RAML 0.8
title: Test
documentation:
-
content: ''
title: 'Test'
traits:
- foo:
headers:
auth:
type: string
queryParameters:
limit:
type: string
responses:
'404': {}
12 changes: 12 additions & 0 deletions test/data/swagger-import/raml/swagger-trait-to-raml10.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#%RAML 1.0
title: Test
traits:
foo:
headers:
auth:
type: string
queryParameters:
limit:
type: string
responses:
'404': {}
Loading

0 comments on commit a20a807

Please sign in to comment.