Merge pull request #50826 from austinvazquez/remove-legacy-marshal-json-function

api/types/registry: move `ServiceConfig` legacy field marshaling support into daemon backend
This commit is contained in:
Austin Vazquez
2025-08-29 09:56:45 -07:00
committed by GitHub
5 changed files with 84 additions and 71 deletions

View File

@@ -17,23 +17,6 @@ type ServiceConfig struct {
ExtraFields map[string]any `json:"-"`
}
// MarshalJSON implements a custom marshaler to include legacy fields
// in API responses.
func (sc *ServiceConfig) MarshalJSON() ([]byte, error) {
type tmp ServiceConfig
base, err := json.Marshal((*tmp)(sc))
if err != nil {
return nil, err
}
var merged map[string]any
_ = json.Unmarshal(base, &merged)
for k, v := range sc.ExtraFields {
merged[k] = v
}
return json.Marshal(merged)
}
// NetIPNet is the net.IPNet type, which can be marshalled and
// unmarshalled to JSON
type NetIPNet net.IPNet

View File

@@ -1,32 +0,0 @@
package registry
import (
"encoding/json"
"testing"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestServiceConfigMarshalLegacyFields(t *testing.T) {
t.Run("without legacy fields", func(t *testing.T) {
b, err := json.Marshal(&ServiceConfig{})
assert.NilError(t, err)
const expected = `{"IndexConfigs":null,"InsecureRegistryCIDRs":null,"Mirrors":null}`
assert.Check(t, is.Equal(string(b), expected), "Legacy nondistributable-artifacts fields should be omitted in output")
})
// Legacy fields should be returned when set to an empty slice. This is
// used for API versions < 1.49.
t.Run("with legacy fields", func(t *testing.T) {
b, err := json.Marshal(&ServiceConfig{
ExtraFields: map[string]any{
"AllowNondistributableArtifactsCIDRs": json.RawMessage(nil),
"AllowNondistributableArtifactsHostnames": json.RawMessage(nil),
},
})
assert.NilError(t, err)
const expected = `{"AllowNondistributableArtifactsCIDRs":null,"AllowNondistributableArtifactsHostnames":null,"IndexConfigs":null,"InsecureRegistryCIDRs":null,"Mirrors":null}`
assert.Check(t, is.Equal(string(b), expected))
})
}