Skip to content

Commit b6a692c

Browse files
committed
Fix multiple operation for embedded object while serializing
1 parent 809e962 commit b6a692c

File tree

5 files changed

+143
-16
lines changed

5 files changed

+143
-16
lines changed

blueprint/functions/normalize.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
const { keys, forEach } = require('lodash');
44
const { isArray } = require('../../types/detector');
55
const { isTypeObject } = require('../utils');
6-
const { resolveEmbededObj, parseEmbedValue, initiateTypeHandler, handleUnknownProperties } = require('./utils');
6+
const {
7+
resolveEmbededObj,
8+
parseEmbedValue,
9+
initiateTypeHandler,
10+
handleUnknownProperties
11+
} = require('./utils');
712

813
const normalizeValue = function (valuesToNormalize, onValidation = false) {
914
if (this.isArray && !isArray(valuesToNormalize)) {
@@ -108,7 +113,10 @@ const normalizeValue = function (valuesToNormalize, onValidation = false) {
108113
doValidation: onValidation
109114
});
110115
if (isAllowUnknownProperties) {
111-
Object.assign(normalizedResult, handleUnknownProperties(v, this.schema));
116+
Object.assign(
117+
normalizedResult,
118+
handleUnknownProperties(v, this.schema)
119+
);
112120
}
113121
return [errorDetails, normalizedResult];
114122
});

blueprint/functions/serialize.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
'use strict';
22

33
const { includes, forEach } = require('lodash');
4-
const { resolveEmbededObj, parseEmbedValue, initiateTypeHandler, handleUnknownProperties } = require('./utils');
4+
const {
5+
resolveEmbededObj,
6+
parseEmbedValue,
7+
initiateTypeHandler,
8+
handleUnknownProperties
9+
} = require('./utils');
510

6-
const serializeValue = function (valuesToSerialize, onValidation = false) {
11+
const serializeValue = function (
12+
valuesToSerialize,
13+
onValidation = false,
14+
opts = { isSelfCall: false }
15+
) {
716
let serialized = this.isArray ? [] : {};
8-
const [
9-
isError,
10-
errorDetails,
11-
normalizedValues
12-
] = this.normalize(valuesToSerialize, { doValidation: onValidation });
17+
18+
let isError = false;
19+
let errorDetails = [];
20+
let normalizedValues = {};
21+
if (!opts.isSelfCall) {
22+
[isError, errorDetails, normalizedValues] = this.normalize(
23+
valuesToSerialize,
24+
{ doValidation: onValidation }
25+
);
26+
} else {
27+
normalizedValues = valuesToSerialize;
28+
}
1329

1430
const isAllowUnknownProperties = this.options.allowUnknownProperties;
1531

@@ -21,7 +37,11 @@ const serializeValue = function (valuesToSerialize, onValidation = false) {
2137
const embedObj = resolveEmbededObj(typeHandler);
2238
if (embedObj !== null) {
2339
if (!embedObj.isHideOnSerialization()) {
24-
const serializedResult = parseEmbedValue('serialize', embedObj, normalizedValues[key]);
40+
const serializedResult = parseEmbedValue(
41+
'serialize',
42+
embedObj,
43+
normalizedValues[key]
44+
);
2545
const serializeTo = embedObj.getSerializeName();
2646
if (serializeTo !== null) {
2747
serialized[serializeTo] = serializedResult;

blueprint/functions/utils.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ const initiateTypeHandler = typehandler => {
2222
* @param {embedCls} embeddedObject
2323
*/
2424
const resolveEmbededObj = obj =>
25-
(isFunction(obj.embed) && obj.embed() instanceof BlueprintEmbedClass
25+
isFunction(obj.embed) && obj.embed() instanceof BlueprintEmbedClass
2626
? obj.embed()
27-
: obj instanceof BlueprintEmbedClass ? obj : null);
27+
: obj instanceof BlueprintEmbedClass
28+
? obj
29+
: null;
2830

2931
const handleUnknownProperties = (params, objToExclude) => {
3032
const registeredKeys = keys(objToExclude);
@@ -61,7 +63,9 @@ const parseEmbedValue = (clsMethodName, embedObj, valueToParse) => {
6163
} else {
6264
// calling normalize/serialize/deserialize function on parent blueprint obj
6365
const [fail, , parsedValues] = embedInstance[clsMethodName](
64-
valueToParse
66+
valueToParse,
67+
false,
68+
{ isSelfCall: true }
6569
);
6670

6771
// applying embed object options

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'use strict';
2+
3+
/* eslint-disable */
4+
5+
const blueprint = require("../blueprint");
6+
const types = require("../types");
7+
8+
test("Should be executed once for transform function in embed", () => {
9+
const expSchema = blueprint.object({
10+
id: types.transform(id => {
11+
return "abc" + id;
12+
})
13+
});
14+
15+
const expSchema2 = blueprint.object({
16+
emb: expSchema
17+
});
18+
19+
const res = expSchema2({
20+
emb: {
21+
id: 123
22+
}
23+
});
24+
25+
expect(res.emb.id).toEqual("abc123");
26+
});
27+
28+
test("Should be executed once for transform function in embed in serialization", () => {
29+
const expSchema = blueprint.object({
30+
id: types.transform(id => {
31+
return "abc" + id;
32+
})
33+
});
34+
35+
const expSchema2 = blueprint.object({
36+
emb: expSchema
37+
});
38+
39+
const expSchema3 = blueprint.array({
40+
emb: expSchema
41+
});
42+
43+
const res = expSchema2.serialize({
44+
emb: {
45+
id: 123
46+
}
47+
});
48+
49+
const res2 = expSchema3.serialize([
50+
{
51+
emb: {
52+
id: 456
53+
}
54+
}
55+
]);
56+
57+
expect(res.emb.id).toEqual("abc123");
58+
expect(res2[0].emb.id).toEqual("abc456");
59+
});
60+
61+
test("Should be executed once for transform function in embed in deserialization", () => {
62+
const expSchema = blueprint.object({
63+
id: types.transform(
64+
id => {
65+
return "abc" + id;
66+
},
67+
{ deserialize: { from: "number" } }
68+
)
69+
});
70+
71+
const expSchema2 = blueprint.object({
72+
emb: expSchema
73+
});
74+
75+
const expSchema3 = blueprint.array({
76+
emb: expSchema
77+
});
78+
79+
const res = expSchema2.deserialize({
80+
emb: {
81+
number: 123
82+
}
83+
});
84+
85+
const res2 = expSchema3.deserialize([
86+
{
87+
emb: {
88+
number: 456
89+
}
90+
}
91+
]);
92+
93+
expect(res.emb.id).toEqual("abc123");
94+
expect(res2[0].emb.id).toEqual("abc456");
95+
});

0 commit comments

Comments
 (0)