From 71375399cdd91c7e4197a18162853d92ece8bc94 Mon Sep 17 00:00:00 2001 From: "Michal Dvorak (cen38289)" Date: Wed, 4 Jul 2018 15:36:14 +0200 Subject: [PATCH 1/2] Add support for integer format int32 Limit range of generated numbers for this integer subtype --- lib/generators/index.js | 6 ++++++ tests/response_mockgen.js | 2 ++ 2 files changed, 8 insertions(+) diff --git a/lib/generators/index.js b/lib/generators/index.js index 9a300dd..d2d71b9 100644 --- a/lib/generators/index.js +++ b/lib/generators/index.js @@ -90,6 +90,12 @@ 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; + } + if (Number.isInteger(schema.minimum)) { opts.min = (schema.exclusiveMinimum) ? schema.minimum + 1 : schema.minimum; } diff --git a/tests/response_mockgen.js b/tests/response_mockgen.js index 5af1d85..3a56e29 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 is has value in int32 range'); //TODO add asserts for pending props done(); }); From 624ea8aa887112f26a6b1ef41ac204375d76aa2b Mon Sep 17 00:00:00 2001 From: "Michal Dvorak (cen38289)" Date: Wed, 4 Jul 2018 16:11:57 +0200 Subject: [PATCH 2/2] Add support for integer format int64 --- lib/generators/index.js | 6 ++++++ tests/request_mockgen.js | 1 + tests/response_mockgen.js | 2 +- tests/responses_mockgen.js | 2 ++ 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/generators/index.js b/lib/generators/index.js index d2d71b9..ce59298 100644 --- a/lib/generators/index.js +++ b/lib/generators/index.js @@ -95,6 +95,12 @@ const integerMock = schema => { 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 3a56e29..a29dd26 100644 --- a/tests/response_mockgen.js +++ b/tests/response_mockgen.js @@ -90,7 +90,7 @@ describe('Response Mock generator', () => { 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 is has value in int32 range'); + 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');