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

Commit 6130e1d

Browse files
author
Marc MacLeod
authored
Merge pull request #40 from mulesoft/master
Some bugs fixed. Some new features done.
2 parents 60fd7fb + 947036e commit 6130e1d

40 files changed

+3091
-111
lines changed

lib/exporters/baseraml.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -388,16 +388,19 @@ RAML.prototype.convertRefFromModel = function(object) {
388388
} else if (id === '$ref') {
389389
object.type = val.replace('#/definitions/', '');
390390
delete object[id];
391-
}
391+
} else if (id === 'exclusiveMinimum' || id === 'exclusiveMaximum') {
392+
delete object[id];
393+
}
392394
}
393395
}
394396

395397
return object;
396398
};
397399

398400
RAML.prototype._mapTraits = function(slTraits, mimeType) {
399-
var traits = [];
400-
var traitMap = {};
401+
var traits = this.initializeTraits();
402+
// var traits = [];
403+
// var traitMap = {};
401404

402405
for (var i in slTraits) {
403406
if (!slTraits.hasOwnProperty(i)) continue;
@@ -424,16 +427,18 @@ RAML.prototype._mapTraits = function(slTraits, mimeType) {
424427
}
425428
} catch(e) {}
426429

427-
var traitKey = _.camelCase(slTrait.name);
428-
var newTrait = {};
429-
newTrait[traitKey] = trait;
430-
traits.push(newTrait);
431-
traitMap[traitKey] = trait;
432-
}
430+
this.addTrait(slTrait.name, trait, traits);
433431

434-
if (this.version() === '1.0') {
435-
return traitMap;
432+
// var traitKey = _.camelCase(slTrait.name);
433+
// var newTrait = {};
434+
// newTrait[traitKey] = trait;
435+
// traits.push(newTrait);
436+
// traitMap[traitKey] = trait;
436437
}
438+
//
439+
// if (this.version() === '1.0') {
440+
// return traitMap;
441+
// }
437442

438443
return traits;
439444
};
@@ -547,7 +552,7 @@ RAML.prototype._export = function () {
547552
method.description = endpoint.Description;
548553
}
549554
if (endpoint.Summary) {
550-
method.description = endpoint.Summary + '. ' + method.description;
555+
method.description = endpoint.Summary + (method.description ? '. ' + method.description : '');
551556
}
552557

