Merge pull request #1097 from s1061123/fix/1096

Fix faulty json.Marshal behavior for embeds types.NetConf
This commit is contained in:
Casey Callendrello 2024-06-10 17:20:05 +02:00 committed by GitHub
commit 6e57c2ee5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 4 deletions

View File

@ -56,8 +56,8 @@ func (n *IPNet) UnmarshalJSON(data []byte) error {
return nil return nil
} }
// NetConf describes a network. // NetConfType describes a network.
type NetConf struct { type NetConfType struct {
CNIVersion string `json:"cniVersion,omitempty"` CNIVersion string `json:"cniVersion,omitempty"`
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
@ -73,6 +73,9 @@ type NetConf struct {
ValidAttachments []GCAttachment `json:"cni.dev/valid-attachments,omitempty"` ValidAttachments []GCAttachment `json:"cni.dev/valid-attachments,omitempty"`
} }
// NetConf is defined as different type as custom MarshalJSON() and issue #1096
type NetConf NetConfType
// GCAttachment is the parameters to a GC call -- namely, // GCAttachment is the parameters to a GC call -- namely,
// the container ID and ifname pair that represents a // the container ID and ifname pair that represents a
// still-valid attachment. // still-valid attachment.
@ -83,11 +86,11 @@ type GCAttachment struct {
// Note: DNS should be omit if DNS is empty but default Marshal function // Note: DNS should be omit if DNS is empty but default Marshal function
// will output empty structure hence need to write a Marshal function // will output empty structure hence need to write a Marshal function
func (n *NetConf) MarshalJSON() ([]byte, error) { func (n *NetConfType) MarshalJSON() ([]byte, error) {
// use type alias to escape recursion for json.Marshal() to MarshalJSON() // use type alias to escape recursion for json.Marshal() to MarshalJSON()
type fixObjType = NetConf type fixObjType = NetConf
bytes, err := json.Marshal(fixObjType(*n)) //nolint:all bytes, err := json.Marshal(fixObjType(*n))
if err != nil { if err != nil {
return nil, err return nil, err
} }