AlinsRan commented on code in PR #2538: URL: https://github.com/apache/apisix-ingress-controller/pull/2538#discussion_r2315257131
########## api/adc/types.go: ########## @@ -505,6 +505,20 @@ func (n *UpstreamNodes) UnmarshalJSON(p []byte) error { return nil } +// MarshalJSON implements the json.Marshaler interface for UpstreamNodes. +// By default, Go serializes a nil slice as JSON null. However, for compatibility +// with APISIX semantics, we want a nil UpstreamNodes to be encoded as an empty +// array ([]) instead of null. Non-nil slices are marshaled as usual. +// +// See APISIX upstream nodes schema definition for details: +// https://github.com/apache/apisix/blob/77dacda31277a31d6014b4970e36bae2a5c30907/apisix/schema_def.lua#L295-L338 +func (n UpstreamNodes) MarshalJSON() ([]byte, error) { + if n == nil { + return []byte("[]"), nil Review Comment: * UpstreamNodes is part of the external API contract; the schema requires it to always be an array ([]), never null. * Overriding MarshalJSON ensures consistent output regardless of whether the slice is initialized. * This is defensive programming, preventing new code or omissions from causing the API to return null and break the contract. * Compared to relying on every construction point to initialize the slice, this approach centralizes the handling and reduces maintenance overhead. * The comment clearly explains the purpose and includes a schema link for reference. https://go.dev/play/p/lS8wac0gRQZ -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@apisix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org