Skip to content

Commit b54b8fb

Browse files
committed
AB-289-added tests and fixes
1 parent aa645c1 commit b54b8fb

File tree

4 files changed

+185
-7
lines changed

4 files changed

+185
-7
lines changed

libV2/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,24 @@ module.exports = {
220220
if (!_.isEmpty(collection.variable)) {
221221
collection.variable = _.uniqBy(collection.variable, 'key');
222222
}
223-
223+
if (context.enableTypeFetching) {
224+
return cb(null, {
225+
result: true,
226+
output: [{
227+
type: 'collection',
228+
data: collection
229+
}],
230+
analytics: this.analytics || {},
231+
extractedTypes: finalExtractedTypesList || []
232+
});
233+
}
224234
return cb(null, {
225235
result: true,
226236
output: [{
227237
type: 'collection',
228238
data: collection
229239
}],
230-
analytics: this.analytics || {},
231-
extractedTypes: context.enableTypeFetching ? finalExtractedTypesList : []
240+
analytics: this.analytics || {}
232241
});
233242
},
234243

libV2/schemaUtils.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -767,11 +767,24 @@ let QUERYPARAM = 'query',
767767
let res = processSchema(prop).properties;
768768
propertyDetails.properties = res;
769769
}
770+
else if (prop.type === 'array' && prop.items) {
771+
propertyDetails.items = processSchema(prop.items);
772+
}
770773

771774
schemaDetails.properties[key] = propertyDetails;
772775
}
773776
return schemaDetails;
774777
}
778+
else if (resolvedSchema.type === 'array' && resolvedSchema.items) {
779+
// Handle array type schema
780+
const arrayDetails = {
781+
type: resolvedSchema.type || 'unknown',
782+
items: processSchema(resolvedSchema.items)
783+
};
784+
if (resolvedSchema.minItems !== undefined) { arrayDetails.minItems = resolvedSchema.minItems; }
785+
if (resolvedSchema.maxItems !== undefined) { arrayDetails.maxItems = resolvedSchema.maxItems; }
786+
return arrayDetails;
787+
}
775788
return {
776789
type: resolvedSchema.type || 'unknown'
777790
};
@@ -2615,10 +2628,8 @@ module.exports = {
26152628
resolvedExampleTypes
26162629
} = resolveResponseForPostmanRequest(context, operationItem[method], request);
26172630

2618-
methodPath = method + path;
26192631
requestBlock = { request: unifiedRequestTypes, response: resolvedExampleTypes };
2620-
requestObj = { [methodPath]: requestBlock };
2621-
extractedTypesList.push(requestObj);
2632+
extractedTypesList.push(requestBlock);
26222633

