From 70f441bc49afa4e9d10c27d7ed5733c4df7bddd3 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Tue, 5 Dec 2017 22:38:36 -0800 Subject: encoding/json: error when trying to set an embedded pointer to unexported struct types This CL reverts CL 76851 and takes a different approach to #21357. The changes in encode.go and encode_test.go are reverts that rolls back the changed behavior in CL 76851 where embedded pointers to unexported struct types were unilaterally ignored in both marshal and unmarshal. Instead, these fields are handled as before with the exception that it returns an error when Unmarshal is unable to set an unexported field. The behavior of Marshal is now unchanged with regards to #21357. This policy maintains the greatest degree of backwards compatibility and avoids silently discarding data the user may have expected to be present. Fixes #21357 Change-Id: I7dc753280c99f786ac51acf7e6c0246618c8b2b1 Reviewed-on: https://go-review.googlesource.com/82135 Run-TryBot: Joe Tsai TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/encoding/json/encode_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src/encoding/json/encode_test.go') diff --git a/src/encoding/json/encode_test.go b/src/encoding/json/encode_test.go index df7338c98d..0f194e13d2 100644 --- a/src/encoding/json/encode_test.go +++ b/src/encoding/json/encode_test.go @@ -364,9 +364,8 @@ func TestAnonymousFields(t *testing.T) { want: `{"X":2,"Y":4}`, }, { // Exported fields of pointers to embedded structs should have their - // exported fields be serialized only for exported struct types. - // Pointers to unexported structs are not allowed since the decoder - // is unable to allocate a struct for that field + // exported fields be serialized regardless of whether the struct types + // themselves are exported. label: "EmbeddedStructPointer", makeInput: func() interface{} { type ( @@ -379,7 +378,7 @@ func TestAnonymousFields(t *testing.T) { ) return S{&s1{1, 2}, &S2{3, 4}} }, - want: `{"Y":4}`, + want: `{"X":2,"Y":4}`, }, { // Exported fields on embedded unexported structs at multiple levels // of nesting should still be serialized. -- cgit v1.3