From 01772f001a7a16042c52704715b5e17577a47579 Mon Sep 17 00:00:00 2001 From: Clark Winters Date: Fri, 25 Nov 2022 19:12:50 -0600 Subject: [PATCH 1/2] bindMap key value should only be set when not already present --- ctx.go | 4 +++- ctx_test.go | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/ctx.go b/ctx.go index e07ce30da6..bd4de26188 100644 --- a/ctx.go +++ b/ctx.go @@ -1437,7 +1437,9 @@ func (c *Ctx) renderExtensions(bind interface{}) { // Bind view map if c.viewBindMap != nil { for _, v := range c.viewBindMap.D { - bindMap[v.Key] = v.Value + if _, ok := bindMap[v.Key]; !ok { + bindMap[v.Key] = v.Value + } } } diff --git a/ctx_test.go b/ctx_test.go index 0d20898065..c2b2c2d9e6 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -2851,6 +2851,29 @@ func Test_Ctx_RenderWithBind(t *testing.T) { } +func Test_Ctx_RenderWithOverwrittenBind(t *testing.T) { + t.Parallel() + + app := New() + c := app.AcquireCtx(&fasthttp.RequestCtx{}) + + err := c.Bind(Map{ + "Title": "Hello, World!", + }) + utils.AssertEqual(t, nil, err) + defer app.ReleaseCtx(c) + err = c.Render("./.github/testdata/index.tmpl", Map{ + "Title": "Hello from Fiber!", + }) + + buf := bytebufferpool.Get() + _, _ = buf.WriteString("overwrite") + defer bytebufferpool.Put(buf) + + utils.AssertEqual(t, nil, err) + utils.AssertEqual(t, "

Hello from Fiber!

", string(c.Response().Body())) +} + func Test_Ctx_RenderWithBindLocals(t *testing.T) { t.Parallel() From 8aef9dd397075865e889b440b13c7f40b4c3ead9 Mon Sep 17 00:00:00 2001 From: Clark Winters Date: Fri, 25 Nov 2022 19:45:15 -0600 Subject: [PATCH 2/2] add comment --- ctx.go | 1 + 1 file changed, 1 insertion(+) diff --git a/ctx.go b/ctx.go index bd4de26188..92d7fad985 100644 --- a/ctx.go +++ b/ctx.go @@ -1437,6 +1437,7 @@ func (c *Ctx) renderExtensions(bind interface{}) { // Bind view map if c.viewBindMap != nil { for _, v := range c.viewBindMap.D { + // make sure key does not exist already if _, ok := bindMap[v.Key]; !ok { bindMap[v.Key] = v.Value }