553558
var is = this._mapEndpointTraits(this.project.Traits, endpoint);
@@ -723,4 +728,8 @@ RAML.prototype.mapSecuritySchemes = function(securitySchemes) { throw new Error(
723728

724729
RAML.prototype.setMethodDisplayName = function(method, displayName) { throw new Error('setMethodDisplayName method not implemented'); };
725730

731+
RAML.prototype.initializeTraits = function() { throw new Error('initializeTraits method not implemented'); };
732+
733+
RAML.prototype.addTrait = function(id, trait, traits) { throw new Error('addTrait method not implemented'); };
734+
726735
module.exports = RAML;

lib/exporters/raml08.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,14 @@ RAML08.prototype.mapSecuritySchemes = function(securitySchemes) {
108108

109109
RAML08.prototype.setMethodDisplayName = function(merthod, displayName) {};
110110

111+
RAML08.prototype.initializeTraits = function() {
112+
return [];
113+
};
114+
115+
RAML08.prototype.addTrait = function(id, trait, traits) {
116+
var newTrait = {};
117+
newTrait[_.camelCase(id)] = trait;
118+
traits.push(newTrait);
119+
};
111120

112121
module.exports = RAML08;

lib/exporters/raml10.js

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,16 @@ RAML10.prototype.mapAuthorizationGrants = function(flow) {
4848
RAML10.prototype.mapBody = function(bodyData) {
4949
var body = jsonHelper.parse(bodyData.body);
5050
var result = this.convertAllOfToModel(this.convertRefFromModel(body));
51+
if (bodyData.example) {
52+
result.example = jsonHelper.parse(bodyData.example);
53+
}
54+
55+
//MERGE
56+
// var example = jsonHelper.format(result.example);
57+
// if (!_.isEmpty(example)) {
58+
// result.example = example;
59+
// }
5160

52-
var example = jsonHelper.format(result.example);
53-
if (!_.isEmpty(example)) {
54-
result.example = example;
55-
}
5661

5762
return result;
5863
};
@@ -110,25 +115,30 @@ RAML10.prototype.convertAllOfToModel = function(object) {
110115

111116

112117
RAML10.prototype.convertAllOfAttribute = function(definition) {
118+
var result = {};
113119
var allOfTypes = [];
114120
if (!definition.allOf) return definition;
115121

116122
for (var j in definition.allOf){
117123
if (!definition.allOf.hasOwnProperty(j)) continue;
118124
var allOf = definition.allOf[j];
119125
if (allOf.properties) {
120-
definition = this.mapSchemaProperties(allOf);
121-
break;
122-
}
123-
if (allOf.type) {
126+
result = this.mapSchemaProperties(allOf);
127+
} else if (allOf.type) {
124128
allOfTypes.push(allOf.type);
125129
}
126130
}
127-
definition.type = allOfTypes.length > 1 ? allOfTypes : allOfTypes[0];
128131

129-
delete definition.allOf;
132+
result.type = allOfTypes.length > 1 ? allOfTypes : allOfTypes[0];
133+
134+
delete result.allOf;
130135

131-
return definition;
136+
// definition.type = allOfTypes.length > 1 ? allOfTypes : allOfTypes[0];
137+
//
138+
// delete definition.allOf;
139+
140+
141+
return result;
132142
};
133143

134144
RAML10.prototype.mapSchema = function(slSchemas) {
@@ -146,9 +156,21 @@ RAML10.prototype.mapSchema = function(slSchemas) {
146156
}
147157
}
148158

149-
var example = jsonHelper.parse(schema.example);
150-
if (!_.isEmpty(example)) {
151-
definition.example = example;
159+
160+
if (definition.additionalProperties) {
161+
if (!definition.properties) {
162+
definition.properties = {};
163+
}
164+
definition.properties['//'] = definition.additionalProperties;
165+
delete definition.additionalProperties;
166+
}
167+
168+
if (schema.example) {
169+
definition.example = jsonHelper.parse(schema.example);
170+
171+
// var example = jsonHelper.parse(schema.example);
172+
// if (!_.isEmpty(example)) {
173+
// definition.example = example;
152174
}
153175

154176
results[schema.NameSpace] = definition;
@@ -203,4 +225,12 @@ RAML10.prototype.setMethodDisplayName = function(method, displayName) {
203225
method.displayName = displayName;
204226
};
205227

228+
RAML10.prototype.initializeTraits = function() {
229+
return {};
230+
};
231+
232+
RAML10.prototype.addTrait = function(id, trait, traits) {
233+
traits[_.camelCase(id)] = trait;
234+
};
235+
206236
module.exports = RAML10;

lib/exporters/swagger.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,11 @@ Swagger.prototype._mapHostAndProtocol = function (env, swaggerDef) {
635635
if (hostUrl.path && hostUrl.path !== '/') {
636636
swaggerDef.BasePath = urlHelper.join(hostUrl.path, env.BasePath);
637637
}
638-
638+
639+
if (this._isTemplateUri(swaggerDef.basePath)) {
640+
this._convertToTemplateUri(swaggerDef);
641+
}
642+
639643
if(Array.isArray(env.Protocols) && !_.isEmpty(env.Protocols)) {
640644
var filteredSchemes = [];
641645
env.Protocols.map(function(p){
@@ -651,6 +655,15 @@ Swagger.prototype._mapHostAndProtocol = function (env, swaggerDef) {
651655
}
652656
};
653657

658+
Swagger.prototype._isTemplateUri = function (uri) {
659+
var decodeUri = decodeURI(uri);
660+
return decodeUri.indexOf('{') !== -1 || decodeUri.indexOf('}') !== -1;
661+
};
662+
663+
Swagger.prototype._convertToTemplateUri = function (swaggerDef) {
664+
swaggerDef['x-basePath'] = decodeURI(swaggerDef.basePath);
665+
delete swaggerDef.basePath;
666+
};
654667

655668
Swagger.prototype._export = function () {
656669
//TODO

lib/importers/baseraml.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,11 @@ RAML.prototype.convertRefToModel = function(object) {
216216
delete object[id];
217217
}
218218
else {
219-
object[id] = this.convertRefToModel(val);
219+
if (id == 'xml') { //no process xml object
220+
object[id] = val;
221+
} else {
222+
object[id] = this.convertRefToModel(val);
223+
}
220224
}
221225
} else if (id == 'name') { //delete garbage
222226
delete object[id];

lib/importers/raml10.js

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ RAML10.prototype.mapRequestBody = function(methodBody) {
1717
data.mimeType = i;
1818
if (mimeType.example) {
1919
data.example = mimeType.example;
20+
delete mimeType.example;
2021
}
2122

2223
if (mimeType.description) {
@@ -57,7 +58,9 @@ RAML10.prototype.mapRequestBody = function(methodBody) {
5758
default:
5859
}
5960
}
60-
61+
else if (this.isArray(mimeType)) {
62+
data.body = this.convertRefToModel(this.convertArray(mimeType));
63+
}
6164
else if (mimeType.schema && !_.isEmpty(mimeType.schema)) {
6265
data.body = this.convertRefToModel({
6366
type: mimeType.schema[0]
@@ -75,7 +78,6 @@ RAML10.prototype.mapRequestBody = function(methodBody) {
7578

7679

7780
RAML10.prototype.mapSchema = function(schemData) {
78-
7981
var schemas = [];
8082
for (var i in schemData) {
8183
if (!schemData.hasOwnProperty(i)) continue;
@@ -98,7 +100,12 @@ RAML10.prototype.mapSchema = function(schemData) {
98100
for (var paramName in definition.properties) {
99101
if (!definition.properties.hasOwnProperty(paramName)) continue;
100102
var param = definition.properties[paramName];
101-
data.properties[paramName] = param;
103+
104+
if (this.isArray(param)) {
105+
data.properties[paramName] = this.convertArray(param);
106+
} else {
107+
data.properties[paramName] = param;
108+
}
102109

103110
if (param.hasOwnProperty('required')) {
104111
if (param.required == true) {
@@ -115,8 +122,8 @@ RAML10.prototype.mapSchema = function(schemData) {
115122
delete data.required;
116123
}
117124
}
118-
119-
if (definition.type && definition.type !== 'object') { //type
125+
126+
if (definition.type && definition.type != 'object') { //type
120127
if (data) { //type and properties
121128
definition.allOf = definition.type;
122129
definition.allOf.push(data);
@@ -127,6 +134,16 @@ RAML10.prototype.mapSchema = function(schemData) {
127134
definition.allOf = definition.type;
128135
delete definition.type;
129136
}
137+
else if (this.isArray(definition)) { //check for array
138+
//convert array
139+
definition = this.convertArray(definition);
140+
}
141+
else if (this.isFacet(definition)) { //check for facets
142+
definition = this.convertFacet(definition);
143+
}
144+
else if (this.isFixedFacet(definition)) {
145+
definition = this.convertFixedFacet(definition);
146+
}
130147
else {
131148
definition = jsonHelper.parse(_.isArray(definition.type) ? definition.type[0] : definition.type);
132149
}
@@ -141,6 +158,61 @@ RAML10.prototype.mapSchema = function(schemData) {
141158
return schemas;
142159
};
143160

161+
RAML10.prototype.isArray = function(definition) {
162+
var type = _.isArray(definition.type) ? definition.type[0] : definition.type;
163+
return type === 'array' && definition.items;
164+
};
165+
166+
RAML10.prototype.isFacet = function(definition) {
167+
return definition.facets;
168+
};
169+
170+
RAML10.prototype.isFixedFacet = function(definition) {
171+
return definition.fixedFacets;
172+
};
173+
174+
RAML10.prototype.convertArray = function(definition) {
175+
if (definition.items.type) {
176+
definition.items.type = _.isArray(definition.items.type) ? definition.items.type[0] : definition.items.type;
177+
} else {
178+
var items = definition.items;
179+
definition.items = {};
180+
definition.items.type = items;
181+
}
182+
definition.type = 'array';
183+
184+
return definition;
185+
};
186+
187+
RAML10.prototype.convertFacet = function(definition) {
188+
var facets = definition.facets;
189+
var result = [];
190+
for (var key in facets) {
191+
if (!facets.hasOwnProperty(key)) continue;
192+
var facet = facets[key];
193+
facet[key] = _.isArray(facet.type) ? facet.type[0] : facet.type;
194+
delete facet.name;
195+
delete facet.type;
196+
result.push(facet);
197+
}
198+
definition['x-facets'] = result;
199+
delete definition.facets;
200+
201+
return definition;
202+
};
203+
204+
RAML10.prototype.convertFixedFacet = function(definition) {
205+
var result = [];
206+
var fixedFacets = definition.fixedFacets;
207+
for (var key in fixedFacets) {
208+
if (!fixedFacets.hasOwnProperty(key)) continue;
209+
definition['x-' + key] = fixedFacets[key];
210+
}
211+
delete definition.fixedFacets;
212+
213+
return definition;
214+
};
215+
144216
RAML10.prototype.getSchema = function (data) {
145217
return data.types;
146218
};

lib/importers/swagger.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,17 +231,19 @@ Swagger.prototype._mapResponseBody = function(responseBody, skipParameterRefs, r
231231
res.body = schema;
232232
}
233233

234-
if (responseBody[code].hasOwnProperty('examples') && _.isEmpty(responseBody[code].examples)) {
234+
if (responseBody[code].hasOwnProperty('examples') && !_.isEmpty(responseBody[code].examples)) {
235235
var examples = responseBody[code].examples;
236-
res.example = jsonHelper.stringify(examples[0], 4);
237236

238-
// TODO: Once stoplight supports multiple examples, support them here.
239-
// for(var t in examples) {
240-
// if (!examples.hasOwnProperty(t)) continue;
241-
// if (t === resType) {
242-
// res.example = jsonHelper.stringify(examples[t], 4);
243-
// }
244-
// }
237+
if (_.isArray(examples)) {
238+
for(var t in examples) {
239+
if (!examples.hasOwnProperty(t)) continue;
240+
if (t === resType) {
241+
res.example = jsonHelper.stringify(examples[t], 4);
242+
}
243+
}
244+
} else {
245+
res.example = jsonHelper.stringify(examples, 4);
246+
}
245247
}
246248

247249
res.description = jsonHelper.stringify(description || responseBody[code].description || '');
@@ -435,7 +437,7 @@ Swagger.prototype._mapEndpoints = function(consumes, produces) {
435437

436438
endpoint.Tags = currentMethod.tags || [];
437439
endpoint.Summary = (currentMethod.summary || '').substring(0, 139);
438-
endpoint.Description = jsonHelper.stringify(currentMethod.description || currentMethod.summary);
440+
endpoint.Description = jsonHelper.stringify(currentMethod.description);
439441
endpoint.Deprecated = currentMethod.deprecated;
440442
endpoint.SetOperationId(currentMethod.operationId, method, path);
441443
endpoint.ExternalDocs = currentMethod.externalDocs;

0 commit comments

Comments
 (0)