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

util/gvalid: json array validation is invalid #3581

Open
shuqingzai opened this issue May 15, 2024 · 2 comments
Open

util/gvalid: json array validation is invalid #3581

shuqingzai opened this issue May 15, 2024 · 2 comments
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@shuqingzai
Copy link

Go version

go version go1.21.9 darwin/amd64

GoFrame version

2.7.1

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

server

package main

import (
	"context"
	"encoding/json"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
)

type HelloReq struct {
	g.Meta `path:"/hello" method:"POST"`

	Name    string          `json:"name" v:"required" dc:"Your name"`
	RawJSON json.RawMessage `json:"rawJson" v:"required|json" dc:"Raw JSON content"`
}
type HelloRes struct {
	Name    string          `json:"name" v:"required" dc:"Your name"`
	RawJSON json.RawMessage `json:"rawJson" v:"required|json" dc:"Raw JSON content"`
}

type Hello struct{}

func (Hello) Say(ctx context.Context, req *HelloReq) (res *HelloRes, err error) {
	g.Log().Debugf(ctx, `receive say: %+v`, req)
	res = &HelloRes{
		Name:    req.Name,
		RawJSON: req.RawJSON,
	}
	return
}

func main() {
	s := g.Server()
	s.Use(ghttp.MiddlewareHandlerResponse)
	s.Group("/", func(group *ghttp.RouterGroup) {
		group.Bind(
			new(Hello),
		)
	})
	s.Run()
}

config

server:
  address:     ":8199"
  openapiPath: "/api.json"
  swaggerPath: "/swagger"

启动服务测试请求

  1. object

JSON Object 可以通过验证

curl --location --request POST 'http://127.0.0.1:8199/hello' \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "name": "jack",
    "rawJson": {
        "name": "j2"
      }
}'
  1. array

JSON Array 无法通过验证

curl --location --request POST 'http://127.0.0.1:8199/hello' \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "name": "jack",
    "rawJson": [
      {
        "name": "j2"
      }
    ]
}'

See:

if json.Valid(in.Value.Bytes()) {

What did you see happen?

JSON Array 验证失效

What did you expect to see?

主要原因

  1. HTTP 服务的参数全部转为 []map[string]any
  2. 对 slice 进行特殊处理,导致源JSON数据丢失
    See:
    case reflect.Array, reflect.Slice:
@shuqingzai shuqingzai added the bug It is confirmed a bug, but don't worry, we'll handle it. label May 15, 2024
@Issues-translate-bot Issues-translate-bot changed the title util/gvalid: json 数组验证无效 util/gvalid: json array validation is invalid May 15, 2024
@wln32
Copy link
Member

wln32 commented May 16, 2024

@shuqingzai 建议直接用string接收,使用json.RawMessage会做两次json转换

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@shuqingzai It is recommended to use string to receive directly and use json.RawMessage to do two json conversions

@gogf gogf deleted a comment from Issues-translate-bot May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.
Projects
None yet
Development

No branches or pull requests

3 participants