diff --git a/packages/jsii-kernel/lib/serialization.ts b/packages/jsii-kernel/lib/serialization.ts index f12b98b1de..7c501f527d 100644 --- a/packages/jsii-kernel/lib/serialization.ts +++ b/packages/jsii-kernel/lib/serialization.ts @@ -298,10 +298,9 @@ export const SERIALIZERS: {[k: string]: Serializer} = { // Similarly to other end, we might be getting a reference type where we're // expecting a value type. Accept this for now. - const prevRef = objectReference(value); - if (prevRef) { + if (isObjRef(value)) { host.debug('Expected value type but got reference type, accepting for now (awslabs/jsii#400)'); - return prevRef; + return host.objects.findObject(value).instance; } const namedType = host.lookupType((optionalValue.type as spec.NamedTypeReference).fqn); diff --git a/packages/jsii-kernel/test/test.kernel.ts b/packages/jsii-kernel/test/test.kernel.ts index a344676082..7d86cf5129 100644 --- a/packages/jsii-kernel/test/test.kernel.ts +++ b/packages/jsii-kernel/test/test.kernel.ts @@ -1158,6 +1158,14 @@ defineTest('erased base: can receive an instance of private type', async (test, test.deepEqual(objref.result, { [api.TOKEN_REF]: 'jsii-calc.JSII417PublicBaseOfBase@10000' }); }); +defineTest('deserialize a struct by reference', async (test, sandbox) => { + sandbox.callbackHandler = makeSyncCallbackHandler(() => 'xoxoxox'); + const objref = sandbox.create({ fqn: 'Object', overrides: [ { property: 'field' } ] }); + const consumer = sandbox.create({ fqn: 'jsii-calc.OptionalStructConsumer', args: [ objref ] }); + const value = sandbox.get({ objref: consumer, property: 'fieldValue' }); + test.deepEqual(value, { value: 'xoxoxox' }); +}); + // ================================================================================================= const testNames: { [name: string]: boolean } = { };