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

try-to-fix-the-non-required-default-value #26920

Closed
Closed
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
8 changes: 6 additions & 2 deletions sdk/core/core-client/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class SerializerImpl implements Serializer {
responseBody = [];
}
// specifically check for undefined as default value can be a falsey value `0, "", false, null`
if (mapper.defaultValue !== undefined) {
if (mapper.required && mapper.defaultValue !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this is a pretty big change in runtime behavior. To ensure we didn't break we'd have to audit every use of defaultValue in a mapper for all SDKs we've shipped.

responseBody = mapper.defaultValue;
}
return responseBody;
Expand Down Expand Up @@ -1001,7 +1001,11 @@ function deserializeCompositeType(
}
}
instance = arrayInstance;
} else if (propertyInstance !== undefined || propertyMapper.defaultValue !== undefined) {
} else if (
propertyInstance !== undefined ||
((propertyMapper.required || propertyMapper.isConstant) &&
propertyMapper.defaultValue !== undefined)
) {
serializedValue = serializer.deserialize(
propertyMapper,
propertyInstance,
Expand Down
22 changes: 22 additions & 0 deletions sdk/core/core-client/test/serializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@ describe("Serializer", function () {
assert.deepEqual(serialized, expected);
});

it.only("should not set default value for non-required properties", function () {
const expected = {
id: 1,
name: "testProduct",
details: {
max_product_capacity: "Large",
},
};

const serialized = Serializer.serialize(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need a deserialize test rather than a serialize one

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing it out :)
silly me 😂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me a little bit to notice as well 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found if I change the serialize into deserialize and revert all the other changes back, the test will fail, it seems like we have some issues when handling flatten? Sorry I haven't get time to look into the details yet.
Also, as flatten will also need special serialization / deserialization in Modular, and I heard other language DPGs are using TypeSpec alias to do the flatten, I might need to investigate it a little first.

Mappers.SimpleProduct,
{
id: 1,
name: "testProduct",
},
"SimpleProduct"
);

assert.deepEqual(serialized, expected);
assert.deepEqual(serialized.details.max_product_display_name, undefined);
});

it("should correctly serialize a string if the type is 'any'", function () {
const mapper: Mapper = {
type: { name: "any" },
Expand Down
1 change: 1 addition & 0 deletions sdk/core/core-client/test/testMappers1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ internalMappers.SimpleProduct = {
},
maxProductDisplayName: {
serializedName: "details.max_product_display_name",
defaultValue: "testDisPlayName",
type: {
name: "String",
},
Expand Down
Loading