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

[Question] Can not bind parameter of struct using ReadBody #2371

Open
soarchorale opened this issue Feb 29, 2024 · 0 comments
Open

[Question] Can not bind parameter of struct using ReadBody #2371

soarchorale opened this issue Feb 29, 2024 · 0 comments
Assignees

Comments

@soarchorale
Copy link

soarchorale commented Feb 29, 2024

Describe the bug
I use iris as a simple web server and found a problem in parameter binding. When I send two int64 parameters with Get method, the second parameter will not bind to my struct. This problem may not the regular of bug description but occurred when I debug and step into the variable.

To Reproduce
Steps to reproduce the behavior:

// 1. My struct is defined as:
type GTClip struct {
	Id      int64  `gorm:"column:id" json:"id"`
	Clip    string `gorm:"column:clip" json:"clip"`
	Scene   string `gorm:"column:scene" json:"scene"`
	Path    string `gorm:"column:path" json:"path"`
	SetId   int64  `gorm:"column:set_id" json:"set_id"`
	Deleted int    `gorm:"column:deleted" json:"-"`
}
// 2. the route and function implemented as:
	gt := app.Party("/gt")
	{
		gt.Use(iris.Compression)
		clip := gt.Party("/clip")
		{
			// ... other unrelated method
			clip.Get("/list", func (ctx iris.Context) {
	                          var (
		                          gtClipReq  = &model.GTClip{}
		                          err        error
	                          )
	                          if err := ctx.ReadBody(gtClip); err != nil { // step point 1
		                          ctx.StopWithError(iris.StatusBadRequest, err)
		                          return
	                          }
	                          if gtClip.SetId, err = ctx.URLParamInt64("set_id"); err != nil {  // step point 2
		                          ctx.StopWithError(iris.StatusBadRequest, err)
		                          return
	                          }
	                          ctx.JSON(iris.Map{
		                          "code": 0,
		                          "err":  nil,
	                          })
                          })
		}
	}
  1. launch server and send request: http://127.0.0.1:8080/gt/clip/list?id=11&set_id=12
    Expected behavior
    The struct GTClip menber of Id and SetId should be bind as 11 and 12 respectively, with above requests. But until called function "ctx.URLParamInt64("set_id")", the member SetId is not binded the expected value 12. As comment at "step point 1".
    After calling the ctx.URLParamInt64("set_id"), SetId is set to 12. As comment at "step point 2".
    Another experiment is that if I add a parameter scene, member of Scene will be correctly binded! eg: http://127.0.0.1:8080/gt/clip/list?id=11&set_id=11&scene=turn
    Screenshots
    image
    image

Desktop (please complete the following information):

  • OS: macOs 13.2 (22D49)

iris.Version

  • v12.2.8

Please make sure the bug is reproducible over the main branch:
YES, the version I updated is v12.2.11-0.20240122133509-ddda4f699854, still has same problem

Additional context
I think there might be some misunderstand when I use ctx.ReadBody method. In fact I can't remember such many parameter binding methods and always use ctx.ReadBody instead. Am I right?

@soarchorale soarchorale changed the title [BUG] Can not bind parameter of struct using ReadBody [Problem] Can not bind parameter of struct using ReadBody Feb 29, 2024
@soarchorale soarchorale changed the title [Problem] Can not bind parameter of struct using ReadBody [Question] Can not bind parameter of struct using ReadBody Feb 29, 2024
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