Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ctx.Keys is overwritten by obj in methods like ctx.HTML #4022

Open
Yiannis128 opened this issue Aug 1, 2024 · 2 comments
Open

ctx.Keys is overwritten by obj in methods like ctx.HTML #4022

Yiannis128 opened this issue Aug 1, 2024 · 2 comments

Comments

@Yiannis128
Copy link

Description

If you have middleware (using r.Use()), you can set keys through the method ctx.Set(k, v) which puts them into ctx.Keys. This sets the expectation of when using:

ctx.HTML(http.StatusOK, "account.html", gin.H{
	"Name":  user.Name,
	"Email": user.Email,
})

You expect that the map gin.H's entries will be applied on top of ctx.Keys, however, it overwrites them. Here is my Use method:

r.Use(func(ctx *gin.Context) {
// Set common context values
ctx.Set("Title", "My website")

if cookieId, err := ctx.Cookie(data.COOKIE_SESSION); err == nil {
	ctx.Set("SignedIn", len(cookieId) > 0)
}
})

Here are some of my routes:

Route /

r.GET("/", func(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "index.html", ctx.Keys)
})

Route about

r.GET("/about", func(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "about.html", gin.H{})
})

Inside each one I simply output the context using {{.}}, the context is as follows:

  • Route /: CTX: map[SignedIn:true Title:My website]
  • Route about: CTX: map[]

Multitemplate

I am using the github.com/gin-contrib/multitemplate plugin as I need to use multiple templates, and it's recommended in the docs to use that. I am assuming that it's working as intended.

Environment

  • go version: go version go1.21.12 linux/amd64
  • gin version (or commit ref): v1.10.0
  • gin multitemplate: v1.0.1
  • operating system: Fedora Linux 40
@RedCrazyGhost
Copy link
Contributor

RedCrazyGhost commented Aug 9, 2024

Although Conetxt.Keys and gin.H are both map[string]any, Keys are only used during the lifetime of the request and should not be exported as a result. gin.H is a specific output structure with multi-type support.

I suggest you use the code below to process the results and do something with the output

r.GET("/", func(ctx *gin.Context) {
   var mergeData gin.H
   for k, v := range ctx.Keys {
      mergedData[k] = v  // Process merge data
   }
   ctx.HTML(http.StatusOK, "index.html", mergeData)
})

@Yiannis128
Copy link
Author

Maybe it would be worth updating the documentation to state that, since my first impression was that it was a way of setting messages to be sent from the middleware to the endpoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants