Skip to content

api/types: use "omitzero" instead of "omitempty" for "netip" fields#51932

Merged
thaJeztah merged 1 commit intomoby:masterfrom
thaJeztah:api_omitzero
Jan 26, 2026
Merged

api/types: use "omitzero" instead of "omitempty" for "netip" fields#51932
thaJeztah merged 1 commit intomoby:masterfrom
thaJeztah:api_omitzero

Conversation

@thaJeztah
Copy link
Member

@thaJeztah thaJeztah commented Jan 26, 2026

Pull request 50956 (88adc28) updated various types in the API module from a string to a netip.Prefix or netip.Addr. A side-effect of this was that zero values would no longer be omitted, and instead marshaled as an empty string;

package main

import (
    "encoding/json"
    "fmt"
    "net/netip"
)

type Foo struct {
    OmitEmpty netip.Prefix `json:",omitempty"`
    OmitZero  netip.Prefix `json:",omitzero"`
}

func main() {
    out, _ := json.Marshal(Foo{})
    fmt.Println(string(out))
}

The above produces {"OmitEmpty":""}, not omitting the empty address.

This patch;

  • updates most types to use omitzero instead of omitempty.
  • adds explicit json names to fields.

There's one type remaining that uses omitzero, but it's generated by go-swagger, which currently doesn't support omitzero; the PortSummary.IP;

// PortSummary Describes a port-mapping between the container and the host.
//
// Example: {"PrivatePort":8080,"PublicPort":80,"Type":"tcp"}
//
// swagger:model PortSummary
type PortSummary struct {
// Host IP address that the container's port is mapped to
IP netip.Addr `json:"IP,omitempty"`

- What I did

- How I did it

- How to verify it

docker network create foo
docker run -d --name foo --network name=foo,ip=172.19.0.10 nginx:alpine

Before this patch:

docker container inspect foo | jq .[].NetworkSettings.Networks.foo.IPAMConfig
{
  "IPv4Address": "172.19.0.10",
  "IPv6Address": ""
}

With this patch:

docker container inspect foo | jq .[].NetworkSettings.Networks.foo.IPAMConfig
{
  "IPv4Address": "172.19.0.10"
}

- Human readable description for the release notes

Fix some empty fields not being omitted in API responses.

- A picture of a cute animal (not mandatory but encouraged)

Pull request 50956 (88adc28) updated
various types in the API module from a string to a `netip.Prefix` or
`netip.Addr`. A side-effect of this was that zero values would no longer
be omitted, and instead marshaled as an empty string;

    package main

    import (
        "encoding/json"
        "fmt"
        "net/netip"
    )

    type Foo struct {
        OmitEmpty netip.Prefix `json:",omitempty"`
        OmitZero  netip.Prefix `json:",omitzero"`
    }

    func main() {
        out, _ := json.Marshal(Foo{})
        fmt.Println(string(out))
    }

The above produces `{"OmitEmpty":""}`, not omitting the empty address.

This patch;

- updates most types to use `omitzero` instead of `omitempty`.
- adds explicit `json` names to fields.

There's one type remaining that uses `omitzero`, but it's generated by
go-swagger, which currently doesn't support `omitzero`; the `PortSummary.IP`;
https://github.com/moby/moby/blob/335f60509fde10cf8a148ef96f67faeba6517a4b/api/types/container/port_summary.go#L12-L20

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah thaJeztah merged commit 31ffe93 into moby:master Jan 26, 2026
331 of 338 checks passed
@thaJeztah thaJeztah deleted the api_omitzero branch January 26, 2026 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docker network inspect ipam config with empty IPRange

2 participants