You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by ju...@apache.org on 2020/10/22 16:05:44 UTC

[apisix-dashboard] branch master updated: fix: check whether it is nil before using the variable (#589)

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

juzhiyuan 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 e133ca7  fix: check whether it is nil before using the variable (#589)
e133ca7 is described below

commit e133ca73a80f5a8de1e8316c6632081681f573f6
Author: nic-chen <33...@users.noreply.github.com>
AuthorDate: Fri Oct 23 00:02:44 2020 +0800

    fix: check whether it is nil before using the variable (#589)
---
 api/internal/handler/route/route.go          |  8 +++-
 api/internal/handler/route/route_test.go     | 60 ++++++++++++++++++++++++++++
 api/internal/handler/service/service.go      |  8 +++-
 api/internal/handler/service/service_test.go | 46 +++++++++++++++++++++
 4 files changed, 118 insertions(+), 4 deletions(-)

diff --git a/api/internal/handler/route/route.go b/api/internal/handler/route/route.go
index c0ba6a9..5794951 100644
--- a/api/internal/handler/route/route.go
+++ b/api/internal/handler/route/route.go
@@ -93,7 +93,9 @@ func (h *Handler) Get(c droplet.Context) (interface{}, error) {
 	}
 
 	//format
-	route.Upstream.Nodes = entity.NodesFormat(route.Upstream.Nodes)
+	if route.Upstream != nil && route.Upstream.Nodes != nil {
+		route.Upstream.Nodes = entity.NodesFormat(route.Upstream.Nodes)
+	}
 
 	return route, nil
 }
@@ -138,7 +140,9 @@ func (h *Handler) List(c droplet.Context) (interface{}, error) {
 		},
 		Format: func(obj interface{}) interface{} {
 			route := obj.(*entity.Route)
-			route.Upstream.Nodes = entity.NodesFormat(route.Upstream.Nodes)
+			if route.Upstream != nil && route.Upstream.Nodes != nil {
+				route.Upstream.Nodes = entity.NodesFormat(route.Upstream.Nodes)
+			}
 			return route
 		},
 		PageSize:   input.PageSize,
diff --git a/api/internal/handler/route/route_test.go b/api/internal/handler/route/route_test.go
index 0b23a49..3f79cd3 100644
--- a/api/internal/handler/route/route_test.go
+++ b/api/internal/handler/route/route_test.go
@@ -907,4 +907,64 @@ func TestRoute(t *testing.T) {
 	assert.NotNil(t, err)
 	assert.Equal(t, http.StatusBadRequest, ret.(*data.SpecCodeResponse).StatusCode)
 
+	//create route with out upstream
+	route11 := &entity.Route{}
+	reqBody = `{
+      "id": "11",
+      "name": "bbbbb",
+      "uri": "/r11",
+      "hosts": ["foo.com", "*.bar.com"],
+      "remote_addrs": ["127.0.0.0/8"],
+      "methods": ["PUT", "GET"],
+      "plugins": {
+          "limit-count": {
+              "count": 2,
+              "time_window": 60,
+              "rejected_code": 503,
+              "key": "remote_addr"
+          }
+      }
+  }`
+	json.Unmarshal([]byte(reqBody), route11)
+	ctx.SetInput(route11)
+	_, err = handler.Create(ctx)
+	assert.Nil(t, err)
+
+	//sleep
+	time.Sleep(time.Duration(100) * time.Millisecond)
+
+	//get
+	input11 := &GetInput{}
+	input11.ID = "11"
+	ctx.SetInput(input11)
+	ret, err = handler.Get(ctx)
+	assert.Nil(t, err)
+	stored = ret.(*entity.Route)
+	assert.Equal(t, "11", stored.ID)
+
+	//list
+	listInput11 := &ListInput{}
+	reqBody = `{"page_size": 10, "page": 1}`
+	json.Unmarshal([]byte(reqBody), listInput11)
+	ctx.SetInput(listInput11)
+	retPage, err = handler.List(ctx)
+	assert.Nil(t, err)
+
+	//list search match
+	listInput12 := &ListInput{}
+	reqBody = `{"page_size": 1, "page": 1,  "uri": "r11"}`
+	json.Unmarshal([]byte(reqBody), listInput12)
+	ctx.SetInput(listInput12)
+	retPage, err = handler.List(ctx)
+	assert.Nil(t, err)
+	dataPage = retPage.(*store.ListOutput)
+	assert.Equal(t, len(dataPage.Rows), 1)
+
+	//delete test data
+	reqBody = `{"ids": "11"}`
+	json.Unmarshal([]byte(reqBody), inputDel)
+	ctx.SetInput(inputDel)
+	_, err = handler.BatchDelete(ctx)
+	assert.Nil(t, err)
+
 }
diff --git a/api/internal/handler/service/service.go b/api/internal/handler/service/service.go
index 3c15b8e..64d4653 100644
--- a/api/internal/handler/service/service.go
+++ b/api/internal/handler/service/service.go
@@ -76,7 +76,9 @@ func (h *Handler) Get(c droplet.Context) (interface{}, error) {
 	}
 
 	service := r.(*entity.Service)
-	service.Upstream.Nodes = entity.NodesFormat(service.Upstream.Nodes)
+	if service.Upstream != nil && service.Upstream.Nodes != nil {
+		service.Upstream.Nodes = entity.NodesFormat(service.Upstream.Nodes)
+	}
 
 	return r, nil
 }
@@ -98,7 +100,9 @@ func (h *Handler) List(c droplet.Context) (interface{}, error) {
 		},
 		Format: func(obj interface{}) interface{} {
 			service := obj.(*entity.Service)
-			service.Upstream.Nodes = entity.NodesFormat(service.Upstream.Nodes)
+			if service.Upstream != nil && service.Upstream.Nodes != nil {
+				service.Upstream.Nodes = entity.NodesFormat(service.Upstream.Nodes)
+			}
 			return service
 		},
 		PageSize:   input.PageSize,
diff --git a/api/internal/handler/service/service_test.go b/api/internal/handler/service/service_test.go
index 0bf78eb..42efc4d 100644
--- a/api/internal/handler/service/service_test.go
+++ b/api/internal/handler/service/service_test.go
@@ -149,4 +149,50 @@ func TestService(t *testing.T) {
 	_, err = handler.BatchDelete(ctx)
 	assert.Nil(t, err)
 
+	//create without upstream
+	service11 := &entity.Service{}
+	reqBody = `{
+      "id": "11",
+      "plugins": {
+          "limit-count": {
+              "count": 2,
+              "time_window": 60,
+              "rejected_code": 503,
+              "key": "remote_addr"
+          }
+      }
+  }`
+	json.Unmarshal([]byte(reqBody), service11)
+	ctx.SetInput(service11)
+	_, err = handler.Create(ctx)
+	assert.Nil(t, err)
+
+	//sleep
+	time.Sleep(time.Duration(100) * time.Millisecond)
+
+	//get
+	input11 := &GetInput{}
+	input11.ID = "11"
+	ctx.SetInput(input11)
+	ret, err = handler.Get(ctx)
+	stored = ret.(*entity.Service)
+	assert.Nil(t, err)
+	assert.Equal(t, "11", stored.ID)
+
+	//list
+	listInput11 := &ListInput{}
+	reqBody = `{"page_size": 10, "page": 1}`
+	json.Unmarshal([]byte(reqBody), listInput11)
+	ctx.SetInput(listInput11)
+	retPage, err = handler.List(ctx)
+	assert.Nil(t, err)
+
+	//delete test data
+	inputDel11 := &BatchDelete{}
+	reqBody = `{"ids": "11"}`
+	json.Unmarshal([]byte(reqBody), inputDel11)
+	ctx.SetInput(inputDel11)
+	_, err = handler.BatchDelete(ctx)
+	assert.Nil(t, err)
+
 }