diff --git a/src/services/exposed/record-serializer.js b/src/services/exposed/record-serializer.js index 8a264c430..570309939 100644 --- a/src/services/exposed/record-serializer.js +++ b/src/services/exposed/record-serializer.js @@ -1,5 +1,6 @@ const { inject } = require('@forestadmin/context'); const ResourceSerializer = require('../../serializers/resource'); +const ParamsFieldsDeserializer = require('../../deserializers/params-fields'); class RecordSerializer { constructor(model, user, query, { configStore } = inject()) { @@ -17,6 +18,7 @@ class RecordSerializer { this.model = model; this.configStore = configStore; + this.query = query; } serialize(records, meta = null) { @@ -26,6 +28,9 @@ class RecordSerializer { records, this.configStore.integrator, meta, + null, + null, + this.query?.fields ? new ParamsFieldsDeserializer(this.query.fields).perform() : null, ).perform(); } } diff --git a/test/services/exposed/record-serializer.test.js b/test/services/exposed/record-serializer.test.js new file mode 100644 index 000000000..b28a93f6b --- /dev/null +++ b/test/services/exposed/record-serializer.test.js @@ -0,0 +1,96 @@ +const RecordSerializer = require('../../../src/services/exposed/record-serializer'); +const Schemas = require('../../../src/generators/schemas'); +const usersSchema = require('../../fixtures/users-schema'); + +const getMockedContext = () => ({ + configStore: { + Implementation: { + getModelName: (model) => model.name, + getLianaName: () => 'name', + getOrmVersion: () => 'version', + }, + integrator: {}, + }, +}); + +describe('services › exposed › record-serializer', () => { + describe('when model is not provided', () => { + it('should throw an error', () => { + expect(() => new RecordSerializer(null, null, null, getMockedContext())).toThrow('RecordSerializer initialization error: missing first argument "model"'); + }); + }); + + describe('when model is provided', () => { + describe('when model is incorrect', () => { + it('should throw an error', () => { + expect(() => new RecordSerializer(1, null, null, getMockedContext())).toThrow('RecordSerializer initialization error: "model" argument should be an object (ex: `{ name: "myModel" }`)'); + }); + }); + + describe('when no query is specified', () => { + it('should compute all the smart fields', async () => { + Schemas.schemas = { users: usersSchema }; + const modelMock = { name: 'users' }; + + const serializer = new RecordSerializer(modelMock, null, null, getMockedContext()); + + const articlesSerialized = await serializer.serialize([{ + hasAddress: true, + id: '1', + name: 'foo', + }]); + + expect(articlesSerialized.data[0].attributes).toContainAllKeys(['hasAddress', 'id', 'name', 'smart']); + }); + }); + + describe('when query is specified', () => { + describe('when smart field is not specified in query', () => { + it('should not compute smart field', async () => { + Schemas.schemas = { users: usersSchema }; + const modelMock = { name: 'users' }; + const queryFields = { fields: { users: 'hasAddress' } }; + + const serializer = new RecordSerializer(modelMock, null, queryFields, { + configStore: { + Implementation: { + getModelName: (model) => model.name, + getLianaName: () => 'forest-express-sequelize', + getOrmVersion: () => '9.5.0', + }, + integrator: {}, + }, + }); + + const articlesSerialized = await serializer.serialize([{ + hasAddress: true, + id: '1', + name: 'foo', + }]); + + const { attributes } = articlesSerialized.data[0]; + expect(attributes).toContainAllKeys(['hasAddress', 'id', 'name']); + expect(attributes).not.toContainKey('smart'); + }); + }); + describe('when smart field is specified in query', () => { + it('should compute smart field', async () => { + Schemas.schemas = { users: usersSchema }; + const modelMock = { name: 'users' }; + const queryFields = { fields: { users: 'hasAddress,id,smart' } }; + + const serializer = new RecordSerializer(modelMock, null, queryFields, getMockedContext()); + + const articlesSerialized = await serializer.serialize([{ + hasAddress: true, + id: '1', + }]); + + const { attributes } = articlesSerialized.data[0]; + expect(attributes).toContainAllKeys(['hasAddress', 'smart', 'id']); + expect(attributes).not.toContainKey('name'); + }); + }); + }); + }); +});