Skip to content

Commit

Permalink
fix(kernel): json deserialization fails on null value (#4000)
Browse files Browse the repository at this point in the history
The JSON serialization class is a quasi-synonym for `Map<string, any>`, except it is meant to accept any valid JSON object, including one with `null` values. The serializer was however interpreting these as `Map<string, JSON>` when wired as a `$jsii.map` envelope, and this serialization class does NOT allow for null values.

Addresses the "easy" part of aws/aws-cdk#14639 (more complicated is that it would need to preserve `null` across the process boundary, which is currently not possible).

---

By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license].

[Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
  • Loading branch information
RomainMuller authored Mar 13, 2023
1 parent 536f79a commit 884b0ff
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions packages/@jsii/kernel/src/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ interface Serializer {
value: unknown,
type: OptionalValueOrVoid,
host: SerializerHost,
options?: { allowNullishMapValue?: boolean },
): any;
}

Expand Down Expand Up @@ -236,7 +237,6 @@ export const SERIALIZERS: { [k: string]: Serializer } = {
return SERIALIZERS[SerializationClass.Map].deserialize(
value,
{
optional: false,
type: {
collection: {
kind: spec.CollectionKind.Map,
Expand All @@ -245,6 +245,7 @@ export const SERIALIZERS: { [k: string]: Serializer } = {
},
},
host,
{ allowNullishMapValue: true },
);
}

Expand All @@ -266,9 +267,7 @@ export const SERIALIZERS: { [k: string]: Serializer } = {
host,
'deserialize',
toMap,
{
type: { primitive: spec.PrimitiveType.Json },
},
{ type: { primitive: spec.PrimitiveType.Json } },
typeof key === 'string' ? `key ${inspect(key)}` : `index ${key}`,
);
}
Expand Down Expand Up @@ -387,7 +386,12 @@ export const SERIALIZERS: { [k: string]: Serializer } = {
),
};
},
deserialize(value, optionalValue, host) {
deserialize(
value,
optionalValue,
host,
{ allowNullishMapValue = false } = {},
) {
if (nullAndOk(value, optionalValue)) {
return undefined;
}
Expand All @@ -401,7 +405,10 @@ export const SERIALIZERS: { [k: string]: Serializer } = {
host,
'deserialize',
v,
{ type: mapType.collection.elementtype },
{
optional: allowNullishMapValue,
type: mapType.collection.elementtype,
},
`key ${inspect(key)}`,
),
);
Expand All @@ -411,7 +418,10 @@ export const SERIALIZERS: { [k: string]: Serializer } = {
host,
'deserialize',
v,
{ type: mapType.collection.elementtype },
{
optional: allowNullishMapValue,
type: mapType.collection.elementtype,
},
`key ${inspect(key)}`,
),
);
Expand Down Expand Up @@ -1112,7 +1122,7 @@ export function process(
context: string,
) {
const wireTypes = serializationType(type, host.lookupType);
host.debug(serde, value, wireTypes);
host.debug(serde, value, ...wireTypes);

const errors = new Array<any>();
for (const { serializationClass, typeRef } of wireTypes) {
Expand Down

0 comments on commit 884b0ff

Please sign in to comment.