Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for integer formats int32 and int64 #56

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
12 changes: 12 additions & 0 deletions lib/generators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions tests/request_mockgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 2 additions & 0 deletions tests/response_mockgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
2 changes: 2 additions & 0 deletions tests/responses_mockgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down