Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

Get mock values from schema.example (if no default value) #304

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions middleware/swagger-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,20 @@ var getMockValue = function (version, schema) {

switch (type) {
case 'array':
value = [getMockValue(version, _.isArray(schema.items) ? schema.items[0] : schema.items)];

var minItems = schema.minItems || 1;
value = [];
for (var i = 0; i < minItems; ++i) {
value.push(getMockValue(version, _.isArray(schema.items) ? schema.items[0] : schema.items));
}
break;

case 'boolean':
if (version === '1.2' && !_.isUndefined(schema.defaultValue)) {
value = schema.defaultValue;
} else if (version === '2.0' && !_.isUndefined(schema.default)) {
value = schema.default;
} else if (version === '2.0' && !_.isUndefined(schema.example)) {
value = schema.example;
} else if (_.isArray(schema.enum)) {
value = schema.enum[0];
} else {
Expand All @@ -145,6 +150,8 @@ var getMockValue = function (version, schema) {
value = schema.defaultValue;
} else if (version === '2.0' && !_.isUndefined(schema.default)) {
value = schema.default;
} else if (version === '2.0' && !_.isUndefined(schema.example)) {
value = schema.example;
} else if (_.isArray(schema.enum)) {
value = schema.enum[0];
} else {
Expand Down Expand Up @@ -180,6 +187,8 @@ var getMockValue = function (version, schema) {
value = schema.defaultValue;
} else if (version === '2.0' && !_.isUndefined(schema.default)) {
value = schema.default;
} else if (version === '2.0' && !_.isUndefined(schema.example)) {
value = schema.example;
} else if (_.isArray(schema.enum)) {
value = schema.enum[0];
} else {
Expand All @@ -200,6 +209,8 @@ var getMockValue = function (version, schema) {
value = schema.defaultValue;
} else if (version === '2.0' && !_.isUndefined(schema.default)) {
value = schema.default;
} else if (version === '2.0' && !_.isUndefined(schema.example)) {
value = schema.example;
} else if (_.isArray(schema.enum)) {
value = schema.enum[0];
} else {
Expand Down
215 changes: 215 additions & 0 deletions test/2.0/test-middleware-swagger-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,221 @@ describe('Swagger Router Middleware v2.0', function () {
done();
});
});

describe('get mock values from default then example', function () {
it('should return mock string from default over example', function (done) {
var cPetStoreJson = _.cloneDeep(petStoreJson);

cPetStoreJson.paths['/pets/{id}'].get.responses['200'].schema = {
type: 'string',
pattern: '^([A-Za-z0-9]*)$',
example: 'exampleString',
default: 'defaultString'
};

helpers.createServer([cPetStoreJson], {
swaggerRouterOptions: {
useStubs: true
}
}, function (app) {
request(app)
.get('/api/pets/1')
.expect(200)
.end(helpers.expectContent(JSON.stringify('defaultString'), done));
});
});

it('should return mock string from example if no default', function (done) {
var cPetStoreJson = _.cloneDeep(petStoreJson);

cPetStoreJson.paths['/pets/{id}'].get.responses['200'].schema = {
type: 'string',
pattern: '^([A-Za-z0-9]*)$',
example: 'exampleString'
};

helpers.createServer([cPetStoreJson], {
swaggerRouterOptions: {
useStubs: true
}
}, function (app) {
request(app)
.get('/api/pets/1')
.expect(200)
.end(helpers.expectContent(JSON.stringify('exampleString'), done));
});
});

it('should return mock number from default over example', function (done) {
var cPetStoreJson = _.cloneDeep(petStoreJson);

cPetStoreJson.paths['/pets/{id}'].get.responses['200'].schema = {
type: 'number',
example: 1.01,
default: 2.02
};

helpers.createServer([cPetStoreJson], {
swaggerRouterOptions: {
useStubs: true
}
}, function (app) {
request(app)
.get('/api/pets/1')
.expect(200)
.end(helpers.expectContent(JSON.stringify(2.02), done));
});
});

it('should return mock number from example if no default', function (done) {
var cPetStoreJson = _.cloneDeep(petStoreJson);

cPetStoreJson.paths['/pets/{id}'].get.responses['200'].schema = {
type: 'number',
example: 1.01
};

helpers.createServer([cPetStoreJson], {
swaggerRouterOptions: {
useStubs: true
}
}, function (app) {
request(app)
.get('/api/pets/1')
.expect(200)
.end(helpers.expectContent(JSON.stringify(1.01), done));
});
});

it('should return mock integer from default over example', function (done) {
var cPetStoreJson = _.cloneDeep(petStoreJson);

cPetStoreJson.paths['/pets/{id}'].get.responses['200'].schema = {
type: 'integer',
example: 1,
default: 2
};

helpers.createServer([cPetStoreJson], {
swaggerRouterOptions: {
useStubs: true
}
}, function (app) {
request(app)
.get('/api/pets/1')
.expect(200)
.end(helpers.expectContent(JSON.stringify(2), done));
});
});

it('should return mock integer from example if no default', function (done) {
var cPetStoreJson = _.cloneDeep(petStoreJson);

cPetStoreJson.paths['/pets/{id}'].get.responses['200'].schema = {
type: 'integer',
example: 1
};

helpers.createServer([cPetStoreJson], {
swaggerRouterOptions: {
useStubs: true
}
}, function (app) {
request(app)
.get('/api/pets/1')
.expect(200)
.end(helpers.expectContent(JSON.stringify(1), done));
});
});

it('should return mock boolean from default over example', function (done) {
var cPetStoreJson = _.cloneDeep(petStoreJson);

cPetStoreJson.paths['/pets/{id}'].get.responses['200'].schema = {
type: 'boolean',
example: false,
default: true
};

helpers.createServer([cPetStoreJson], {
swaggerRouterOptions: {
useStubs: true
}
}, function (app) {
request(app)
.get('/api/pets/1')
.expect(200)
.end(helpers.expectContent(JSON.stringify(true), done));
});
});

it('should return mock boolean from example if no default', function (done) {
var cPetStoreJson = _.cloneDeep(petStoreJson);

cPetStoreJson.paths['/pets/{id}'].get.responses['200'].schema = {
type: 'boolean',
example: true
};

helpers.createServer([cPetStoreJson], {
swaggerRouterOptions: {
useStubs: true
}
}, function (app) {
request(app)
.get('/api/pets/1')
.expect(200)
.end(helpers.expectContent(JSON.stringify(true), done));
});
});
});

describe('mock arrays', function () {
it('should return array of specified min length', function (done) {
var cPetStoreJson = _.cloneDeep(petStoreJson);

cPetStoreJson.paths['/pets/{id}'].get.responses['200'].schema = {
type: 'array',
items: [{type: 'number'}],
minItems: 2,
maxItems: 3
};

helpers.createServer([cPetStoreJson], {
swaggerRouterOptions: {
useStubs: true
}
}, function (app) {
request(app)
.get('/api/pets/1')
.expect(200)
.end(helpers.expectContent(JSON.stringify([1,1]), done));
});
});

it('should return array of length 1 if not specified', function (done) {
var cPetStoreJson = _.cloneDeep(petStoreJson);

cPetStoreJson.paths['/pets/{id}'].get.responses['200'].schema = {
type: 'array',
items: [{type: 'number'}],
maxItems: 3
};

helpers.createServer([cPetStoreJson], {
swaggerRouterOptions: {
useStubs: true
}
}, function (app) {
request(app)
.get('/api/pets/1')
.expect(200)
.end(helpers.expectContent(JSON.stringify([1]), done));
});
});


});

describe('issues', function () {
it('should handle uncaught exceptions (Issue 123)', function (done) {
Expand Down