From 269dfcc9dd8c339383fd2a2d4b5101520e4225f6 Mon Sep 17 00:00:00 2001
From: 178inaba <178inaba.git@gmail.com>
Date: Tue, 31 Mar 2020 04:28:07 +0900
Subject: [PATCH] Set maxParam with SetParamNames (#1535)

* Set maxParam with SetParamNames

Fixes #1492

* Revert go.mod
---
 bind_test.go           | 2 --
 context.go             | 6 ++----
 context_test.go        | 5 ++---
 middleware/jwt_test.go | 2 --
 4 files changed, 4 insertions(+), 11 deletions(-)

diff --git a/bind_test.go b/bind_test.go
index 943cfd55..b9fb9de3 100644
--- a/bind_test.go
+++ b/bind_test.go
@@ -332,7 +332,6 @@ func TestBindbindData(t *testing.T) {
 
 func TestBindParam(t *testing.T) {
 	e := New()
-	*e.maxParam = 2
 	req := httptest.NewRequest(GET, "/", nil)
 	rec := httptest.NewRecorder()
 	c := e.NewContext(req, rec)
@@ -363,7 +362,6 @@ func TestBindParam(t *testing.T) {
 	// Bind something with param and post data payload
 	body := bytes.NewBufferString(`{ "name": "Jon Snow" }`)
 	e2 := New()
-	*e2.maxParam = 2
 	req2 := httptest.NewRequest(POST, "/", body)
 	req2.Header.Set(HeaderContentType, MIMEApplicationJSON)
 
diff --git a/context.go b/context.go
index dfcbe16c..99ef03bc 100644
--- a/context.go
+++ b/context.go
@@ -310,6 +310,7 @@ func (c *context) ParamNames() []string {
 
 func (c *context) SetParamNames(names ...string) {
 	c.pnames = names
+	*c.echo.maxParam = len(names)
 }
 
 func (c *context) ParamValues() []string {
@@ -317,10 +318,7 @@ func (c *context) ParamValues() []string {
 }
 
 func (c *context) SetParamValues(values ...string) {
-	// NOTE: Don't just set c.pvalues = values, because it has to have length c.echo.maxParam at all times
-	for i, val := range values {
-		c.pvalues[i] = val
-	}
+	c.pvalues = values
 }
 
 func (c *context) QueryParam(name string) string {
diff --git a/context_test.go b/context_test.go
index 866d0643..73e5dcb6 100644
--- a/context_test.go
+++ b/context_test.go
@@ -93,7 +93,6 @@ func (responseWriterErr) WriteHeader(statusCode int) {
 
 func TestContext(t *testing.T) {
 	e := New()
-	*e.maxParam = 1
 	req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
 	rec := httptest.NewRecorder()
 	c := e.NewContext(req, rec).(*context)
@@ -472,7 +471,6 @@ func TestContextPath(t *testing.T) {
 
 func TestContextPathParam(t *testing.T) {
 	e := New()
-	*e.maxParam = 2
 	req := httptest.NewRequest(http.MethodGet, "/", nil)
 	c := e.NewContext(req, nil)
 
@@ -491,7 +489,8 @@ func TestContextPathParam(t *testing.T) {
 
 func TestContextGetAndSetParam(t *testing.T) {
 	e := New()
-	*e.maxParam = 2
+	r := e.Router()
+	r.Add(http.MethodGet, "/:foo", func(Context) error { return nil })
 	req := httptest.NewRequest(http.MethodGet, "/:foo", nil)
 	c := e.NewContext(req, nil)
 	c.SetParamNames("foo")
diff --git a/middleware/jwt_test.go b/middleware/jwt_test.go
index 1731d90f..ce44f9c9 100644
--- a/middleware/jwt_test.go
+++ b/middleware/jwt_test.go
@@ -60,8 +60,6 @@ func TestJWTRace(t *testing.T) {
 
 func TestJWT(t *testing.T) {
 	e := echo.New()
-	r := e.Router()
-	r.Add("GET", "/:jwt", func(echo.Context) error { return nil })
 	handler := func(c echo.Context) error {
 		return c.String(http.StatusOK, "test")
 	}