1
votes

I have the struct below

type foos struct { Foo string `json:"foo" binding:"required"`}

and I have the following endpoint

  func sendFoo(c *gin.Context) {
      var json *foos

      if err := c.BindJSON(&json); err != nil {
          c.AbortWithStatus(400)
          return
      }

      // Do something about json
   }

when I post this JSON

{"bar":"bar bar"}

the err is always nil. I write binding required and it doesn't work. But when I change the endpoint like below,

func sendFoo(c *gin.Context) {
    var json foos //remove pointer

    if err := c.BindJSON(&json); err != nil {
          c.AbortWithStatus(400)
          return
    }

    // Do something about json
}

binding works and the err is not nil. Why?

1
What happens if you do c.BindJSON(json) in the first case?zerkms
you can't do it, it will cause unmarshall errorWisnu Wardoyo
Could you please elaborate? How passing a pointer would "cause unmarshall error", if it does not do so in the second case? What if you initialise the pointer var json *foos = &foos{} or json := &foos{} and simply pass json?zerkms
the all endpoint I mentioned is not causing any error. When you initialize var json *foos and you just use c.BindJSON(json) it's return unmarshal error because json is the value of *foos which is nil, and &json will pass the pointer address which will initialized in json.decode.Wisnu Wardoyo
When you do &json for a variable of *foos type you get a pointer to a pointer. It correctly tells you about it being null since you did not initialise it, like json := &foos{}zerkms

1 Answers

6
votes

It is documented in binding.go, lines 25-32 :

type StructValidator interface {
    // ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.
    // If the received type is not a struct, any validation should be skipped and nil must be returned.
    // If the received type is a struct or pointer to a struct, the validation should be performed.
    // If the struct is not valid or the validation itself fails, a descriptive error should be returned.
    // Otherwise nil must be returned.
    ValidateStruct(interface{}) error
}

In your case, ValidateStruct receives a pointer to a pointer to a struct, and no checking takes place, as documented.