bzp2010 commented on code in PR #2538:
URL: 
https://github.com/apache/apisix-ingress-controller/pull/2538#discussion_r2317755677


##########
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:
   Perhaps it's best to do this during both translation and encoding.
   
   The implementation of the Marshaler interface is implicit; without examining 
the function implementations on UpstreamNodes, one cannot understand its 
specific operations. This requires familiarity with the codebase to explain why 
this data structure diverges from standard Go JSON encoding behavior.
   I agree this should serve as a fallback, but only as a fallback. This 
doesn't mean we should overlook potential errors in the translation process.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to