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

Commit

Permalink
DESIGNER-167: swagger to raml : created annotations types for attribu…
Browse files Browse the repository at this point in the history
…tes supposed to be created as custom facets
  • Loading branch information
Gaston Lodieu committed Nov 29, 2016
1 parent 69f926b commit 4636036
Show file tree
Hide file tree
Showing 12 changed files with 383 additions and 345 deletions.
116 changes: 54 additions & 62 deletions lib/exporters/baseraml.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ function RAML() {
this.hasExternalDocs = false;
this.hasInfo = false;
this.hasSummary = false;
this.hasCollectionFormat = false;
this.hasAllowEmptyValue = false;
this.hasExclusiveMaximum = false;
this.hasExclusiveMinimum = false;
this.hasSchemaTitle = false;
this.hasBodyName = false;
this.hasResponsesDefault = false;
Expand Down Expand Up @@ -210,10 +206,11 @@ RAML.prototype._validateParam = function(params) {
case 'maxItems':
case 'minItems':
case 'uniqueItems':
case '(oas-collectionFormat)':
case '(oas-allowEmptyValue)':
case '(oas-exclusiveMaximum)':
case '(oas-exclusiveMinimum)':
case 'collectionFormat':
case 'allowEmptyValue':
case 'exclusiveMaximum':
case 'exclusiveMinimum':
case 'facets':
break;
default:
//not supported types
Expand Down Expand Up @@ -337,26 +334,8 @@ RAML.prototype._mapURIParams = function(pathParamData) {

pathParams[key].type = pathParams[key].type || 'string';

//annotation types
if (prop.hasOwnProperty('collectionFormat')) {
this.hasCollectionFormat = true;
pathParams[key]['(oas-collectionFormat)'] = prop.collectionFormat;
}

if (prop.hasOwnProperty('allowEmptyValue')) {
this.hasAllowEmptyValue = true;
pathParams[key]['(oas-allowEmptyValue)'] = prop.allowEmptyValue;
}

if (prop.hasOwnProperty('exclusiveMaximum')) {
this.hasExclusiveMaximum = true;
pathParams[key]['(oas-exclusiveMaximum)'] = prop.exclusiveMaximum;
}

if (prop.hasOwnProperty('exclusiveMinimum')) {
this.hasExclusiveMinimum = true;
pathParams[key]['(oas-exclusiveMinimum)'] = prop.exclusiveMinimum;
}
//facets
this._addFacetsDeclaration(prop, pathParams[key]);
}

return this._validateParam(pathParams);
Expand Down Expand Up @@ -420,6 +399,11 @@ RAML.prototype.convertRefFromModel = function(object) {
object['(oas-schema-title)'] = val;
this.hasSchemaTitle = true;
delete object[id];
} else if (id === 'collectionFormat') {
if (!object.facets) {
object.facets = {};
}
object.facets['collectionFormat'] = 'string';
}
} else if (val && (typeof val) === 'object') {
if (val.type == 'string') {
Expand Down Expand Up @@ -447,22 +431,59 @@ RAML.prototype.convertRefFromModel = function(object) {
} else if (id === '$ref') {
object.type = val.replace('#/definitions/', '');
delete object[id];
} else if (id === 'exclusiveMinimum' || id === 'exclusiveMaximum') {
delete object[id];
} else if (id === 'exclusiveMinimum' || id === 'exclusiveMaximum' || id === 'allowEmptyValue' || id === 'collectionFormat') {
if (!object.facets) {
object.facets = {};
}
if (id === 'exclusiveMinimum') {
object.facets['exclusiveMinimum'] = 'boolean';
}
if (id === 'exclusiveMaximum') {
object.facets['exclusiveMaximum'] = 'boolean';
}
if (id === 'allowEmptyValue') {
object.facets['allowEmptyValue'] = 'boolean';
}
if (id === 'collectionFormat') {
object.facets['collectionFormat'] = 'string';
}
}
}
}

return object;
};

RAML.prototype._addFacetsDeclaration = function(property, target) {
if (property.hasOwnProperty('collectionFormat') || property.hasOwnProperty('allowEmptyValue') ||
property.hasOwnProperty('exclusiveMaximum') || property.hasOwnProperty('exclusiveMinimum')) {

if (!target['facets']) {
target['facets'] = {};
}

if (property.hasOwnProperty('collectionFormat')) {
target['facets']['collectionFormat'] = 'string';
}
if (property.hasOwnProperty('allowEmptyValue')) {
target['facets']['allowEmptyValue'] = 'boolean';
}
if (property.hasOwnProperty('exclusiveMaximum')) {
target['facets']['exclusiveMaximum'] = 'boolean';
}
if (property.hasOwnProperty('exclusiveMinimum')) {
target['facets']['exclusiveMinimum'] = 'boolean';
}
}
};

