From e77cda217a5195cb3c551e56437c3f547a32ea0c Mon Sep 17 00:00:00 2001 From: Baoyuan Date: Thu, 16 Mar 2023 15:07:47 +0800 Subject: [PATCH 1/2] fix: upstream nodes metadata miss --- 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 c13bb6cb91..a9f37502c5 100644 --- a/api/internal/core/entity/format.go +++ b/api/internal/core/entity/format.go @@ -92,6 +92,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 109aa384e8..a0f0fbb984 100644 --- a/api/internal/core/entity/format_test.go +++ b/api/internal/core/entity/format_test.go @@ -293,3 +293,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"}`) +} From aaa3158e9175b88f8d24945c6cdb64ec74e991ff Mon Sep 17 00:00:00 2001 From: Baoyuan Date: Thu, 16 Mar 2023 15:49:31 +0800 Subject: [PATCH 2/2] fix: lint code --- api/internal/core/entity/format_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/internal/core/entity/format_test.go b/api/internal/core/entity/format_test.go index a0f0fbb984..c7b7dcf4ca 100644 --- a/api/internal/core/entity/format_test.go +++ b/api/internal/core/entity/format_test.go @@ -307,7 +307,7 @@ func TestNodesFormatWithMetadata(t *testing.T) { "priority":10, "metadata": { "name": "test" - } + } }] } }`