You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by mi...@apache.org on 2020/04/15 02:07:41 UTC

[trafficcontrol] branch master updated: Allow negative limit to receive all records back (#4598)

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

mitchell852 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new 86c1e09  Allow negative limit to receive all records back (#4598)
86c1e09 is described below

commit 86c1e0987a1ff3171840d5afd08de2e6553a952c
Author: Michael Hoppal <54...@users.noreply.github.com>
AuthorDate: Tue Apr 14 20:07:31 2020 -0600

    Allow negative limit to receive all records back (#4598)
    
    * Allow negative limit to recieve all records back
    
    * Fix typo
    
    Co-authored-by: shamrickus <sh...@gmail.com>
---
 traffic_ops/testing/api/v1/origins_test.go             |  8 ++++----
 traffic_ops/testing/api/v2/origins_test.go             |  8 ++++----
 traffic_ops/traffic_ops_golang/dbhelpers/db_helpers.go | 10 +++++++---
 3 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/traffic_ops/testing/api/v1/origins_test.go b/traffic_ops/testing/api/v1/origins_test.go
index 69c646d..f9ae5e9 100644
--- a/traffic_ops/testing/api/v1/origins_test.go
+++ b/traffic_ops/testing/api/v1/origins_test.go
@@ -174,11 +174,11 @@ func VerifyPaginationSupport(t *testing.T) {
 		t.Error("expected GET origins with limit = 1, page = 2 to return second result")
 	}
 
-	_, _, err = TOSession.GetOriginsByQueryParams("?limit=0")
+	_, _, err = TOSession.GetOriginsByQueryParams("?limit=-2")
 	if err == nil {
-		t.Error("expected GET origins to return an error when limit is not a positive integer")
-	} else if !strings.Contains(err.Error(), "must be a positive integer") {
-		t.Errorf("expected GET origins to return an error for limit is not a positive integer, actual error: " + err.Error())
+		t.Error("expected GET origins to return an error when limit is not bigger than -1")
+	} else if !strings.Contains(err.Error(), "must be bigger than -1") {
+		t.Errorf("expected GET origins to return an error for limit is not bigger than -1, actual error: " + err.Error())
 	}
 	_, _, err = TOSession.GetOriginsByQueryParams("?limit=1&offset=0")
 	if err == nil {
diff --git a/traffic_ops/testing/api/v2/origins_test.go b/traffic_ops/testing/api/v2/origins_test.go
index cc5c6b2..1c06a41 100644
--- a/traffic_ops/testing/api/v2/origins_test.go
+++ b/traffic_ops/testing/api/v2/origins_test.go
@@ -174,11 +174,11 @@ func VerifyPaginationSupport(t *testing.T) {
 		t.Error("expected GET origins with limit = 1, page = 2 to return second result")
 	}
 
-	_, _, err = TOSession.GetOriginsByQueryParams("?limit=0")
+	_, _, err = TOSession.GetOriginsByQueryParams("?limit=-2")
 	if err == nil {
-		t.Error("expected GET origins to return an error when limit is not a positive integer")
-	} else if !strings.Contains(err.Error(), "must be a positive integer") {
-		t.Errorf("expected GET origins to return an error for limit is not a positive integer, actual error: " + err.Error())
+		t.Error("expected GET origins to return an error when limit is not bigger than -1")
+	} else if !strings.Contains(err.Error(), "must be bigger than -1") {
+		t.Errorf("expected GET origins to return an error for limit is not bigger than -1, actual error: " + err.Error())
 	}
 	_, _, err = TOSession.GetOriginsByQueryParams("?limit=1&offset=0")
 	if err == nil {
diff --git a/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers.go b/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers.go
index aa9af11..6716d00 100644
--- a/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers.go
+++ b/traffic_ops/traffic_ops_golang/dbhelpers/db_helpers.go
@@ -157,12 +157,16 @@ func BuildWhereAndOrderByAndPagination(parameters map[string]string, queryParams
 	if limit, exists := parameters["limit"]; exists {
 		// try to convert to int, if it fails the limit parameter is invalid, so return an error
 		limitInt, err := strconv.Atoi(limit)
-		if err != nil || limitInt < 1 {
-			errs = append(errs, errors.New("limit parameter must be a positive integer"))
+		if err != nil || limitInt < -1 {
+			errs = append(errs, errors.New("limit parameter must be bigger than -1"))
 			return "", "", "", queryValues, errs
 		}
 		log.Debugln("limit: ", limit)
-		paginationClause += " " + limit
+		if limitInt == -1 {
+			paginationClause = ""
+		} else {
+			paginationClause += " " + limit
+		}
 		if offset, exists := parameters["offset"]; exists {
 			// check that offset is valid
 			offsetInt, err := strconv.Atoi(offset)