added more tests

This commit is contained in:
Dhroov7
2019-11-15 14:46:39 +05:30
parent 40a646e981
commit bee0e8b6f3
4 changed files with 94 additions and 33 deletions

View File

@@ -754,7 +754,7 @@ module.exports = {
resolveTo = '';
if (bodyObj.example && (this.options.requestParametersResolution === 'example' ||
this.options.exampleParametersResolution === 'example')) {
this.options.exampleParametersResolution === 'example' || !bodyObj.schema)) {
if (bodyObj.example.hasOwnProperty('$ref')) {
bodyObj.example = this.getRefObject(bodyObj.example.$ref);
if (this.getHeaderFamily(contentType) === HEADER_TYPE.JSON) {
@@ -774,7 +774,7 @@ module.exports = {
}
}
else if (bodyObj.examples && (this.options.requestParametersResolution === 'example' ||
this.options.exampleParametersResolution === 'example')) {
this.options.exampleParametersResolution === 'example' || !bodyObj.schema)) {
// take one of the examples as the body and not all
bodyData = this.getExampleData(bodyObj.examples);
}

View File

@@ -9,30 +9,30 @@
"post": {
"parameters": [
{
"name": "limit",
"in": "header",
"description": "How many items to return at one time (max 100)",
"required": false,
"schema": {
"type": "integer",
"name": "limit",
"in": "header",
"description": "How many items to return at one time (max 100)",
"required": false,
"schema": {
"type": "integer",
"format": "int32",
"example": "heading example"
}
"example": "header example"
}
},
{
"name": "variable2",
"in": "query",
"description": "another random variable",
"style": "spaceDelimited",
"schema": {
"type": "array",
"items": {
"type": "integer",
"name": "variable2",
"in": "query",
"description": "another random variable",
"style": "spaceDelimited",
"schema": {
"type": "array",
"items": {
"type": "integer",
"format": "int64",
"example": "queryParamExample"
}
}
}
}
}
}
],
"requestBody": {
"content": {

View File

@@ -0,0 +1,35 @@
{
"openapi": "3.0.0",
"info": {
"title": "Example",
"version": "1.0.0"
},
"paths": {
"/pet": {
"post": {
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"a": {
"type": "string"
},
"b": {
"type": "string"
}
}
}
}
}
},
"responses": {
"201": {
"description": "subscription successfully created"
}
}
}
}
}
}

View File

@@ -19,7 +19,8 @@ describe('CONVERT FUNCTION TESTS ', function() {
multipleFoldersSpec = path.join(__dirname, VALID_OPENAPI_PATH + '/multiple_folder_problem.json'),
multipleFoldersSpec1 = path.join(__dirname, VALID_OPENAPI_PATH + '/multiple_folder_problem1.json'),
multipleFoldersSpec2 = path.join(__dirname, VALID_OPENAPI_PATH + '/multiple_folder_problem2.json'),
examplesInBodySpec = path.join(__dirname, VALID_OPENAPI_PATH + '/examples_in_body.json');
examplesInSchemaSpec = path.join(__dirname, VALID_OPENAPI_PATH + '/example_in_schema.json'),
schemaWithoutExampleSpec = path.join(__dirname, VALID_OPENAPI_PATH + '/example_not_present.json');
it('Should generate collection conforming to schema for and fail if not valid ' +
testSpec, function(done) {
@@ -162,17 +163,42 @@ describe('CONVERT FUNCTION TESTS ', function() {
done();
});
});
it('[Github #108]- Use example values instead of faking schema' +
examplesInBodySpec, function(done) {
Converter.convert({ type: 'file', data: examplesInBodySpec },
{ schemaFaker: true, requestParametersResolution: 'schema', exampleParametersResolution: 'example' },
(err, conversionResult) => {
expect(conversionResult.output[0].data.item[0].request.body.raw).to
.equal('{\n "a": "<string>",\n "b": "<string>"\n}');
expect(conversionResult.output[0].data.item[0].response[0].originalRequest.body.raw).to
.equal('{\n "a": "example-a",\n "b": "example-b"\n}');
done();
});
describe('[Github #108]- Use example values instead of faking schema', function() {
it('Set an option for choosing schema faking for root request and example for example request' +
examplesInSchemaSpec, function(done) {
Converter.convert({ type: 'file', data: examplesInSchemaSpec },
{ schemaFaker: true, requestParametersResolution: 'schema', exampleParametersResolution: 'example' },
(err, conversionResult) => {
let rootRequest = conversionResult.output[0].data.item[0].request,
exampleRequest = conversionResult.output[0].data.item[0].response[0].originalRequest;
// Request body
expect(rootRequest.body.raw).to
.equal('{\n "a": "<string>",\n "b": "<string>"\n}');
expect(exampleRequest.body.raw).to
.equal('{\n "a": "example-a",\n "b": "example-b"\n}');
// Request header
expect(rootRequest.header[0].value).to.equal('<integer>');
expect(exampleRequest.header[0].value).to.equal('header example');
// Request query parameters
expect(rootRequest.url.query[0].value).to.equal('<long> <long>');
expect(exampleRequest.url.query[0].value).to.equal('queryParamExample queryParamExample');
done();
});
});
it('Fallback to schema if the example is not present in the spec and the option is set to example' +
schemaWithoutExampleSpec, function(done) {
Converter.convert({ type: 'file', data: schemaWithoutExampleSpec },
{ schemaFaker: true, requestParametersResolution: 'example', exampleParametersResolution: 'example' },
(err, conversionResult) => {
let rootRequest = conversionResult.output[0].data.item[0].request,
exampleRequest = conversionResult.output[0].data.item[0].response[0].originalRequest;
expect(exampleRequest.body.raw).to
.equal('{\n "a": "<string>",\n "b": "<string>"\n}');
expect(rootRequest.body.raw).to
.equal('{\n "a": "<string>",\n "b": "<string>"\n}');
done();
});
});
});
});
describe('for invalid requestNameSource option', function() {