gin icon indicating copy to clipboard operation
gin copied to clipboard

ShouldBindJSON return nil error

Open karterlizard opened this issue 1 year ago • 1 comments

  • With issues:
    • Use the search tool before opening a new issue.
    • Please provide source code and commit sha if you found a bug.
    • Review existing issues and provide feedback or react to them.

Description

How to reproduce

package main

import (
	"github.com/gin-gonic/gin"
)
type CreatePlaybackRecordReq struct {
	Data []DeviceRecordList `json:"data"`
}

func CreatePlaybackRecord(c *gin.Context) {
	var req *CreatePlaybackRecordReq
	if err := c.ShouldBindJSON(&req); err != nil {
		c.Status(http.StatusBadRequest)
		return
	}

	for _, deviceRecord := range req.Data {
         .....
        }
}

panic happend at line

for _, deviceRecord := range req.Data 

what kind body will cause panic like

goroutine 338245 [running]:
runtime/debug.Stack()
	/opt/go/src/runtime/debug/stack.go:24 +0x65
whgo/library/collect/api.(*middleware).handle.func1()
/data/jenkins/workspace/openplatform-stream-service/library/collect/api/interceptor.go:96 +0x37a
panic({0x2117780, 0x46e0f70})
	/opt/go/src/runtime/panic.go:1038 +0x215
whgo/openplatform/stream-service/router/api/v1.CreatePlaybackRecord(0xc001816960)
/data/jenkins/workspace/openplatform-stream-service/openplatform/stream-service/router/api/v1/playback_record.go:232 +0x77
github.com/gin-gonic/gin.(*Context).Next(...)
/opt/workspace/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161
whgo/library/collect/api.(*middleware).handle(0x0, 0xc001816960)
/data/jenkins/workspace/openplatform-stream-service/library/collect/api/interceptor.go:101 +0x1d0
github.com/gin-gonic/gin.(*Context).Next(...)
/opt/workspace/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161
go.elastic.co/apm/module/apmgin.(*middleware).handle(0xc0016eab40, 0xc001816960
/opt/workspace/pkg/mod/go.elastic.co/apm/module/[email protected]/middleware.go:132 +0x259
github.com/gin-gonic/gin.(*Context).Next(...)
/opt/workspace/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161
github.com/gin-gonic/gin.RecoveryWithWriter.func1(0xc001816960)
/opt/workspace/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:83 +0x7b
github.com/gin-gonic/gin.(*Context).Next(...)/opt/workspace/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161
github.com/gin-gonic/gin.LoggerWithConfig.func1(0xc001816960)
/opt/workspace/pkg/mod/github.com/gin-gonic/[email protected]/logger.go:241 +0xe7
github.com/gin-gonic/gin.(*Context).Next(...)
/opt/workspace/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161
github.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0xc000273a40, 0xc001816960)
/opt/workspace/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:409 +0x6c5
github.com/gin-gonic/gin.(*Engine).ServeHTTP(0xc000273a40, {0x29106a0, 0xc000d74540}, 0xc002d8f300)
/opt/workspace/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:367 +0x1b4
net/http.serverHandler.ServeHTTP({0x290b060}, {0x29106a0, 0xc000d74540}, 0xc002d8f300)
/opt/go/src/net/http/server.go:2879 +0x43b
net/http.(*conn).serve(0xc002db68c0, {0x29403b8, 0xc0015de810})
/opt/go/src/net/http/server.go:1930 +0xb08
created by net/http.(*Server).Serve/opt/go/src/net/http/server.go:3034 +0x4e8

Expectations

Environment

  • go version: 1.17
  • gin version (or commit ref): v1.6.3
  • operating system:

karterlizard avatar Jun 30 '22 16:06 karterlizard

I think you should var req CreatePlaybackRecordReq, not var req *CreatePlaybackRecordReq

CCpro10 avatar Jul 03 '22 04:07 CCpro10