Skip to content

Commit

Permalink
Merge pull request #22 from subii/enum
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
subeeshcbabu authored Sep 9, 2016
2 parents 5c405ba + 9fe71de commit 8e70d85
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Unreleased
# v0.0.4
## Features
- #11 support additional format options as per json schema (uri, hostname, ipv4, ipv6)

## Bugfixes
- Fix the `maxItems` bug and add additional test cases - #16
- Fix the issue - enum of type integer is not handled properly #21
- Fix the issue - if example is wrong the mock generated is not as per schema #23

# v0.0.3

Expand Down
42 changes: 28 additions & 14 deletions lib/generators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,12 @@ function mock(schema) {
var mock;
if (schema) {
var type = schema.type || findType(schema);
var example = schema.examples || schema.example;
/**
* Use examples as Mock if provided
* Get the mock generator from the `type` of the schema
*/
if (example) {
mock = example;
} else {
/**
* Get the mock generator from the `type` of the schema
*/
var generator = Generators[type];
if (generator) {
mock = generator.call(null, schema);
}
var generator = Generators[type];
if (generator) {
mock = generator.call(null, schema);
}
}
return mock;
Expand Down Expand Up @@ -99,6 +91,14 @@ function arrayMock(schema) {
function integerMock(schema) {
var opts = {};
var intmock;

/**
* If `enum` is defined for the property
*/
if (schema.enum && schema.enum.length > 0) {
return enumMock(schema);
}

if (Util.isInteger(schema.minimum)) {
opts.min = (schema.exclusiveMinimum) ? schema.minimum : schema.minimum + 1;
}
Expand Down Expand Up @@ -127,6 +127,14 @@ function integerMock(schema) {
function numberMock(schema) {
var opts = {};
var nummock;

/**
* If `enum` is defined for the property
*/
if (schema.enum && schema.enum.length > 0) {
return enumMock(schema);
}

if (schema.minimum) {
opts.min = (schema.exclusiveMinimum) ? schema.minimum : schema.minimum + 0.1;
}
Expand All @@ -147,7 +155,13 @@ function numberMock(schema) {
return nummock;
}

function booleanMock() {
function booleanMock(schema) {
/**
* If `enum` is defined for the property
*/
if (schema.enum && schema.enum.length > 0) {
return enumMock(schema);
}
return Chance.bool();
}
/**
Expand Down Expand Up @@ -205,7 +219,7 @@ function fileMock() {
//(This is not a complete list or full proof solution)
function findType(schema) {
var type = 'object';// Use 'object' as the default type
if (schema.enum || schema.pattern) {
if (schema.pattern) {
type = 'string';
} else if (schema.items) {
type = 'array';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swagmock",
"version": "0.0.3",
"version": "0.0.4",
"description": "Mock data generator for swagger api",
"main": "lib/index.js",
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions tests/fixture/petstore.json
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,11 @@
},
"quantity": {
"type": "integer",
"enum": [
1,
3,
5
],
"format": "int32"
},
"shipDate": {
Expand Down
2 changes: 1 addition & 1 deletion tests/response_mockgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Response Mock generator', function () {
Assert.ok(resp, 'Generated response');
Assert.ok(Number.isInteger(resp.id), 'id is integer');
Assert.ok(Number.isInteger(resp.petId), 'petId is integer');
Assert.ok(Number.isInteger(resp.quantity), 'quantity is integer');
Assert.ok([ 1, 3, 5 ].indexOf(resp.quantity) != -1, 'quantity is integer enum');
Assert.ok(typeof resp.shipDate === 'string', 'shipDate is string');
Assert.ok(['placed','approved','delivered'].indexOf(resp.status) !== -1, 'status is enum');
Assert.ok(typeof resp.complete === 'boolean', 'complete is boolean');
Expand Down

0 comments on commit 8e70d85

Please sign in to comment.