From 7f7343099e887e9627167447108b1772f4513e54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9F=A7=91=F0=9F=8F=BB=E2=80=8D=F0=9F=92=BB=20Romain=20M?= =?UTF-8?q?arcadier?= Date: Mon, 11 Apr 2022 14:53:56 +0200 Subject: [PATCH] fix(go): object type not recognized When a struct pointer was passed in an `interface{}` position, the converter would trip and register a new opaque instance of `Object` instead of correctly serializing a struct. This is because hte pointer is double-indirected (the `interface{}` is a reference to the `&struct`, which is a reference to the `struct`). This change adds a condition to detect such cases (`interface{}` abstracts another pointer), and properly serialize that. Fixes #2880 --- .../jsii-runtime-go/internal/kernel/conversions.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/@jsii/go-runtime/jsii-runtime-go/internal/kernel/conversions.go b/packages/@jsii/go-runtime/jsii-runtime-go/internal/kernel/conversions.go index 2897f89efa..aa3eae82e5 100644 --- a/packages/@jsii/go-runtime/jsii-runtime-go/internal/kernel/conversions.go +++ b/packages/@jsii/go-runtime/jsii-runtime-go/internal/kernel/conversions.go @@ -65,7 +65,7 @@ func (c *Client) castAndSetToPtr(ptr reflect.Value, data reflect.Value) { targetType := ptr.Type() if typ, ok := c.Types().FindType(ref.TypeFQN()); ok && typ.AssignableTo(ptr.Type()) { - // Specialize the return type to be the dynamic value type + // Specialize the return type to be the dynamic value type targetType = typ } @@ -191,6 +191,11 @@ func (c *Client) CastPtrToRef(dataVal reflect.Value) interface{} { }, } } + } else if dataVal.Elem().Kind() == reflect.Ptr { + // Typically happens when a struct pointer is passed into an interface{} + // typed API (such as a place where a union is accepted). + elemVal := dataVal.Elem() + return c.CastPtrToRef(elemVal) } if ref, err := c.ManageObject(dataVal); err != nil {