diff --git a/index.js b/index.js index cd7ba6e0..c73cf32c 100644 --- a/index.js +++ b/index.js @@ -231,7 +231,8 @@ function additionalProperty (schema, externalSchema, fullSchema) { let code = '' if (ap === true) { return ` - json += $asString(keys[i]) + ':' + fastSafeStringify(obj[keys[i]]) + ',' + if (obj[keys[i]] !== undefined) + json += $asString(keys[i]) + ':' + fastSafeStringify(obj[keys[i]]) + ',' ` } if (ap['$ref']) { diff --git a/test/additionalProperties.test.js b/test/additionalProperties.test.js index 4287b5b3..6f9dd502 100644 --- a/test/additionalProperties.test.js +++ b/test/additionalProperties.test.js @@ -254,3 +254,20 @@ test('nested additionalProperties set to true', (t) => { let obj = { ap: { value: 'string', someNumber: 42 } } t.equal('{"ap":{"value":"string","someNumber":42}}', stringify(obj)) }) + +test('field passed to fastSafeStringify as undefined should be removed', (t) => { + t.plan(1) + const stringify = build({ + title: 'nested additionalProperties=true', + type: 'object', + properties: { + ap: { + type: 'object', + additionalProperties: true + } + } + }) + + let obj = { ap: { value: 'string', someNumber: undefined } } + t.equal('{"ap":{"value":"string"}}', stringify(obj)) +})