fast-json-stringify icon indicating copy to clipboard operation
fast-json-stringify copied to clipboard

AdditionalProperties with method references causes invalid JSON "undefined"

Open MetalMichael opened this issue 5 years ago • 1 comments

🐛 Bug Report

When trying to serialize a class with additionalProperties, methods cause the serializer to output "undefined", which causes JSON parsing errors.

To Reproduce

Create an object or class with a method property not included in the JSON schema, and try to serialize with additionalProperties: true.

const fastJson = require('fast-json-stringify')
const stringify = fastJson({
  title: 'Example Schema',
  type: 'object',
  additionalProperties: true,
  properties: {
    firstName: {
      type: 'string'
    },
    lastName: {
      type: 'string'
    },
    age: {
      description: 'Age in years',
      type: 'integer'
    },
  }
})

class X {
  firstName = "john";
  lastName = "smith";
  age = 42;
  test = "test";
  
  getProperties = () => {
    return [];
  }
}
console.log(stringify(new X()));

JSON.parse(stringify(new X())); // This throws!

Expected behavior

Invalid properties not included in JSON. This seems to be how it is handled for invalid data types with objects when they have a specified schema.

// Expected
{"firstName":"john","lastName":"smith","age":32,"test":"test"}

// Actual
{"firstName":"john","lastName":"smith","age":32,"test":"test","getProperties":undefined}

Extra

This also can be replicated with the much simpler version (same schema):

const content = stringify({x: 'test', y: () => 'y'});

{"x":"test","y":undefined}

Your Environment

Replicated in docker containers and on my Mac (Catalina 10.15.7) /w node v.14.4.0 & fast-json-stringify@^2.5.0.

MetalMichael avatar Mar 10 '21 12:03 MetalMichael

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Oct 01 '26 03:10 github-actions[bot]