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

fix(binding): form binding error on fields of type any #4005

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

xiaotushaoxia
Copy link

when I want to bind query to any, I got a unknown type error. but it works in BindUri(/:name/:v for example) and BindJSON.
it should be a bug. I fix it.

package main

import (
	"fmt"
	"io"
	"net"
	"net/http"
	"os"
	"time"

	"github.com/gin-gonic/gin"
)

type Req struct {
	Name string `form:"name"`
	V    *any   `form:"v"`
}

func main() {
	// fix(binding): form binding error on fields of type `any`
	stop := newGin() // start server
	time.Sleep(time.Millisecond * 200)
	sendReq("/test?name=234")     // {"Name":"234","V":null}  ok
	sendReq("/test?name=234&v=1") // unknown type !!   want {"Name":"234","V":"1"}. **need fix**
	stop()
}

func newGin() (stopFunc func()) {
	gin.SetMode(gin.ReleaseMode)
	r := gin.New()
	r.GET("/test", func(c *gin.Context) {
		var req Req
		if err := c.ShouldBindQuery(&req); err != nil {
			http.Error(c.Writer, err.Error(), http.StatusBadRequest)
			return
		}
		c.JSON(200, req)
	})
	listen, err := net.Listen("tcp", ":8081")
	if err != nil {
		panic(err)
	}
	var ch = make(chan error, 1)
	go func() {
		ch <- r.RunListener(listen)
	}()
	return func() {
		listen.Close()
		<-ch
	}
}

func sendReq(url string) {
	fmt.Println("send request: ", "http://127.0.0.1:8081"+url)
	req, err := http.NewRequest("GET", "http://127.0.0.1:8081"+url, nil)
	if err != nil {
		fmt.Println(err)
		return
	}
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer resp.Body.Close()
	os.Stdout.Write([]byte("resp:"))
	io.Copy(os.Stdout, resp.Body)
	os.Stdout.Write([]byte("\n"))
}

Copy link

codecov bot commented Jun 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.06%. Comparing base (3dc1cd6) to head (7f4c8c3).
Report is 64 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4005      +/-   ##
==========================================
- Coverage   99.21%   99.06%   -0.16%     
==========================================
  Files          42       44       +2     
  Lines        3182     2772     -410     
==========================================
- Hits         3157     2746     -411     
+ Misses         17       15       -2     
- Partials        8       11       +3     
Flag Coverage Δ
?
-tags "sonic avx" 99.05% <100.00%> (?)
-tags go_json 99.05% <100.00%> (?)
-tags nomsgpack 99.04% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 99.06% <100.00%> (-0.16%) ⬇️
go-1.22 99.06% <100.00%> (?)
macos-latest 99.06% <100.00%> (-0.16%) ⬇️
ubuntu-latest 99.06% <100.00%> (-0.16%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@xiaotushaoxia
Copy link
Author

codecov test failed, because 3dc1cd6) is too early. some commits between this commit and 3dc1cd6) make code coverage decrease

@xiaotushaoxia
Copy link
Author

@appleboy hello, does this pr looks good? and is this pr can be merged in v1.11?

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

Successfully merging this pull request may close these issues.

1 participant