Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(serialization): improved constructor (de)serialization #502

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions packages/serialization/src/serializers/ClassSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export default class ClassSerializer extends ValueSerializer
return object instanceof Object
&& object.serialized === true
&& typeof object.name === 'string'
&& object.args instanceof Array
&& object.args instanceof Object
&& object.args.constructor === Object
&& object.fields instanceof Object
&& object.fields.constructor === Object;
}
Expand All @@ -45,27 +46,27 @@ export default class ClassSerializer extends ValueSerializer
{
const clazz = reflector.getClass(object);
const model = reflector.fromClass(clazz, true);
const parameterNames = this.#extractParameterNames(model);
const parameterNames = this.#extractConstructorParameters(model);

const name = clazz.name;
const source = (clazz as Loadable).source;
const args: unknown[] = await this.#extractArguments(model, parameterNames, object);
const fields: FlexObject = await this.#extractFields(model, parameterNames, object);
const args: FlexObject = await this.#serializeConstructor(model, parameterNames, object);
const fields: FlexObject = await this.#serializeFields(model, parameterNames, object);

return { serialized: true, name: name, source: source, args: args, fields: fields };
}

#extractParameterNames(model: ReflectionClass): string[]
#extractConstructorParameters(model: ReflectionClass): string[]
{
const constructor = model.getFunction('constructor');
const parameters = (constructor?.parameters ?? []) as ReflectionField[];

return parameters.map(parameter => parameter.name);
}

async #extractArguments(model: ReflectionClass, includeNames: string[], object: object): Promise<unknown[]>
async #serializeConstructor(model: ReflectionClass, includeNames: string[], object: object): Promise<FlexObject>
{
const args: unknown[] = [];
const args: FlexObject = {};

for (const name of includeNames)
{
Expand All @@ -75,13 +76,13 @@ export default class ClassSerializer extends ValueSerializer
? await this.serializeOther((object as FlexObject)[name])
: undefined;

args.push(objectValue);
args[name] = objectValue;
}

return args;
}

async #extractFields(model: ReflectionClass, excludeNames: string[], object: object): Promise<FlexObject>
async #serializeFields(model: ReflectionClass, excludeNames: string[], object: object): Promise<FlexObject>
{
const fields: FlexObject = {};

Expand Down Expand Up @@ -115,7 +116,7 @@ export default class ClassSerializer extends ValueSerializer
throw new InvalidClass(object.name);
}

const args = await Promise.all(object.args.map(async (value) => this.deserializeOther(value)));
const args = await this.#deserializeConstructor(clazz as Function, object.args);

const instance = reflector.createInstance(clazz as Function, args) as SerializableObject;

Expand All @@ -129,13 +130,29 @@ export default class ClassSerializer extends ValueSerializer
return instance;
}

async #getClass(clazz: Loadable): Promise<unknown>
async #deserializeConstructor(clazz: Function, args: FlexObject): Promise<unknown[]>
{
if (clazz.source === undefined)
const model = reflector.fromClass(clazz, true);
const constructor = model.getFunction('constructor');
const parameters = (constructor?.parameters ?? []) as ReflectionField[];

const values = parameters.map(parameter =>
{
const value = args[parameter.name];

return this.deserializeOther(value);
});

return Promise.all(values);
}

async #getClass(loadable: Loadable): Promise<unknown>
{
if (loadable.source === undefined)
{
return (globalThis as FlexObject)[clazz.name];
return (globalThis as FlexObject)[loadable.name];
}

return this.#classLoader.loadClass(clazz);
return this.#classLoader.loadClass(loadable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Serialized from '../Serialized.js';

type SerializedClass = Serialized & Loadable &
{
args: unknown[],
args: Record<string, unknown>,
fields: Record<string, unknown>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ class PrivateFieldClass
{
#a: number;
#b: boolean;
c?: string;
#c?: string;

constructor(a: number, b: boolean)
constructor(a: number, b: boolean, c?: string)
{
this.#a = a;
this.#b = b;
this.#c = c;
}

get d() { return this.#a; }
Expand Down Expand Up @@ -101,20 +102,20 @@ constructedClass.c = 'hello';
const nestedClass = new Nested('hello', constructedClass);
const privateClass = new PrivateFieldClass(1, true);

const serializedDataClass = { serialized: true, name: 'Data', source: 'Data', args: [], fields: { a: 1, b: true } };
const serializedConstructedClass = { serialized: true, name: 'Constructed', source: 'Constructed', args: [1, true], fields: { c: 'hello' } };
const serializedNestedClass = { serialized: true, name: 'Nested', source: 'Nested', args: ['hello', { serialized: true, name: 'Constructed', source: 'Constructed', args: [1, true], fields: { c: 'hello' } }], fields: {} };
const serializedPrivateClass = { serialized: true, name: 'PrivateFieldClass', source: 'PrivateFieldClass', args: [undefined, undefined], fields: { c: undefined } };
const serializedDataClass = { serialized: true, name: 'Data', source: 'Data', args: {}, fields: { a: 1, b: true } };
const serializedConstructedClass = { serialized: true, name: 'Constructed', source: 'Constructed', args: { a: 1, b: true }, fields: { c: 'hello' } };
const serializedNestedClass = { serialized: true, name: 'Nested', source: 'Nested', args: { name: 'hello', constructed: { serialized: true, name: 'Constructed', source: 'Constructed', args: { a: 1, b: true }, fields: { c: 'hello' } } }, fields: {} };
const serializedPrivateClass = { serialized: true, name: 'PrivateFieldClass', source: 'PrivateFieldClass', args: { a: undefined, b: undefined, c: undefined }, fields: { } };

const serializedInvalidClass = { serialized: true, name: 'Invalid', source: undefined, args: [], fields: {} };
const serializedUnserializableClass = { serialized: true, name: 'Infinity', source: undefined, args: [], fields: {} };
const serializedInvalidClass = { serialized: true, name: 'Invalid', source: undefined, args: {}, fields: {} };
const serializedUnserializableClass = { serialized: true, name: 'Infinity', source: undefined, args: {}, fields: {} };

const nonObject = 42;
const nonClassObject = new Object();
const notSerialized = { name: 'Data', source: undefined, args: [], fields: {} };
const invalidName = { serialized: true, name: 123, source: undefined, args: [], fields: {} };
const invalidArgs = { serialized: true, name: 'Data', source: undefined, args: {}, fields: {} };
const invalidFields = { serialized: true, name: 'Data', source: undefined, args: [], fields: [] };
const notSerialized = { name: 'Data', source: undefined, args: {}, fields: {} };
const invalidName = { serialized: true, name: 123, source: undefined, args: {}, fields: {} };
const invalidArgs = { serialized: true, name: 'Data', source: undefined, args: [], fields: {} };
const invalidFields = { serialized: true, name: 'Data', source: undefined, args: {}, fields: [] };

export {
Data, Constructed, Nested, PrivateFieldClass,
Expand Down