26232634
// add accept header if found and not present already
26242635
if (!_.isEmpty(acceptHeader)) {

test/unit/convertV2.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ describe('The convert v2 Function', function() {
205205
tooManyRefs, function(done) {
206206
var openapi = fs.readFileSync(tooManyRefs, 'utf8');
207207
Converter.convertV2({ type: 'string', data: openapi }, { schemaFaker: true }, (err, conversionResult) => {
208-
209208
expect(err).to.be.null;
210209
expect(conversionResult.result).to.equal(true);
210+
expect(conversionResult).to.not.have.property('extractedTypes');
211211
expect(conversionResult.output.length).to.equal(1);
212212
expect(conversionResult.output[0].type).to.equal('collection');
213213
expect(conversionResult.output[0].data).to.have.property('info');

test/unit/convertV2WithTypes.test.js

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/* eslint-disable one-var */
2+
const expect = require('chai').expect,
3+
Converter = require('../../index.js'),
4+
fs = require('fs'),
5+
path = require('path'),
6+
VALID_OPENAPI_PATH = '../data/valid_openapi',
7+
Ajv = require('ajv'),
8+
testSpec = path.join(__dirname, VALID_OPENAPI_PATH + '/test.json'),
9+
testSpec1 = path.join(__dirname, VALID_OPENAPI_PATH + '/test1.json'),
10+
readOnlyNestedSpec =
11+
path.join(__dirname, VALID_OPENAPI_PATH, '/readOnlyNested.json'),
12+
ajv = new Ajv({ allErrors: true, strict: false }),
13+
transformSchema = (schema) => {
14+
const properties = schema.properties,
15+
rest = Object.keys(schema)
16+
.filter((key) => { return key !== 'properties'; })
17+
.reduce((acc, key) => {
18+
acc[key] = schema[key];
19+
return acc;
20+
}, {}),
21+
22+
transformedProperties = Object.entries(properties).reduce(
23+
(acc, [key, value]) => {
24+
acc[key] = {
25+
type: value.type,
26+
enum: value.enum !== null ? value.enum : undefined,
27+
minLength: value.minLength !== null ? value.minLength : undefined,
28+
maxLength: value.maxLength !== null ? value.maxLength : undefined,
29+
minimum: value.minimum !== null ? value.minimum : undefined,
30+
maximum: value.maximum !== null ? value.maximum : undefined,
31+
pattern: value.pattern !== null ? value.pattern : undefined,
32+
format: value.format !== null ? value.format : undefined
33+
};
34+
return acc;
35+
},
36+
{}
37+
),
38+
39+
40+
transformedObject = Object.assign({}, rest, { properties: transformedProperties });
41+
42+
return transformedObject;
43+
};
44+
45+
46+
describe('convertV2WithTypes should generate collection confirming to collection schema', function() {
47+
48+
it('Should generate collection conforming to schema for and fail if not valid ' +
49+
testSpec, function(done) {
50+
var openapi = fs.readFileSync(testSpec, 'utf8');
51+
Converter.convertV2WithTypes({ type: 'string', data: openapi }, { schemaFaker: true }, (err, conversionResult) => {
52+
expect(err).to.be.null;
53+
expect(conversionResult.result).to.equal(true);
54+
expect(conversionResult.output.length).to.equal(1);
55+
expect(conversionResult.output[0].type).to.equal('collection');
56+
expect(conversionResult.output[0].data).to.have.property('info');
57+
expect(conversionResult.output[0].data).to.have.property('item');
58+
done();
59+
});
60+
});
61+
62+
it('Should generate collection conforming to schema for and fail if not valid ' +
63+
testSpec1, function(done) {
64+
Converter.convertV2WithTypes(
65+
{ type: 'file', data: testSpec1 }, { requestNameSource: 'url' }, (err, conversionResult) => {
66+
expect(err).to.be.null;
67+
expect(conversionResult.result).to.equal(true);
68+
expect(conversionResult.output.length).to.equal(1);
69+
expect(conversionResult.output[0].type).to.equal('collection');
70+
expect(conversionResult.output[0].data).to.have.property('info');
71+
expect(conversionResult.output[0].data).to.have.property('item');
72+
73+
done();
74+
});
75+
});
76+
});
77+
78+
79+
describe('convertV2WithTypes', function() {
80+
it('should contain extracted types' + testSpec1, function () {
81+
Converter.convertV2WithTypes(
82+
{ type: 'file', data: testSpec1 }, { requestNameSource: 'url' }, (err, conversionResult) => {
83+
expect(err).to.be.null;
84+
expect(conversionResult.result).to.equal(true);
85+
expect(conversionResult.extractedTypes).to.not.be.undefined;
86+
expect(conversionResult.extractedTypes.length).to.not.equal(0);
87+
}
88+
);
89+
});
90+
91+
it('should validate the schema' + testSpec1, function() {
92+
const example = {
93+
code: 200,
94+
message: 'Success'
95+
};
96+
Converter.convertV2WithTypes(
97+
{ type: 'file', data: testSpec1 }, { requestNameSource: 'url' }, (err, conversionResult) => {
98+
99+
expect(err).to.be.null;
100+
expect(conversionResult.extractedTypes).to.be.an('array').that.is.not.empty;
101+
const element = conversionResult.extractedTypes[0];
102+
103+
expect(element).to.be.an('object').that.includes.keys('request');
104+
expect(element).to.be.an('object').that.includes.keys('response');
105+
const { response } = element;
106+
expect(response).to.be.an('object').that.is.not.empty;
107+
const [key, value] = Object.entries(response)[1];
108+
expect(key).to.be.a('string');
109+
const schema = JSON.parse(value.body),
110+
transformedSchema = transformSchema(schema),
111+
validate = ajv.compile(transformedSchema),
112+
valid = validate(example);
113+
114+
expect(value).to.have.property('body').that.is.a('string');
115+
116+
117+
expect(valid, `Validation failed for key: ${key} with errors: ${JSON.stringify(validate.errors)}`).to.be.true;
118+
});
119+
});
120+
121+
it('should resolve nested array and object schema types correctly in extractedTypes', function(done) {
122+
const example = {
123+
name: 'Buddy',
124+
pet: {
125+
id: 123,
126+
name: 'Charlie',
127+
address: {
128+
addressCode: {
129+
code: 'A123'
130+
},
131+
city: 'New York'
132+
}
133+
}
134+
},
135+
openapi = fs.readFileSync(readOnlyNestedSpec, 'utf8'),
136+
options = { schemaFaker: true, exampleParametersResolution: 'schema' };
137+
138+
Converter.convertV2WithTypes({ type: 'string', data: openapi }, options, (err, conversionResult) => {
139+
expect(err).to.be.null;
140+
expect(conversionResult.extractedTypes).to.be.an('array').that.is.not.empty;
141+
142+
// Validate the first extracted type
143+
const element = conversionResult.extractedTypes[0];
144+
const { response } = element;
145+
146+
// Get the schema from the response
147+
const [key, value] = Object.entries(response)[0];
148+
expect(value).to.have.property('body').that.is.a('string');
149+
150+
const schema = JSON.parse(value.body),
151+
transformedSchema = transformSchema(schema),
152+
validate = ajv.compile(transformedSchema),
153+
valid = validate(example);
154+
expect(valid, `Validation failed for key: ${key} with errors: ${JSON.stringify(validate.errors)}`).to.be.true;
155+
done();
156+
});
157+
});
158+
});

0 commit comments

Comments
 (0)