Skip to content

Commit 8f67b8f

Browse files
authored
[communication-common] Handle unknown additional properties when deserializing identifier REST model (Azure#22629)
* [communication-common] Handle unknown additional properties when deserializing identifier REST model * fix let to const * remove unnecessary escape character
1 parent c8ddd26 commit 8f67b8f

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

sdk/communication/communication-common/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
- Added `getIdentifierRawId` and `createIdentifierFromRawId` to translate between a `CommunicationIdentifier` and its underlying canonical rawId representation. Developers can now use the rawId as an encoded format for identifiers to store in their databases or as stable keys in general.
88
- Always include `rawId` when serializing identifiers to wire format.
99

10+
### Bugs Fixed
11+
12+
- Made internal `CommunicationIdentifierSerializer` resilient to unknown additional response properties.
13+
1014
## 2.0.0 (2022-03-08)
1115

1216
### Features Added

sdk/communication/communication-common/src/identifierModelSerializer.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,20 @@ const assertNotNullOrUndefined = <
9595
};
9696

9797
const assertMaximumOneNestedModel = (identifier: SerializedCommunicationIdentifier): void => {
98-
const { rawId: _rawId, ...props } = identifier;
99-
const keys = Object.keys(props);
100-
if (keys.length > 1) {
101-
throw new Error(`Only one of the properties in ${JSON.stringify(keys)} should be present.`);
98+
const presentProperties: string[] = [];
99+
if (identifier.communicationUser !== undefined) {
100+
presentProperties.push("communicationUser");
101+
}
102+
if (identifier.microsoftTeamsUser !== undefined) {
103+
presentProperties.push("microsoftTeamsUser");
104+
}
105+
if (identifier.phoneNumber !== undefined) {
106+
presentProperties.push("phoneNumber");
107+
}
108+
if (presentProperties.length > 1) {
109+
throw new Error(
110+
`Only one of the properties in ${JSON.stringify(presentProperties)} should be present.`
111+
);
102112
}
103113
};
104114

sdk/communication/communication-common/test/identifierModelSerializer.spec.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ const assertThrowsMissingProperty = <
4040
const assertThrowsTooManyProperties = (
4141
serializedIdentifier: SerializedCommunicationIdentifier
4242
): void => {
43-
const { rawId: _rawId, ...props } = serializedIdentifier;
4443
assert.throws(() => {
4544
deserializeCommunicationIdentifier(serializedIdentifier);
46-
}, `Only one of the properties in ${JSON.stringify(Object.keys(props))} should be present.`);
45+
}, /^Only one of the properties in \[[\w,"\s]+\] should be present.$/);
4746
};
4847

4948
describe("Identifier model serializer", () => {
@@ -161,6 +160,32 @@ describe("Identifier model serializer", () => {
161160
"8:acs:37691ec4-57fb-4c0f-ae31-32791610cb14_37691ec4-57fb-4c0f-ae31-32791610cb14",
162161
}
163162
);
163+
assertDeserialize(
164+
{
165+
kind: "communicationUser",
166+
communicationUser: {
167+
id: "8:acs:37691ec4-57fb-4c0f-ae31-32791610cb14_37691ec4-57fb-4c0f-ae31-32791610cb14",
168+
},
169+
} as any,
170+
{
171+
kind: "communicationUser",
172+
communicationUserId:
173+
"8:acs:37691ec4-57fb-4c0f-ae31-32791610cb14_37691ec4-57fb-4c0f-ae31-32791610cb14",
174+
}
175+
);
176+
assertDeserialize(
177+
{
178+
someFutureProperty: "fooBar",
179+
communicationUser: {
180+
id: "8:acs:37691ec4-57fb-4c0f-ae31-32791610cb14_37691ec4-57fb-4c0f-ae31-32791610cb14",
181+
},
182+
} as any,
183+
{
184+
kind: "communicationUser",
185+
communicationUserId:
186+
"8:acs:37691ec4-57fb-4c0f-ae31-32791610cb14_37691ec4-57fb-4c0f-ae31-32791610cb14",
187+
}
188+
);
164189
assertDeserialize(
165190
{ phoneNumber: { value: "+1234555000" }, rawId: "4:+1234555000" },
166191
{ kind: "phoneNumber", phoneNumber: "+1234555000", rawId: "4:+1234555000" }

0 commit comments

Comments
 (0)