Skip to content

Commit

Permalink
(fix) #367 - handle negative floats
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimascio committed Sep 14, 2020
1 parent a6992d4 commit a7e5d4d
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/framework/ajv/formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const maxInt64 = 2 ** 63 - 1;
const minInt64 = (-2) ** 63;

const maxFloat = (2 - 2 ** -23) * 2 ** 127;
const minFloat = 2 ** -126;
const minPosFloat = 2 ** -126;
const minFloat = -1 * maxFloat;
const maxNegFloat = -1 * minPosFloat;

const alwaysTrue = () => true;
const base64regExp = /^[A-Za-z0-9+/]*(=|==)?$/;
Expand All @@ -20,7 +22,7 @@ export const formats = {
type: 'number',
},
float: {
validate: i => typeof i === 'number' && (i <= maxFloat && i >= minFloat),
validate: i => typeof i === 'number' && (i === 0 || (i <= maxFloat && i >= minPosFloat) || (i >= minFloat && i <= maxNegFloat)),
type: 'number',
},
double: {
Expand Down
80 changes: 80 additions & 0 deletions test/formats.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import * as path from 'path';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';

const apiSpecPath = path.join('test', 'resources', 'formats.yaml');

describe('path params', () => {
let app = null;

before(async () => {
// set up express app
app = await createApp(
{ apiSpec: apiSpecPath },
3005,
(app) => {
app.get(`${app.basePath}/fees`, (req, res) => {
res.json([req.query]);
});
app.use((err, req, res, next) => {
console.error(err);
res.status(err.status ?? 500).json({
message: err.message,
code: err.status ?? 500,
});
});
},
false,
);
return app;
});

after(() => {
app.server.close();
});

// TODO add tests for min and max float values
// TODO add tests for min max float and max min float

it('should handle float type with negative', async () =>
request(app)
.get(`${app.basePath}/fees`)
.query({
id: 10,
amount: -10.0,
})
.expect(200)
.then((r) => {
const body = r.body;
console.log(body);
expect(body[0]).to.have.property('amount').that.equals(-10.0);
}));

it('should handle float type with 0 value', async () =>
request(app)
.get(`${app.basePath}/fees`)
.query({
id: 10,
amount: -0.0,
})
.expect(200)
.then((r) => {
const body = r.body;
console.log(body);
expect(body[0]).to.have.property('amount').that.equals(0.0);
}));
it('should handle float type with positive value', async () =>
request(app)
.get(`${app.basePath}/fees`)
.query({
id: 10,
amount: 10.0,
})
.expect(200)
.then((r) => {
const body = r.body;
console.log(body);
expect(body[0]).to.have.property('amount').that.equals(10.0);
}));
});
26 changes: 26 additions & 0 deletions test/resources/formats.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
openapi: 3.0.0
info:
version: 1.0.0
title: test bug OpenApiValidator
servers:
- url: '/v1'
paths:
/fees:
get:
parameters:
- name: id
in: query
required: true
schema:
type: number
- name: amount
in: query
required: true
schema:
type: number
format: float
minimum: -10.0
responses:
'200':
description: response

0 comments on commit a7e5d4d

Please sign in to comment.