Skip to content

fix: better support for polymorphic inputs #87

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

Merged
merged 1 commit into from
Apr 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
42 changes: 37 additions & 5 deletions src/classes/polymorphic-serialiser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,48 @@ export default class PolymorphicSerializer<
})
);

return documents.reduce((result, document) => {
if (!result) {
return document;
}
// Construct initial document and included data
let document: Partial<DataDocument<PrimaryType>> = {
data: [],
};

// Document versioning
if (options?.version) {
document.jsonapi = { ...document.jsonapi, version: options.version };
}

if (options?.metaizers?.jsonapi) {
document.jsonapi = { ...document.jsonapi, meta: options.metaizers.jsonapi.metaize() };
}

document = documents.reduce((result, document) => {
result.data = [result.data ?? [], document.data ?? []].flat();
result.included = [result.included ?? [], document.included ?? []].flat();

return result;
});
}, document);

// Handle meta
if (options?.metaizers?.document) {
document.meta = options.metaizers.document.metaize(data);
}

// Handle links
if (options?.linkers?.document) {
if (options.linkers.document) {
document.links = { ...document.links, self: options.linkers.document.link(data) };
}
if (options.linkers.paginator) {
const pagination = options.linkers.paginator.paginate(
data as PrimaryType | PrimaryType[]
);
if (pagination) {
document.links = { ...document.links, ...pagination };
}
}
}

return document;
} else if (data) {
return this.serializeType(data, options);
}
Expand Down
37 changes: 37 additions & 0 deletions test/issue-80.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,41 @@ describe('Issue #80 - Polymorphic serializer', () => {
expect(data.data[0].id).toEqual('1');
expect(data.data[0].type).toEqual('Model1');
});

it('should correctly handle empty input', async () => {
const Model1Serializer = new Serializer<Model1>('Model1');
const Model2Serializer = new Serializer<Model2>('Model2');

const PolySerializer = new PolymorphicSerializer<Model>('Model', 'type', {
'type:Model1': Model1Serializer,
'type:Model2': Model2Serializer,
});

const data = (await PolySerializer.serialize([])) as {
data: Resource<Model>;
};

expect(data.data).toBeInstanceOf(Array);
expect(data.data).toHaveLength(0);
});

it('should correctly handle non-array input', async () => {
const model1: Model1 = new Model1('1', 'model1');

const Model1Serializer = new Serializer<Model1>('Model1');
const Model2Serializer = new Serializer<Model2>('Model2');

const PolySerializer = new PolymorphicSerializer<Model>('Model', 'type', {
'type:Model1': Model1Serializer,
'type:Model2': Model2Serializer,
});

const data = (await PolySerializer.serialize(model1)) as {
data: Resource<Model>;
};

expect(data.data).toBeInstanceOf(Resource);
expect(data.data.id).toEqual('1');
expect(data.data.type).toEqual('Model1');
});
});
Loading