RAML.prototype._mapParametersTraits = function(slTraits) {
var traits = this.initializeTraits();

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

try {
var queryString = jsonHelper.parse(slTrait.request.queryString);
Expand Down Expand Up @@ -551,8 +572,7 @@ function getDefaultMimeType(mimeType, defMimeType) {

RAML.prototype._annotationsSignature = function(ramlDef) {
if (this.hasTags || this.hasDeprecated || this.hasExternalDocs || this.hasInfo ||
this.hasSummary || this.hasCollectionFormat || this.hasAllowEmptyValue ||
this.hasExclusiveMaximum || this.hasExclusiveMinimum || this.hasSchemaTitle || this.hasBodyName || this.hasResponsesDefault)
this.hasSummary || this.hasSchemaTitle || this.hasBodyName || this.hasResponsesDefault)
{
if (!ramlDef.annotationTypes) {
ramlDef.annotationTypes = {};
Expand All @@ -578,34 +598,6 @@ RAML.prototype._annotationsSignature = function(ramlDef) {
allowedTargets: 'Method'
};
}

if (this.hasAllowEmptyValue) {
ramlDef.annotationTypes['oas-allowEmptyValue'] = {
type: 'boolean',
allowedTargets: 'TypeDeclaration'
};
}

if (this.hasExclusiveMaximum) {
ramlDef.annotationTypes['oas-exclusiveMaximum'] = {
type: 'boolean',
allowedTargets: 'TypeDeclaration'
};
}

if (this.hasExclusiveMinimum) {
ramlDef.annotationTypes['oas-exclusiveMinimum'] = {
type: 'boolean',
allowedTargets: 'TypeDeclaration'
};
}

if (this.hasCollectionFormat) {
ramlDef.annotationTypes['oas-collectionFormat'] = {
type: 'string',
allowedTargets: 'TypeDeclaration'
};
}

if (this.hasExternalDocs) {
ramlDef.annotationTypes['oas-externalDocs'] = {
Expand Down
30 changes: 4 additions & 26 deletions lib/exporters/raml10.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,11 @@ RAML10.prototype.mapRequestBodyForm = function(bodyData) {
for (var i in body.properties) {
if (!body.properties.hasOwnProperty(i)) continue;
var property = body.properties[i];

if (property.hasOwnProperty('allowEmptyValue')) {
this.hasAllowEmptyValue = true;
property['(oas-allowEmptyValue)'] = property.allowEmptyValue;
delete property.allowEmptyValue;
}

if (property.hasOwnProperty('exclusiveMaximum')) {
this.hasExclusiveMaximum = true;
property['(oas-exclusiveMaximum)'] = property.exclusiveMaximum;
delete property.exclusiveMaximum;
}

if (property.hasOwnProperty('exclusiveMinimum')) {
this.hasExclusiveMinimum = true;
property['(oas-exclusiveMinimum)'] = property.exclusiveMinimum;
delete property.exclusiveMinimum;
}

property.required = false;
}

//facets
this._addFacetsDeclaration(property, property);
}

if (bodyData.required && bodyData.required.length > 0) {
for(var j in bodyData.required) {
Expand Down Expand Up @@ -145,14 +129,8 @@ RAML10.prototype.convertAllOfAttribute = function(definition) {
}

result.type = allOfTypes.length > 1 ? allOfTypes : allOfTypes[0];

delete result.allOf;

// definition.type = allOfTypes.length > 1 ? allOfTypes : allOfTypes[0];
//
// delete definition.allOf;


return result;
};

Expand Down
4 changes: 2 additions & 2 deletions lib/helpers/raml.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
'displayName', 'type', 'description', 'default', 'maximum',
'minimum', 'maxLength', 'minLength', 'pattern', 'enum', 'format',
'collectionFormat', 'allowEmptyValue', 'exclusiveMaximum', 'exclusiveMinimum',
'maxItems', 'minItems', 'uniqueItems', 'required'
'maxItems', 'minItems', 'uniqueItems', 'required', 'facets'
],

setParameterFields: function(source, target) {
Expand Down Expand Up @@ -40,7 +40,7 @@ module.exports = {
} catch (e) {}
}

if (!target[prop] || (_.isArray(target[prop]) && _.isEmpty(target[prop]))) {
if (!target.hasOwnProperty(prop) || (_.isArray(target[prop]) && _.isEmpty(target[prop]))) {
delete target[prop];
}
}
Expand Down
9 changes: 5 additions & 4 deletions test/data/petstore-separate/spec/raml10.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,21 @@ description: A sample API that uses a petstore as an example to demonstrate feat
queryParameters:
tags:
description: tags to filter by
required: false
type: array
collectionFormat: csv
displayName: tags to filter by
items:
type: string
(oas-collectionFormat): csv
facets:
collectionFormat: string
limit:
description: maximum number of results to return
required: false
type: integer
format: int32
displayName: maximum number of results to return
annotationTypes:
oas-collectionFormat:
type: string
allowedTargets: TypeDeclaration
oas-info:
properties:
termsOfService?: string
Expand Down
6 changes: 6 additions & 0 deletions test/data/swagger-import/raml/bitbucket.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1383,10 +1383,12 @@ securitySchemes:
queryParameters:
context:
description: Generate diffs with <n> lines of context instead of the usual three
required: false
type: integer
displayName: Generate diffs with <n> lines of context instead of the usual three
path:
description: Limit the diff to a single file
required: false
type: string
displayName: Limit the diff to a single file
securedBy:
Expand Down Expand Up @@ -2198,6 +2200,7 @@ securitySchemes:
- contributor
- member
- owner
required: false
type: string
displayName: |
Expand Down Expand Up @@ -2779,6 +2782,7 @@ securitySchemes:
- owner
- contributor
- member
required: false
type: string
displayName: 'Filter down the result based on the authenticated user''s role (`owner`, `contributor`, or `member`).'
securedBy:
Expand Down Expand Up @@ -2847,6 +2851,7 @@ securitySchemes:
- owner
- contributor
- member
required: false
type: string
displayName: 'Filter down the result based on the authenticated user''s role (`owner`, `contributor`, or `member`).'
securedBy:
Expand Down Expand Up @@ -3523,6 +3528,7 @@ securitySchemes:
- admin
- contributor
- member
required: false
type: string
displayName: |
Expand Down
5 changes: 5 additions & 0 deletions test/data/swagger-import/raml/dataTypesExamples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ types:
minimum: 0
maximum: 10000
required: false
facets:
exclusiveMinimum: boolean
exclusiveMaximum: boolean
exclusiveMinimum: true
exclusiveMaximum: false
Vinyl:
properties:
albumName:
Expand Down
Loading

0 comments on commit 4636036

Please sign in to comment.