Skip to content

Commit 1f7ee05

Browse files
authored
Merge pull request #433 from AllenX2018/fix-anonymous-struct-error-message
fix issue #421
2 parents b22f393 + 6f4c196 commit 1f7ee05

File tree

2 files changed

+234
-11
lines changed

2 files changed

+234
-11
lines changed

misc_tests/jsoniter_object_test.go

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package misc_tests
22

33
import (
44
"bytes"
5+
"reflect"
56
"testing"
67

78
"github.com/json-iterator/go"
@@ -147,3 +148,225 @@ func Test_unmarshal_into_existing_value(t *testing.T) {
147148
"k": "v",
148149
}, m)
149150
}
151+
152+
// for issue421
153+
func Test_unmarshal_anonymous_struct_invalid(t *testing.T) {
154+
should := require.New(t)
155+
t0 := struct {
156+
Field1 string
157+
}{}
158+
159+
cfg := jsoniter.ConfigCompatibleWithStandardLibrary
160+
err := cfg.UnmarshalFromString(`{"Field1":`, &t0)
161+
should.NotNil(err)
162+
should.NotContains(err.Error(), reflect.TypeOf(t0).String())
163+
164+
cfgCaseSensitive := jsoniter.Config{
165+
CaseSensitive: true,
166+
}.Froze()
167+
168+
type TestObject1 struct {
169+
Field1 struct {
170+
InnerField1 string
171+
}
172+
}
173+
t1 := TestObject1{}
174+
err = cfgCaseSensitive.UnmarshalFromString(`{"Field1":{"InnerField1"`, &t1)
175+
should.NotNil(err)
176+
should.NotContains(err.Error(), reflect.TypeOf(t1.Field1).String())
177+
should.Contains(err.Error(), reflect.TypeOf(t1).String())
178+
179+
type TestObject2 struct {
180+
Field1 int
181+
Field2 struct {
182+
InnerField1 string
183+
InnerField2 string
184+
}
185+
}
186+
t2 := TestObject2{}
187+
err = cfgCaseSensitive.UnmarshalFromString(`{"Field2":{"InnerField2"`, &t2)
188+
should.NotNil(err)
189+
should.NotContains(err.Error(), reflect.TypeOf(t2.Field2).String())
190+
should.Contains(err.Error(), reflect.TypeOf(t2).String())
191+
192+
type TestObject3 struct {
193+
Field1 int
194+
Field2 int
195+
Field3 struct {
196+
InnerField1 string
197+
InnerField2 string
198+
InnerField3 string
199+
}
200+
}
201+
t3 := TestObject3{}
202+
err = cfgCaseSensitive.UnmarshalFromString(`{"Field3":{"InnerField3"`, &t3)
203+
should.NotNil(err)
204+
should.NotContains(err.Error(), reflect.TypeOf(t3.Field3).String())
205+
should.Contains(err.Error(), reflect.TypeOf(t3).String())
206+
207+
type TestObject4 struct {
208+
Field1 int
209+
Field2 int
210+
Field3 int
211+
Field4 struct {
212+
InnerField1 string
213+
InnerField2 string
214+
InnerField3 string
215+
InnerField4 string
216+
}
217+
}
218+
t4 := TestObject4{}
219+
err = cfgCaseSensitive.UnmarshalFromString(`{"Field4":{"InnerField4"`, &t4)
220+
should.NotNil(err)
221+
should.NotContains(err.Error(), reflect.TypeOf(t4.Field4).String())
222+
should.Contains(err.Error(), reflect.TypeOf(t4).String())
223+
224+
type TestObject5 struct {
225+
Field1 int
226+
Field2 int
227+
Field3 int
228+
Field4 int
229+
Field5 struct {
230+
InnerField1 string
231+
InnerField2 string
232+
InnerField3 string
233+
InnerField4 string
234+
InnerField5 string
235+
}
236+
}
237+
t5 := TestObject5{}
238+
err = cfgCaseSensitive.UnmarshalFromString(`{"Field5":{"InnerField5"`, &t5)
239+
should.NotNil(err)
240+
should.NotContains(err.Error(), reflect.TypeOf(t5.Field5).String())
241+
should.Contains(err.Error(), reflect.TypeOf(t5).String())
242+
243+
type TestObject6 struct {
244+
Field1 int
245+
Field2 int
246+
Field3 int
247+
Field4 int
248+
Field5 int
249+
Field6 struct {
250+
InnerField1 string
251+
InnerField2 string
252+
InnerField3 string
253+
InnerField4 string
254+
InnerField5 string
255+
InnerField6 string
256+
}
257+
}
258+
t6 := TestObject6{}
259+
err = cfgCaseSensitive.UnmarshalFromString(`{"Field6":{"InnerField6"`, &t6)
260+
should.NotNil(err)
261+
should.NotContains(err.Error(), reflect.TypeOf(t6.Field6).String())
262+
should.Contains(err.Error(), reflect.TypeOf(t6).String())
263+
264+
type TestObject7 struct {
265+
Field1 int
266+
Field2 int
267+
Field3 int
268+
Field4 int
269+
Field5 int
270+
Field6 int
271+
Field7 struct {
272+
InnerField1 string
273+
InnerField2 string
274+
InnerField3 string
275+
InnerField4 string
276+
InnerField5 string
277+
InnerField6 string
278+
InnerField7 string
279+
}
280+
}
281+
t7 := TestObject7{}
282+
err = cfgCaseSensitive.UnmarshalFromString(`{"Field7":{"InnerField7"`, &t7)
283+
should.NotNil(err)
284+
should.NotContains(err.Error(), reflect.TypeOf(t7.Field7).String())
285+
should.Contains(err.Error(), reflect.TypeOf(t7).String())
286+
287+
type TestObject8 struct {
288+
Field1 int
289+
Field2 int
290+
Field3 int
291+
Field4 int
292+
Field5 int
293+
Field6 int
294+
Field7 int
295+
Field8 struct {
296+
InnerField1 string
297+
InnerField2 string
298+
InnerField3 string
299+
InnerField4 string
300+
InnerField5 string
301+
InnerField6 string
302+
InnerField7 string
303+
InnerField8 string
304+
}
305+
}
306+
t8 := TestObject8{}
307+
err = cfgCaseSensitive.UnmarshalFromString(`{"Field8":{"InnerField8"`, &t8)
308+
should.NotNil(err)
309+
should.NotContains(err.Error(), reflect.TypeOf(t8.Field8).String())
310+
should.Contains(err.Error(), reflect.TypeOf(t8).String())
311+
312+
type TestObject9 struct {
313+
Field1 int
314+
Field2 int
315+
Field3 int
316+
Field4 int
317+
Field5 int
318+
Field6 int
319+
Field7 int
320+
Field8 int
321+
Field9 struct {
322+
InnerField1 string
323+
InnerField2 string
324+
InnerField3 string
325+
InnerField4 string
326+
InnerField5 string
327+
InnerField6 string
328+
InnerField7 string
329+
InnerField8 string
330+
InnerField9 string
331+
}
332+
}
333+
t9 := TestObject9{}
334+
err = cfgCaseSensitive.UnmarshalFromString(`{"Field9":{"InnerField9"`, &t9)
335+
should.NotNil(err)
336+
should.NotContains(err.Error(), reflect.TypeOf(t9.Field9).String())
337+
should.Contains(err.Error(), reflect.TypeOf(t9).String())
338+
339+
type TestObject10 struct {
340+
Field1 int
341+
Field2 int
342+
Field3 int
343+
Field4 int
344+
Field5 int
345+
Field6 int
346+
Field7 int
347+
Field8 int
348+
Field9 int
349+
Field10 struct {
350+
InnerField1 string
351+
InnerField2 string
352+
InnerField3 string
353+
InnerField4 string
354+
InnerField5 string
355+
InnerField6 string
356+
InnerField7 string
357+
InnerField8 string
358+
InnerField9 string
359+
InnerField10 string
360+
}
361+
}
362+
t10 := TestObject10{}
363+
err = cfgCaseSensitive.UnmarshalFromString(`{"Field10":{"InnerField10"`, &t10)
364+
should.NotNil(err)
365+
should.NotContains(err.Error(), reflect.TypeOf(t10.Field10).String())
366+
should.Contains(err.Error(), reflect.TypeOf(t10).String())
367+
368+
err = cfg.UnmarshalFromString(`{"Field10":{"InnerField10"`, &t10)
369+
should.NotNil(err)
370+
should.NotContains(err.Error(), reflect.TypeOf(t10.Field10).String())
371+
should.Contains(err.Error(), reflect.TypeOf(t10).String())
372+
}

