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

[core-http] Fix constant flattened property serialization #8658

Merged
merged 3 commits into from
Jun 8, 2020
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
2 changes: 1 addition & 1 deletion sdk/core/core-http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Release History

## 1.1.4 (Unreleased)

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

## 1.1.3 (2020-06-03)

Expand Down
5 changes: 4 additions & 1 deletion sdk/core/core-http/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,10 @@ function serializeCompositeType(

for (const pathName of paths) {
const childObject = parentObject[pathName];
if (childObject == undefined && object[key] != undefined) {
if (
childObject == undefined &&
(object[key] != undefined || propertyMapper.defaultValue !== undefined)
) {
parentObject[pathName] = {};
}
parentObject = parentObject[pathName];
Expand Down
76 changes: 76 additions & 0 deletions sdk/core/core-http/test/data/TestClient/src/models/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,82 @@
*/
const internalMappers: any = {};

internalMappers.SimpleProduct = {
type: {
name: "Composite",
className: "SimpleProduct",
modelProperties: {
id: {
serializedName: "id",
constraints: {},
required: true,
type: {
name: "Number"
}
},
name: {
serializedName: "name",
required: true,
type: {
name: "String"
}
},
maxProductDisplayName: {
serializedName: "details.max_product_display_name",
type: {
name: "String"
}
},
capacity: {
defaultValue: "Large",
isConstant: true,
serializedName: "details.max_product_capacity",
type: {
name: "String"
}
}
}
}
};

internalMappers.SimpleProductConstFirst = {
type: {
name: "Composite",
className: "SimpleProduct",
modelProperties: {
id: {
serializedName: "id",
constraints: {},
required: true,
type: {
name: "Number"
}
},
name: {
serializedName: "name",
required: true,
type: {
name: "String"
}
},
capacity: {
defaultValue: "Large",
isConstant: true,
chradek marked this conversation as resolved.
Show resolved Hide resolved
serializedName: "details.max_product_capacity",
type: {
name: "String"
}
},
maxProductDisplayName: {
serializedName: "details.max_product_display_name",
type: {
name: "String"
}
}
}
}
};

internalMappers.Cat = {
required: false,
serializedName: "cat",
Expand Down
48 changes: 48 additions & 0 deletions sdk/core/core-http/test/serializationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,54 @@ function stringToByteArray(str: string): Uint8Array {

describe("msrest", function() {
describe("serializeObject", function() {
it("should correctly serialize flattened properties", (done) => {
const expected = {
id: 1,
name: "testProduct",
details: {
max_product_capacity: "Large",
max_product_display_name: "MaxDisplayName"
}
};

const serialized = Serializer.serialize(
Mappers.SimpleProduct,
{
id: 1,
name: "testProduct",
maxProductDisplayName: "MaxDisplayName"
},
"SimpleProduct"
);

assert.deepEqual(serialized, expected);
done();
});

it("should correctly serialize flattened properties when flattened constant is defined first", (done) => {
const expected = {
id: 1,
name: "testProduct",
details: {
max_product_capacity: "Large",
max_product_display_name: "MaxDisplayName"
}
};

const serialized = Serializer.serialize(
Mappers.SimpleProductConstFirst,
{
id: 1,
name: "testProduct",
maxProductDisplayName: "MaxDisplayName"
},
"SimpleProduct"
);

assert.deepEqual(serialized, expected);
done();
});

it("should correctly serialize a Date Object", function(done) {
const dateObj = new Date("2015-01-01");
const dateISO = "2015-01-01T00:00:00.000Z";
Expand Down