Bug fix: serializing nested value objects wasn't working.

This commit is contained in:
Yossi Kolesnicov 2018-11-25 10:52:13 +02:00
Родитель 899a5e48bf
Коммит 6605b4bb16
4 изменённых файлов: 48 добавлений и 2 удалений

9
CHANGELOG.md Normal file
Просмотреть файл

@ -0,0 +1,9 @@
# [1.3.6] (2018-11-25)
### Bug Fixes
* `Modeler.serializeModel` was serializing nested value objects as `undefined`
### Tests
* Added test for `Modeler.serializeModel` with nested value object.

Просмотреть файл

@ -348,7 +348,7 @@ export class Modeler {
else modelValue = itemFieldValue.map((item: any) => DataTransformersService.serialize(entityField.arrayOf, item));
} else modelValue = null;
}
else if (fieldRepository)
else if (fieldRepository && fieldRepository.entity)
modelValue = isNilValue ? fieldRepository.modelConfig.getDefaultValue() || null : itemFieldValue.id;
else if (fieldValueObjectType)
modelValue = isNilValue ? fieldValueObjectType.getDefaultValue() || null : this.serializeModel(itemFieldValue, fieldValueObjectType, serializationData);

Просмотреть файл

@ -1,6 +1,6 @@
{
"name": "@microsoft/paris",
"version": "1.3.5",
"version": "1.3.6",
"description": "Library for the implementation of Domain Driven Design with TypeScript + RxJS",
"repository": {
"type": "git",

Просмотреть файл

@ -15,6 +15,9 @@ import {EntityModelBase} from "../../lib/config/entity-model.base";
import {Repository} from "../../lib/api/repository/repository";
import {EntityField} from "../../lib/config/decorators/entity-field.decorator";
import {TodoStatus} from "../mock/todo-status.entity";
import {ModelBase} from "../../lib/config/model.base";
import {ValueObject} from "../../lib/config/decorators/value-object.decorator";
import {ModelConfig} from "../../lib/config/model-config";
describe('Modeler', () => {
let paris: Paris;
@ -247,9 +250,24 @@ describe('Modeler', () => {
let serializeItem:jest.Mock;
let serializedItemRepo:Repository<any>;
let serializedEntityWithValueObject:{ new(data:any):any };
let serializedEntityWithValueObjectRepo:Repository<any>;
beforeEach(() => {
serializeItem = jest.fn();
@ValueObject({
singularName: 'property',
pluralName: 'property'
})
class SerializedEntityPropertyValueObject extends ModelBase {
@EntityField()
prop1: string;
@EntityField()
prop2: string;
}
@Entity({
singularName: "Serialized entity",
pluralName: "Serialized entities",
@ -260,8 +278,21 @@ describe('Modeler', () => {
@EntityField() name:string;
}
@Entity({
singularName: "Serialized entity2",
pluralName: "Serialized entities2",
endpoint: "serialized2",
})
class SerializedEntityWithValueObject extends EntityModelBase {
@EntityField() name:string;
@EntityField() val: SerializedEntityPropertyValueObject;
}
serializedEntity = SerializedEntity;
serializedItemRepo = paris.getRepository(SerializedEntity);
serializedEntityWithValueObject = SerializedEntityWithValueObject;
serializedEntityWithValueObjectRepo = paris.getRepository(SerializedEntityWithValueObject);
});
it("calls the entity's `serializeItem` method with the proper params", () => {
@ -271,5 +302,11 @@ describe('Modeler', () => {
serializedItemRepo.save(newSerializedItem, null, serializationData);
expect(serializeItem).toBeCalledWith(newSerializedItem, newSerializedItem, (<DataEntityType<any>>serializedEntity).entityConfig, paris.config, serializationData);
});
it ("serializes the entity with the nested value object intact", () => {
const newSerializedItem = { name: 'test', val: { prop1: 'test', prop2: 'test2' } };
const serializedModel:Record<string, any> = paris.modeler.serializeModel<any>(newSerializedItem, serializedEntityWithValueObjectRepo.modelConfig as ModelConfig<any>);
expect(serializedModel['val']).toBeDefined();
});
});
});