Skip to content

Commit 8f7bcf8

Browse files
authored
[core-http] Fix constant flattened property serialization (Azure#8658)
* Fix constant flattened property serialization * Update changelog * Update changelog
1 parent 0363f89 commit 8f7bcf8

File tree

4 files changed

+129
-2
lines changed

4 files changed

+129
-2
lines changed

sdk/core/core-http/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Release History
22

33
## 1.1.4 (Unreleased)
4-
4+
- Fix issue with flattened model serialization, where constant properties are being dropped [PR#8658](https://github.com/Azure/azure-sdk-for-js/pull/8658)
55

66
## 1.1.3 (2020-06-03)
77

sdk/core/core-http/src/serializer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,10 @@ function serializeCompositeType(
573573

574574
for (const pathName of paths) {
575575
const childObject = parentObject[pathName];
576-
if (childObject == undefined && object[key] != undefined) {
576+
if (
577+
childObject == undefined &&
578+
(object[key] != undefined || propertyMapper.defaultValue !== undefined)
579+
) {
577580
parentObject[pathName] = {};
578581
}
579582
parentObject = parentObject[pathName];

sdk/core/core-http/test/data/TestClient/src/models/mappers.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,82 @@
55
*/
66
const internalMappers: any = {};
77

8+
internalMappers.SimpleProduct = {
9+
type: {
10+
name: "Composite",
11+
className: "SimpleProduct",
12+
modelProperties: {
13+
id: {
14+
serializedName: "id",
15+
constraints: {},
16+
required: true,
17+
type: {
18+
name: "Number"
19+
}
20+
},
21+
name: {
22+
serializedName: "name",
23+
required: true,
24+
type: {
25+
name: "String"
26+
}
27+
},
28+
maxProductDisplayName: {
29+
serializedName: "details.max_product_display_name",
30+
type: {
31+
name: "String"
32+
}
33+
},
34+
capacity: {
35+
defaultValue: "Large",
36+
isConstant: true,
37+
serializedName: "details.max_product_capacity",
38+
type: {
39+
name: "String"
40+
}
41+
}
42+
}
43+
}
44+
};
45+
46+
internalMappers.SimpleProductConstFirst = {
47+
type: {
48+
name: "Composite",
49+
className: "SimpleProduct",
50+
modelProperties: {
51+
id: {
52+
serializedName: "id",
53+
constraints: {},
54+
required: true,
55+
type: {
56+
name: "Number"
57+
}
58+
},
59+
name: {
60+
serializedName: "name",
61+
required: true,
62+
type: {
63+
name: "String"
64+
}
65+
},
66+
capacity: {
67+
defaultValue: "Large",
68+
isConstant: true,
69+
serializedName: "details.max_product_capacity",
70+
type: {
71+
name: "String"
72+
}
73+
},
74+
maxProductDisplayName: {
75+
serializedName: "details.max_product_display_name",
76+
type: {
77+
name: "String"
78+
}
79+
}
80+
}
81+
}
82+
};
83+
884
internalMappers.Cat = {
985
required: false,
1086
serializedName: "cat",

sdk/core/core-http/test/serializationTests.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,54 @@ function stringToByteArray(str: string): Uint8Array {
2121

2222
describe("msrest", function() {
2323
describe("serializeObject", function() {
24+
it("should correctly serialize flattened properties", (done) => {
25+
const expected = {
26+
id: 1,
27+
name: "testProduct",
28+
details: {
29+
max_product_capacity: "Large",
30+
max_product_display_name: "MaxDisplayName"
31+
}
32+
};
33+
34+
const serialized = Serializer.serialize(
35+
Mappers.SimpleProduct,
36+
{
37+
id: 1,
38+
name: "testProduct",
39+
maxProductDisplayName: "MaxDisplayName"
40+
},
41+
"SimpleProduct"
42+
);
43+
44+
assert.deepEqual(serialized, expected);
45+
done();
46+
});
47+
48+
it("should correctly serialize flattened properties when flattened constant is defined first", (done) => {
49+
const expected = {
50+
id: 1,
51+
name: "testProduct",
52+
details: {
53+
max_product_capacity: "Large",
54+
max_product_display_name: "MaxDisplayName"
55+
}
56+
};
57+
58+
const serialized = Serializer.serialize(
59+
Mappers.SimpleProductConstFirst,
60+
{
61+
id: 1,
62+
name: "testProduct",
63+
maxProductDisplayName: "MaxDisplayName"
64+
},
65+
"SimpleProduct"
66+
);
67+
68+
assert.deepEqual(serialized, expected);
69+
done();
70+
});
71+
2472
it("should correctly serialize a Date Object", function(done) {
2573
const dateObj = new Date("2015-01-01");
2674
const dateISO = "2015-01-01T00:00:00.000Z";

0 commit comments

Comments
 (0)