This is an automated email from the ASF dual-hosted git repository.

baoyuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 4a2b50fff fix: upstream nodes metadata miss (#2773)
4a2b50fff is described below

commit 4a2b50fff0a60e38224116b565a35828b197a8fb
Author: Baoyuan <[email protected]>
AuthorDate: Fri Mar 17 18:24:05 2023 +0800

    fix: upstream nodes metadata miss (#2773)
---
 api/internal/core/entity/format.go      |  5 +++++
 api/internal/core/entity/format_test.go | 37 +++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/api/internal/core/entity/format.go 
b/api/internal/core/entity/format.go
index dcc50c480..d7df2b13c 100644
--- a/api/internal/core/entity/format.go
+++ b/api/internal/core/entity/format.go
@@ -99,6 +99,11 @@ func NodesFormat(obj interface{}) interface{} {
                        if _, ok := val["priority"]; ok {
                                node.Priority = int(val["priority"].(float64))
                        }
+
+                       if _, ok := val["metadata"]; ok {
+                               node.Metadata = 
val["metadata"].(map[string]interface{})
+                       }
+
                        nodes = append(nodes, node)
                }
                return nodes
diff --git a/api/internal/core/entity/format_test.go 
b/api/internal/core/entity/format_test.go
index b3b5d299e..ef1254204 100644
--- a/api/internal/core/entity/format_test.go
+++ b/api/internal/core/entity/format_test.go
@@ -385,3 +385,40 @@ func TestMapKV2Node(t *testing.T) {
                })
        }
 }
+
+func TestNodesFormatWithMetadata(t *testing.T) {
+       // route data saved in ETCD
+       routeStr := `{
+               "uris": ["/*"],
+               "upstream": {
+                       "type": "roundrobin",
+                       "nodes": [{
+                               "host": "127.0.0.1",
+                               "port": 80,
+                               "weight": 0,
+                               "priority":10,
+                               "metadata": {
+                                       "name": "test"
+                               }
+                       }]
+               }
+       }`
+
+       // bind struct
+       var route Route
+       err := json.Unmarshal([]byte(routeStr), &route)
+       assert.Nil(t, err)
+
+       // nodes format
+       nodes := NodesFormat(route.Upstream.Nodes)
+
+       // json encode for client
+       res, err := json.Marshal(nodes)
+       assert.Nil(t, err)
+       jsonStr := string(res)
+       assert.Contains(t, jsonStr, `"weight":0`)
+       assert.Contains(t, jsonStr, `"port":80`)
+       assert.Contains(t, jsonStr, `"host":"127.0.0.1"`)
+       assert.Contains(t, jsonStr, `"priority":10`)
+       assert.Contains(t, jsonStr, `"metadata":{"name":"test"}`)
+}

Reply via email to