@@ -580,6 +580,10 @@ type TestMarshal_Embedded struct {
580
580
Foo string `json:"foo" groups:"test"`
581
581
}
582
582
583
+ type TestMarshal_NamedEmbedded struct {
584
+ Qux string `json:"qux" groups:"test"`
585
+ }
586
+
583
587
// TestMarshal_EmbeddedCustom is used to test an embedded struct with a custom marshaler that is not a pointer.
584
588
type TestMarshal_EmbeddedCustom struct {
585
589
Val int
@@ -610,14 +614,16 @@ func (t *TestMarshal_EmbeddedCustomPtr) MarshalJSON() ([]byte, error) {
610
614
611
615
type TestMarshal_EmbeddedParent struct {
612
616
* TestMarshal_Embedded
617
+ * TestMarshal_NamedEmbedded `json:"embedded"`
613
618
* TestMarshal_EmbeddedCustom `json:"value"`
614
619
* TestMarshal_EmbeddedCustomPtr `json:"value_ptr"`
615
- Bar string `json:"bar" groups:"test"`
620
+ Bar string `json:"bar" groups:"test"`
616
621
}
617
622
618
623
func TestMarshal_EmbeddedField (t * testing.T ) {
619
624
v := TestMarshal_EmbeddedParent {
620
625
& TestMarshal_Embedded {"Hello" },
626
+ & TestMarshal_NamedEmbedded {"Big" },
621
627
& TestMarshal_EmbeddedCustom {10 , true },
622
628
& TestMarshal_EmbeddedCustomPtr {20 , true },
623
629
"World" ,
@@ -630,15 +636,26 @@ func TestMarshal_EmbeddedField(t *testing.T) {
630
636
actual , err := json .Marshal (actualMap )
631
637
assert .NoError (t , err )
632
638
633
- expected , err := json . Marshal ( map [ string ] interface {} {
634
- "bar" : "World" ,
635
- "foo" : "Hello" ,
636
- "value" : 10 ,
637
- "value_ptr" : 20 ,
639
+ t . Run ( "should match the original json marshal" , func ( t * testing. T ) {
640
+ expected , err := json . Marshal ( v )
641
+ assert . NoError ( t , err )
642
+
643
+ assert . JSONEq ( t , string ( expected ), string ( actual ))
638
644
})
639
- assert .NoError (t , err )
640
645
641
- assert .Equal (t , string (expected ), string (actual ))
646
+ t .Run ("should match the expected map" , func (t * testing.T ) {
647
+ expectedMap , err := json .Marshal (map [string ]interface {}{
648
+ "bar" : "World" ,
649
+ "foo" : "Hello" ,
650
+ "value" : 10 ,
651
+ "value_ptr" : 20 ,
652
+ "embedded" : map [string ]interface {}{
653
+ "qux" : "Big" ,
654
+ },
655
+ })
656
+ assert .NoError (t , err )
657
+ assert .JSONEq (t , string (expectedMap ), string (actual ))
658
+ })
642
659
}
643
660
644
661
type TestMarshal_EmbeddedEmpty struct {
0 commit comments