Skip to content

Preserving references fails with user-defined conversions  #11

@HannesSommer

Description

@HannesSommer

The following minimal test case (in typescript) reveals a potential bug where a reference is undefined after revive through user defined conversion:

const Typeson = require('typeson');

class ComplexClassThroughArray {
	constructor(public x = {}) {
	}
}

const typeson = new Typeson().register({
	ComplexClassThroughArray: [
		(x: any) => x instanceof ComplexClassThroughArray, // test function
		(d: ComplexClassThroughArray) => [d.x], // encapsulator function
		(d: Array<object>) => { // reviver function
			return new ComplexClassThroughArray(d[0]);
		}
	],
});

it('revive_complex_class_through_array', () => {
	const o = new ComplexClassThroughArray();
	const json = JSON.stringify(typeson.encapsulate({
		o,
		x: o.x
	}));

	const r = typeson.revive(JSON.parse(json));

	expect(r.x).toStrictEqual(o.x);
});

This test fails at the very end because r.x is undefined.

The json variable has the serialized value {"o":[{}],"x":"#o.0","$types":{"o":"ComplexClassThrughArray","x":"#"}}.

The same problem appears if you replace ComplexClassThroughArray with the built-in type Set, which has basically an identical user defined conversion in typeson-registry.

Could it be that the reference in "x":"#o.0" does get resolved after "o":[{}] has been revived rendering the json path invalid?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions