More updates for generating valid swagger
-Added items object to parameter when type is array. Setting default type of array elements to string. -Updated package.json to include a validator library. -Added basic validation test for a small set of generated files. -Increased the wait time for larger files. Change-Id: I35999fd035d7399d4cc8e46c1bb4edca99608ce3
This commit is contained in:
parent
c5ed51d3fc
commit
fc8330cf0f
|
@ -77,12 +77,6 @@ TYPE_MAP = {
|
|||
'xsd:base64binary': 'string',
|
||||
'enum': 'array',
|
||||
'xsd:float': 'number',
|
||||
|
||||
# TODO(arrsim) This array types also set the items
|
||||
# "tags": {
|
||||
# "type": "array",
|
||||
# "items": {
|
||||
# "type": "string"
|
||||
'xsd:list': 'array',
|
||||
'array': 'array',
|
||||
}
|
||||
|
@ -654,7 +648,7 @@ class WADLHandler(xml.sax.ContentHandler):
|
|||
self.current_api = {
|
||||
'produces': set(),
|
||||
'consumes': set(),
|
||||
'examples': {},
|
||||
'x-examples': {},
|
||||
'responses': {},
|
||||
'parameters': {},
|
||||
}
|
||||
|
@ -704,7 +698,13 @@ class WADLHandler(xml.sax.ContentHandler):
|
|||
for param, doc in self.url_params.items():
|
||||
if ('{%s}' % param) in url:
|
||||
parameter = create_parameter(param, 'template', doc)
|
||||
# in = path, requires true
|
||||
|
||||
# Swagger: array type requires items object
|
||||
if parameter['type'] == 'array':
|
||||
parameter['items'] = {}
|
||||
parameter['items']['type'] = 'string'
|
||||
|
||||
# Swagger: in = path, requires true
|
||||
parameter['required'] = True
|
||||
self.current_api['parameters'].append(parameter)
|
||||
|
||||
|
@ -798,13 +798,19 @@ class WADLHandler(xml.sax.ContentHandler):
|
|||
description='',
|
||||
type=attrs.get('type', 'string'),
|
||||
required=attrs.get('required'))
|
||||
|
||||
# Swagger: array type requires items object
|
||||
if parameter['type'] == 'array':
|
||||
parameter['items'] = {}
|
||||
parameter['items']['type'] = 'string'
|
||||
|
||||
if parameter['in'] == 'body':
|
||||
schema_name = parameters[0]['schema']['$ref'].rsplit('/', 1)[1]
|
||||
if schema_name not in self.schemas:
|
||||
self.schemas[schema_name] = {'type': 'object',
|
||||
'properties': {}}
|
||||
schema_properties = self.schemas[schema_name]['properties']
|
||||
# Array of required properties
|
||||
# Swagger: array of required properties
|
||||
if parameter['required'] is True:
|
||||
if 'required' not in self.schemas[schema_name]:
|
||||
self.schemas[schema_name]['required'] = []
|
||||
|
@ -838,6 +844,12 @@ class WADLHandler(xml.sax.ContentHandler):
|
|||
description='',
|
||||
type=attrs.get('type', 'string'),
|
||||
required=attrs.get('required'))
|
||||
|
||||
# Swagger: array type requires items object
|
||||
if parameter['type'] == 'array':
|
||||
parameter['items'] = {}
|
||||
parameter['items']['type'] = 'string'
|
||||
|
||||
if parameter['in'] == 'body':
|
||||
if len(parameters) > 0:
|
||||
s = parameters[0]['schema']['$ref']
|
||||
|
@ -935,7 +947,7 @@ def main1(source_file, output_dir):
|
|||
u'info': {
|
||||
'version': api_ref['version'],
|
||||
'title': api_ref['title'],
|
||||
# No service field, add as extension
|
||||
# Swagger: No service field, add as extension
|
||||
'x-service': api_ref['service'],
|
||||
'license': {
|
||||
"name": "Apache 2.0",
|
||||
|
@ -967,7 +979,7 @@ def main1(source_file, output_dir):
|
|||
ch = WADLHandler(abs_filename, api_ref)
|
||||
xml.sax.parse(file, ch)
|
||||
|
||||
# path item objects
|
||||
# Swagger: Path Item objects
|
||||
for urlpath, apis in ch.apis.items():
|
||||
output['paths'][urlpath] = {}
|
||||
for i in apis:
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
// validate_test.js
|
||||
// Validation of generated Swagger files
|
||||
// run 'mocha' in mocha dir
|
||||
|
||||
'use strict';
|
||||
var v = require('json-schema-remote');
|
||||
var fs = require('fs');
|
||||
|
||||
var converted_files = ["blockstorage-v1-swagger.json",
|
||||
"blockstorage-v2-swagger.json",
|
||||
"compute-v2.1-swagger.json",
|
||||
"data-processing-v1.1-swagger.json",
|
||||
"database-v1-swagger.json",
|
||||
"identity-admin-v2-swagger.json",
|
||||
"identity-extensions-v2-swagger.json",
|
||||
"identity-v2-swagger.json",
|
||||
"identity-v3-swagger.json",
|
||||
"image-v1-swagger.json",
|
||||
"image-v2-swagger.json",
|
||||
"networking-extensions-v2-swagger.json",
|
||||
"networking-v2-swagger.json",
|
||||
"objectstorage-v1-swagger.json"
|
||||
];
|
||||
|
||||
var i = 0;
|
||||
|
||||
function validate_file(filename, done) {
|
||||
var swagger_file = fs.readFileSync('../../../conversion_files_valid/'.concat(filename), 'utf8');
|
||||
var myswagger = JSON.parse(swagger_file);
|
||||
|
||||
v.validate(myswagger, 'http://swagger.io/v2/schema.json', function(err, isValid) {
|
||||
if (err) {
|
||||
done(err);
|
||||
console.error('Error occurred', err);
|
||||
}
|
||||
if (isValid) {
|
||||
console.log(filename.concat(" is valid."));
|
||||
done();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// mocha test suite and tests
|
||||
describe('Validate generated swagger:', function() {
|
||||
// identity and compute take some time to validate
|
||||
this.timeout(12000);
|
||||
var filen = '';
|
||||
|
||||
before(function() {
|
||||
// TODO, check whether preload call is actually pre-loading the schema
|
||||
v.preload({
|
||||
$schema:'http://swagger.io/v2/schema.json',
|
||||
id:'',
|
||||
type:''
|
||||
});
|
||||
});
|
||||
|
||||
beforeEach(function() {
|
||||
filen = converted_files[i];
|
||||
console.log(filen);
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
i += 1;
|
||||
});
|
||||
|
||||
var j = 0;
|
||||
for (j = i; j < converted_files.length; j++) {
|
||||
// Validate each file
|
||||
it(filen, function(done) {
|
||||
validate_file(filen, done);
|
||||
});
|
||||
}
|
||||
});
|
|
@ -27,5 +27,8 @@
|
|||
"pretest": "npm install",
|
||||
"test": "karma start karma.conf.js",
|
||||
"test-single-run": "karma start karma.conf.js --single-run"
|
||||
},
|
||||
"dependencies": {
|
||||
"json-schema-remote": "^0.1.5"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue