Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix faulty json.Marshal behavior for embeds types.NetConf #1097

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func (n *IPNet) UnmarshalJSON(data []byte) error {
return nil
}

// NetConf describes a network.
type NetConf struct {
// NetConfType describes a network.
type NetConfType struct {
CNIVersion string `json:"cniVersion,omitempty"`

Name string `json:"name,omitempty"`
Expand All @@ -73,6 +73,9 @@ type NetConf struct {
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,
// the container ID and ifname pair that represents a
// still-valid attachment.
Expand All @@ -83,11 +86,11 @@ type GCAttachment struct {

// Note: DNS should be omit if DNS is empty but default 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()
type fixObjType = NetConf

bytes, err := json.Marshal(fixObjType(*n)) //nolint:all
bytes, err := json.Marshal(fixObjType(*n))
if err != nil {
return nil, err
}
Expand Down
Loading