reflect_struct_decoder.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ func (decoder *generalStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator)
507507
for c = ','; c == ','; c = iter.nextToken() {
508508
decoder.decodeOneField(ptr, iter)
509509
}
510-
if iter.Error != nil && iter.Error != io.EOF {
510+
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
511511
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
512512
}
513513
if c != '}' {
@@ -588,7 +588,7 @@ func (decoder *oneFieldStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator)
588588
break
589589
}
590590
}
591-
if iter.Error != nil && iter.Error != io.EOF {
591+
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
592592
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
593593
}
594594
iter.decrementDepth()
@@ -622,7 +622,7 @@ func (decoder *twoFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator
622622
break
623623
}
624624
}
625-
if iter.Error != nil && iter.Error != io.EOF {
625+
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
626626
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
627627
}
628628
iter.decrementDepth()
@@ -660,7 +660,7 @@ func (decoder *threeFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterat
660660
break
661661
}
662662
}
663-
if iter.Error != nil && iter.Error != io.EOF {
663+
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
664664
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
665665
}
666666
iter.decrementDepth()
@@ -702,7 +702,7 @@ func (decoder *fourFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterato
702702
break
703703
}
704704
}
705-
if iter.Error != nil && iter.Error != io.EOF {
705+
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
706706
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
707707
}
708708
iter.decrementDepth()
@@ -748,7 +748,7 @@ func (decoder *fiveFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterato
748748
break
749749
}
750750
}
751-
if iter.Error != nil && iter.Error != io.EOF {
751+
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
752752
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
753753
}
754754
iter.decrementDepth()
@@ -798,7 +798,7 @@ func (decoder *sixFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator
798798
break
799799
}
800800
}
801-
if iter.Error != nil && iter.Error != io.EOF {
801+
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
802802
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
803803
}
804804
iter.decrementDepth()
@@ -852,7 +852,7 @@ func (decoder *sevenFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterat
852852
break
853853
}
854854
}
855-
if iter.Error != nil && iter.Error != io.EOF {
855+
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
856856
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
857857
}
858858
iter.decrementDepth()
@@ -910,7 +910,7 @@ func (decoder *eightFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterat
910910
break
911911
}
912912
}
913-
if iter.Error != nil && iter.Error != io.EOF {
913+
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
914914
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
915915
}
916916
iter.decrementDepth()
@@ -972,7 +972,7 @@ func (decoder *nineFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterato
972972
break
973973
}
974974
}
975-
if iter.Error != nil && iter.Error != io.EOF {
975+
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
976976
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
977977
}
978978
iter.decrementDepth()
@@ -1038,7 +1038,7 @@ func (decoder *tenFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator
10381038
break
10391039
}
10401040
}
1041-
if iter.Error != nil && iter.Error != io.EOF {
1041+
if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
10421042
iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
10431043
}
10441044
iter.decrementDepth()

0 commit comments

Comments
 (0)