Skip to content

Commit

Permalink
fix: make number default to float if format is float or double (#154)
Browse files Browse the repository at this point in the history
* fix: make number default to float

* review
  • Loading branch information
kennethaasan authored Dec 4, 2023
1 parent d143da1 commit bad48ef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/samplers/number.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export function sampleNumber(schema) {
let res = 0;
if (typeof schema.exclusiveMinimum === 'boolean' || typeof schema.exclusiveMaximum === 'boolean') { //legacy support for jsonschema draft 4 of exclusiveMaximum/exclusiveMinimum as booleans
if (schema.type === 'number' && (schema.format === 'float' || schema.format === 'double')) {
res = 0.1;
}
if (typeof schema.exclusiveMinimum === 'boolean' || typeof schema.exclusiveMaximum === 'boolean') { //legacy support for jsonschema draft 4 of exclusiveMaximum/exclusiveMinimum as booleans
if (schema.maximum && schema.minimum) {
res = schema.exclusiveMinimum ? Math.floor(schema.minimum) + 1 : schema.minimum;
if ((schema.exclusiveMaximum && res >= schema.maximum) ||
Expand Down
20 changes: 20 additions & 0 deletions test/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ describe('Integration', function() {
expect(result).to.deep.equal(expected);
});

it('should sample number with format float', function() {
schema = {
'type': 'number',
format: 'float'
};
result = OpenAPISampler.sample(schema);
expected = 0.1;
expect(result).to.deep.equal(expected);
});

it('should sample number with format double', function() {
schema = {
'type': 'number',
format: 'double'
};
result = OpenAPISampler.sample(schema);
expected = 0.1;
expect(result).to.deep.equal(expected);
});

it('should sample boolean', function() {
schema = {
'type': 'boolean'
Expand Down

0 comments on commit bad48ef

Please sign in to comment.