Skip to content

Can't bind list of struct pointer in query #3659

@claneo

Description

@claneo

Description

Sorry I'm not familiar with golang, so the description below maybe is not accurate.

Can't bind list of struct pointer in query

It seem like the pointer support was lost in this commit

0d50ce8?diff=split#diff-b0f2c06474cd98fa718e657b7037a516ada93c3de8f51fd795bfdf0ab3e2ade5L133-L138

I try to add these lines into setWithProperType,then the problem is resolved

	case reflect.Ptr:
		if !value.Elem().IsValid() {
			value.Set(reflect.New(value.Type().Elem()))
		}
		return setWithProperType(val, value.Elem(), field)

How to reproduce

package main

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

type Struct struct {
	Id int64 `json:"id"`
}

type Req struct {
	Items []*Struct `json:"items" form:"items"`
}

func main() {
	r := gin.Default()

	r.GET("/api", func(c *gin.Context) {
		req := new(Req)
		err := c.Bind(req)
		if err != nil {
			c.JSON(500, err.Error())
			return
		}
		c.JSON(200, req)
	})

	_ = r.Run("127.0.0.1:8080")
}

Expectations

$ curl -i 'http://localhost:8080/api?items=\{"id":1\}&items=\{"id":2\}'
{"items":[{"id":1},{"id":2}]}

Actual result

$ curl -i 'http://localhost:8080/api?items=\{"id":1\}&items=\{"id":2\}'
"unknown type"

Environment

  • go version: v1.20.4
  • gin version (or commit ref): v1.9.1
  • operating system: macos 13.4.1 arm64

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions