Skip to content

Commit

Permalink
fix(serialiser): serialiser does not compute hidden smart fields anym…
Browse files Browse the repository at this point in the history
…ore (#1020)
  • Loading branch information
SteveBunlon authored Nov 3, 2023
1 parent 419acd9 commit 12bd547
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/services/exposed/record-serializer.js
Original file line number Diff line number Diff line change
@@ -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()) {
Expand All @@ -17,6 +18,7 @@ class RecordSerializer {

this.model = model;
this.configStore = configStore;
this.query = query;
}

serialize(records, meta = null) {
Expand All @@ -26,6 +28,9 @@ class RecordSerializer {
records,
this.configStore.integrator,
meta,
null,
null,
this.query?.fields ? new ParamsFieldsDeserializer(this.query.fields).perform() : null,
).perform();
}
}
Expand Down
96 changes: 96 additions & 0 deletions test/services/exposed/record-serializer.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
});
});

0 comments on commit 12bd547

Please sign in to comment.