diff --git a/lib/generators/index.js b/lib/generators/index.js index 9a300dd..ce59298 100644 --- a/lib/generators/index.js +++ b/lib/generators/index.js @@ -90,6 +90,18 @@ const integerMock = schema => { return enumMock(schema); } + // Limit range for int32 format - it can still be overwritten by explicit range declaration + if (schema.format === 'int32') { + opts.min = -2147483648; + opts.max = 2147483647; + } + else if (schema.format === 'int64') { + // Note: We cannot use 9223372036854775807 and -9223372036854775808 range, + // they are rounded by JS and therefore unsafe to be used + opts.max = Number.MIN_SAFE_INTEGER; + opts.max = Number.MAX_SAFE_INTEGER; + } + if (Number.isInteger(schema.minimum)) { opts.min = (schema.exclusiveMinimum) ? schema.minimum + 1 : schema.minimum; } diff --git a/tests/request_mockgen.js b/tests/request_mockgen.js index cd805df..506ee6d 100644 --- a/tests/request_mockgen.js +++ b/tests/request_mockgen.js @@ -89,6 +89,7 @@ describe('Request Mock generator', () => { let order = request.body; Assert.ok(typeof order === 'object', 'OK value for body'); Assert.ok(Number.isInteger(order.id), 'order.id is integer'); + Assert.ok(order.id >= -9223372036854775000 && order.id <= 9223372036854775000, 'id has value in int64 range'); Assert.ok(Number.isInteger(order.petId), 'order.petId is integer'); Assert.ok(Number.isInteger(order.quantity), 'order.quantity is integer'); Assert.ok(typeof order.shipDate === 'string', 'order.shipDate is string'); diff --git a/tests/response_mockgen.js b/tests/response_mockgen.js index 5af1d85..a29dd26 100644 --- a/tests/response_mockgen.js +++ b/tests/response_mockgen.js @@ -89,6 +89,8 @@ describe('Response Mock generator', () => { Assert.ok(mock, 'Generated mock'); let resp = mock.responses; Assert.ok(resp, 'Generated response'); + Assert.ok(Number.isInteger(resp.code), 'response.code is integer'); + Assert.ok(resp.code >= -2147483648 && resp.code <= 2147483647, 'response.code ' + resp.code + ' has value in int32 range'); //TODO add asserts for pending props done(); }); diff --git a/tests/responses_mockgen.js b/tests/responses_mockgen.js index 0a1ab17..5c021e8 100644 --- a/tests/responses_mockgen.js +++ b/tests/responses_mockgen.js @@ -22,6 +22,7 @@ describe('Responses Mock generator', () => { Assert.ok(resp.hasOwnProperty('404'), 'Generated 404 response'); let successResp = resp['200']; Assert.ok(Number.isInteger(successResp.id), 'id is integer'); + Assert.ok(successResp.id >= -9223372036854775000 && successResp.id <= 9223372036854775000, 'id has value in int64 range'); Assert.ok(Number.isInteger(successResp.petId), 'petId is integer'); Assert.ok(Number.isInteger(successResp.quantity), 'quantity is integer'); Assert.ok(typeof successResp.shipDate === 'string', 'shipDate is string'); @@ -50,6 +51,7 @@ describe('Responses Mock generator', () => { let pet = resp[0]; Assert.ok(pet, 'Ok Pet response'); Assert.ok(Number.isInteger(pet.id), 'id is integer'); + Assert.ok(pet.id >= -9223372036854775000 && pet.id <= 9223372036854775000, 'id has value in int64 range'); done(); }).catch(err => { Assert.ok(!err, 'No error');