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(serialiser): serialiser does not compute hidden smart fields anymore #1020

Merged
merged 3 commits into from
Nov 3, 2023
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
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');
});
});
});
});
});
Loading