Repository: fastify/fast-json-stringify (JavaScript)

Issue:
Integer properties are always stringified as 0
### Prerequisites

- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure it has not already been reported


### Fastify version

3.x

### Plugin version

2.7.5

### Node.js version

14.x

### Operating system

macOS

### Operating system version (i.e. 20.04, 11.3, 10)

11.3

### Description

Version 2.7.5 introduced a bug where an integer property always ends up being stringified even if the schema doesn't provide a default value for it.

### Steps to Reproduce

```
const fastJson = require('fast-json-stringify')
const stringify = fastJson({
  title: 'Example Schema',
  type: 'object',
  properties: {
    age: {
      type: 'integer'
    }
  }
})

console.log(stringify({}))
```

### Expected Behavior

I'd expect an empty object as string: `{}` but I get `{ age: 0 }` instead.

Version 2.7.4 and prior ones work as expected.


Discussion:
I think this is an unwanted regression introduced by https://github.com/fastify/fast-json-stringify/pull/332. cc @Eomm 

Would you like to send a Pull Request to address this issue? Remember to add unit tests.



Patch:
diff --git a/index.js b/index.js
index 0decd488..2a117bd7 100644
--- a/index.js
+++ b/index.js
@@ -254,9 +254,8 @@ function $asInteger (i) {
   } else if (Number.isInteger(i)) {
     return $asNumber(i)
   } else {
-    // if the output is NaN the type is coerced to int 0
     /* eslint no-undef: "off" */
-    return $asNumber(parseInteger(i) || 0)
+    return $asNumber(parseInteger(i))
   }
 }
 


Test patch:
diff --git a/test/date.test.js b/test/date.test.js
index 0efffb3f..c092616a 100644
--- a/test/date.test.js
+++ b/test/date.test.js
@@ -76,7 +76,7 @@ test('verify padding for rendered date in a string when format is date', (t) =>
   t.ok(validate(JSON.parse(output)), 'valid schema')
 })
 
-test('render a date in a string when format is time as HH:mm:ss', (t) => {
+test('render a date in a string when format is time as kk:mm:ss', (t) => {
   t.plan(3)
 
   const schema = {
diff --git a/test/integer.test.js b/test/integer.test.js
index 283b4ef5..4f0a10bd 100644
--- a/test/integer.test.js
+++ b/test/integer.test.js
@@ -6,6 +6,7 @@ const semver = require('semver')
 const validator = require('is-my-json-valid')
 const proxyquire = require('proxyquire')
 const build = proxyquire('..', { long: null })
+const ROUNDING_TYPES = ['ceil', 'floor', 'round']
 
 test('render an integer as JSON', (t) => {
   t.plan(2)
@@ -40,6 +41,10 @@ test('render a float as an integer', (t) => {
   const cases = [
     { input: Math.PI, output: '3' },
     { input: 5.0, output: '5' },
+    { input: null, output: '0' },
+    { input: 0, output: '0' },
+    { input: 0.0, output: '0' },
+    { input: 42, output: '42' },
     { input: 1.99999, output: '1' },
     { input: -45.05, output: '-45' },
     { input: 0.95, output: '1', rounding: 'ceil' },
@@ -138,7 +143,7 @@ if (semver.gt(process.versions.node, '10.3.0')) {
   t.end()
 }
 
-test('should round interger object parameter ', t => {
+test('should round integer object parameter', t => {
   t.plan(2)
 
   const schema = { type: 'object', properties: { magic: { type: 'integer' } } }
@@ -149,3 +154,29 @@ test('should round interger object parameter ', t => {
   t.equal(output, '{"magic":5}')
   t.ok(validate(JSON.parse(output)), 'valid schema')
 })
+
+test('should not stringify a property if it does not exist', t => {
+  t.plan(2)
+
+  const schema = { title: 'Example Schema', type: 'object', properties: { age: { type: 'integer' } } }
+  const validate = validator(schema)
+  const stringify = build(schema)
+  const output = stringify({})
+
+  t.equal(output, '{}')
+  t.ok(validate(JSON.parse(output)), 'valid schema')
+})
+
+ROUNDING_TYPES.forEach((rounding) => {
+  test(`should not stringify a property if it does not exist (rounding: ${rounding})`, t => {
+    t.plan(2)
+
+    const schema = { type: 'object', properties: { magic: { type: 'integer' } } }
+    const validate = validator(schema)
+    const stringify = build(schema, { rounding })
+    const output = stringify({})
+
+    t.equal(output, '{}')
+    t.ok(validate(JSON.parse(output)), 'valid schema')
+  })
+})