Skip to content

Commit 3e62efc

Browse files
committed
PR requested changes
PR requested changes
1 parent fa623d3 commit 3e62efc

File tree

9 files changed

+45
-15
lines changed

9 files changed

+45
-15
lines changed

lib/schemapack.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// This is the default collection name if one can't be inferred from the OpenAPI spec
44
const COLLECTION_NAME = 'Imported from OpenAPI 3.0',
55
{ getConcreteSchemaUtils } = require('./common/versionUtils.js'),
6-
{ convertSwaggerToOpenapi } = require('./swaggerUtils/swaggerToOpenapi.js'),
6+
{ convertToOAS30IfSwagger } = require('./swaggerUtils/swaggerToOpenapi.js'),
77
BROWSER = 'browser',
88
Ajv = require('ajv'),
99
addFormats = require('ajv-formats'),
@@ -63,6 +63,7 @@ class SchemaPack {
6363
json,
6464
specParseResult,
6565
isFolder = this.input.type === 'folder';
66+
6667
this.computedOptions = Object.assign({ isFolder }, this.computedOptions);
6768
if (!input) {
6869
return {
@@ -255,10 +256,12 @@ class SchemaPack {
255256
return callback(new OpenApiErr('The schema must be validated before attempting conversion'));
256257
}
257258

258-
convertSwaggerToOpenapi(concreteUtils, this.openapi, (error, newOpenapi) => {
259+
// We only convert if swagger is found otherwise this.openapi remains the same
260+
convertToOAS30IfSwagger(concreteUtils, this.openapi, (error, newOpenapi) => {
259261
if (error) {
260262
return callback(error);
261263
}
264+
262265
this.openapi = newOpenapi;
263266
// this cannot be attempted before validation
264267
specComponentsAndUtils = { concreteUtils };

lib/swaggerUtils/inputValidationSwagger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = {
1111
if (spec.swagger !== '2.0') {
1212
return {
1313
result: false,
14-
reason: 'The value of swagger field must be 2.0'
14+
reason: 'The value of "swagger" field must be 2.0'
1515
};
1616
}
1717
if (!spec.info) {

lib/swaggerUtils/schemaUtilsSwagger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
/**
1010
* Parses an OAS string/object as a YAML or JSON
1111
* @param {YAML/JSON} openApiSpec - The swagger 2.0 specification specified in either YAML or JSON
12-
* @param {object} options true if input type is folder
12+
* @param {Object} options computed process options
1313
* @returns {Object} - Contains the parsed JSON-version of the OAS spec, or an error
1414
* @no-unit-test
1515
*/

lib/swaggerUtils/swaggerToOpenapi.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ const Swagger2OpenAPI = require('swagger2openapi'),
22
{ isSwagger } = require('../common/versionUtils');
33

44
module.exports = {
5-
convertSwaggerToOpenapi: function(concreteUtils, parsedSwagger, convertExecution) {
5+
6+
/**
7+
* Converts a Swagger 2.0 API definition into an OpenAPI 3.0 specification
8+
* @param {Object} concreteUtils Concrete schema utils according to the specification version
9+
* @param {object} parsedSwagger Parsed Swagger spec
10+
* @param {function} convertExecution Function to perform the OAS-PM convertion after the Spec convertion
11+
* @return {Object} {error, newOpenapi} The new open api spec or error if there was an error in the process
12+
*/
13+
convertToOAS30IfSwagger: function(concreteUtils, parsedSwagger, convertExecution) {
614
if (isSwagger(concreteUtils.version)) {
715
Swagger2OpenAPI.convertObj(
816
parsedSwagger,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
title: Sample API
2-
description: API description in Markdown.
3-
version: 1.0.0
2+
description: API description in Markdown.
3+
version: 1.0.0

test/unit/swaggerSupport/schemaUtilsSwagger.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,39 @@ describe('parseSpec method', function () {
88
it('should return true and a parsed specification', function () {
99
let fileContent = fs.readFileSync(validSwaggerFolder + '/json/sampleswagger.json', 'utf8');
1010
const parsedSpec = concreteUtils.parseSpec(fileContent, {});
11+
1112
expect(parsedSpec.result).to.be.true;
1213
expect(parsedSpec.openapi.swagger).to.equal('2.0');
1314
});
1415

1516
it('should return false and info must have a title message', function () {
1617
let fileContent = fs.readFileSync(invalidSwaggerFolder + '/invalid_no_info_title.json', 'utf8');
1718
const parsedSpec = concreteUtils.parseSpec(fileContent, {});
19+
1820
expect(parsedSpec.result).to.be.false;
1921
expect(parsedSpec.reason).to.equal('Title, and version fields are required for the Info Object');
2022
});
2123

2224
it('should return false and swagger must have info object message', function () {
2325
let fileContent = fs.readFileSync(invalidSwaggerFolder + '/invalid_no_info.json', 'utf8');
2426
const parsedSpec = concreteUtils.parseSpec(fileContent, {});
27+
2528
expect(parsedSpec.result).to.be.false;
2629
expect(parsedSpec.reason).to.equal('The Swagger specification must have an \"info\" field');
2730
});
2831

2932
it('should return false and invalid version message', function () {
3033
let fileContent = fs.readFileSync(invalidSwaggerFolder + '/invalid_wrong_swagger_version.json', 'utf8');
3134
const parsedSpec = concreteUtils.parseSpec(fileContent, {});
35+
3236
expect(parsedSpec.result).to.be.false;
3337
expect(parsedSpec.reason).to.equal('The value of swagger field must be 2.0');
3438
});
3539

3640
it('should return false and no paths message', function () {
3741
let fileContent = fs.readFileSync(invalidSwaggerFolder + '/invalid_no_paths.json', 'utf8');
3842
const parsedSpec = concreteUtils.parseSpec(fileContent, {});
43+
3944
expect(parsedSpec.result).to.be.false;
4045
expect(parsedSpec.reason).to.equal('The Swagger specification must have a "paths" field');
4146
});

test/unit/swaggerToOpenapi.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe('Test swaggerToOpenapi method', function() {
1111
const fileSource = path.join(__dirname, SWAGGER_20_FOLDER_JSON + '/sampleswagger.json'),
1212
fileData = fs.readFileSync(fileSource, 'utf8'),
1313
parsedSpec = utils.parseSpec(fileData);
14+
1415
convertSwaggerToOpenapi(utils, parsedSpec.openapi, (error, openapi) => {
1516
expect(error).to.be.null;
1617
expect(openapi.openapi).to.be.equal('3.0.0');
@@ -21,6 +22,7 @@ describe('Test swaggerToOpenapi method', function() {
2122
const fileSource = path.join(__dirname, SWAGGER_20_INVALID_FOLDER_JSON + '/invalid_no_info.json'),
2223
fileData = fs.readFileSync(fileSource, 'utf8'),
2324
parsedSpec = utils.parseSpec(fileData);
25+
2426
convertSwaggerToOpenapi(utils, parsedSpec.openapi, (error, openapi) => {
2527
expect(error.message).to.be.equal('Unsupported swagger/OpenAPI version: undefined');
2628
expect(openapi).to.be.undefined;

test/unit/versionUtils.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ describe('filterOptionsByVersion method', function() {
268268
}
269269
],
270270
optionsFiltered = filterOptionsByVersion(optionsMock, '2.0');
271+
271272
expect(optionsFiltered).to.be.an('array');
272273
expect(optionsFiltered.map((option) => {
273274
return option.id;

test/unit/x20schemapack.test.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe('SchemaPack instance creation', function() {
1313
data: fileData
1414
},
1515
schemapack = new SchemaPack(input);
16+
1617
expect(schemapack);
1718
});
1819

@@ -23,6 +24,7 @@ describe('SchemaPack instance creation', function() {
2324
data: fileSource
2425
},
2526
schemapack = new SchemaPack(input);
27+
2628
expect(schemapack);
2729
});
2830
});
@@ -35,6 +37,7 @@ describe('getMetaData method', function() {
3537
data: fileSource
3638
},
3739
schemapack = new SchemaPack(input);
40+
3841
schemapack.getMetaData((error, result) => {
3942
expect(error).to.be.null;
4043
expect(result.result).to.be.true;
@@ -52,6 +55,7 @@ describe('Convert method', function() {
5255
data: fileData
5356
},
5457
schemapack = new SchemaPack(input);
58+
5559
schemapack.convert((error, result) => {
5660
expect(error).to.be.null;
5761
expect(result.result).to.be.true;
@@ -105,17 +109,12 @@ describe('Swagger 2.0 schemapack mergeAndValidate method', function() {
105109
});
106110

107111
it('Should merge correctly the files in folder - basicExample', function(done) {
108-
let folderPath = path.join(__dirname, '../data/swaggerMultifile/uberTest'),
112+
let folderPath = path.join(__dirname, '../data/swaggerMultifile/basicExample'),
109113
files = [],
110114
array = [
111115
{ fileName: folderPath + '/index.yaml' },
112-
{ fileName: folderPath + '/definitions/Activities.yaml' },
113-
{ fileName: folderPath + '/definitions/Activity.yaml' },
114-
{ fileName: folderPath + '/definitions/Error.yaml' },
115-
{ fileName: folderPath + '/definitions/Product.yaml' },
116-
{ fileName: folderPath + '/definitions/ProductList.yaml' },
117-
{ fileName: folderPath + '/definitions/Profile.yaml' },
118-
{ fileName: folderPath + '/definitions/PriceEstimate.yaml' }
116+
{ fileName: folderPath + '/info.yaml' },
117+
{ fileName: folderPath + '/paths.yaml' }
119118
];
120119

121120
array.forEach((item) => {
@@ -139,7 +138,9 @@ describe('Swagger 2.0 schemapack mergeAndValidate method', function() {
139138
expect(result.output.length).to.equal(1);
140139
expect(result.output[0].type).to.have.equal('collection');
141140
expect(result.output[0].data).to.have.property('info');
141+
expect(result.output[0].data.info.name).to.equal('Sample API');
142142
expect(result.output[0].data).to.have.property('item');
143+
expect(result.output[0].data.item[0].request.name).to.equal('Returns a list of users.');
143144
done();
144145
});
145146
}
@@ -186,6 +187,16 @@ describe('Swagger 2.0 schemapack mergeAndValidate method', function() {
186187
expect(result.output[0].type).to.have.equal('collection');
187188
expect(result.output[0].data).to.have.property('info');
188189
expect(result.output[0].data).to.have.property('item');
190+
expect(result.output[0].data.item[0].response[0].body).to.include('product_id');
191+
expect(result.output[0].data.item[0].response[0].body).to.include('description');
192+
expect(result.output[0].data.item[1].item[0].response[0].body).to.include('product_id');
193+
expect(result.output[0].data.item[1].item[0].response[0].body).to.include('currency_code');
194+
expect(result.output[0].data.item[2].response[0].body).to.include('first_name');
195+
expect(result.output[0].data.item[2].response[0].body).to.include('last_name');
196+
expect(result.output[0].data.item[3].response[0].body).to.include('offset');
197+
expect(result.output[0].data.item[3].response[0].body).to.include('limit');
198+
expect(result.output[0].data.item[0].response[1].body).to.include('code');
199+
expect(result.output[0].data.item[0].response[1].body).to.include('message');
189200
done();
190201
});
191202
}

0 commit comments

Comments
 (0)