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

encoding/gjson:struct defines the gjson format when receiving parameters. If the http body passes json in a one-dimensional array format, the gjson parsing error occurs. #3768

Closed
function-gy opened this issue Sep 11, 2024 · 1 comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@function-gy
Copy link

Go version

go1.22.1 darwin/arm64

GoFrame version

v2.7.2

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

gf gen dao 生成的数据库结构

type PmsRoomType struct {
	Uid                    string      `json:"uid"                    orm:"uid"                      description:"第三方系统的ID"`
	Id                     int         `json:"id"                     orm:"id"                       description:"主键ID"`
	Puid                   string      `json:"puid"                   orm:"puid"                     description:"物业ID"`
	Cover                  string      `json:"cover"                  orm:"cover"                    description:"封面"`
	CoverList              *gjson.Json `json:"coverList"             orm:"cover_list"               description:"照片墙"`
	Name                   string      `json:"name"                   orm:"name"                     description:"房型名称"`
	BasePrice              float64     `json:"basePrice"              orm:"base_price"               description:"最低价格"`
	CheckinAt              string      `json:"checkinAt"              orm:"checkin_at"               description:"入住时间"`
	CheckoutAt             string      `json:"checkoutAt"             orm:"checkout_at"              description:"退房时间"`
	BookingStyle           string      `json:"bookingStyle"           orm:"booking_style"            description:"预订方式"`
	RoomStyle              string      `json:"roomStyle"              orm:"room_style"               description:"房间风格"`
	Occupancy              int         `json:"occupancy"              orm:"occupancy"                description:"占用"`
	Size                   string      `json:"size"                   orm:"size"                     description:"房间大小"`
	Bedrooms               string      `json:"bedrooms"               orm:"bedrooms"                 description:"卧室"`
	Bathrooms              string      `json:"bathrooms"              orm:"bathrooms"                description:"浴室"`
	CleaningFee            float64     `json:"cleaningFee"            orm:"cleaning_fee"             description:"清理费"`
	RatePlanId             string      `json:"ratePlanId"             orm:"rate_plan_id"             description:"费率计划模板ID"`
	AdditionalGuestAmounts float64     `json:"additionalGuestAmounts" orm:"additional_guest_amounts" description:"额外客人金额"`
	OccupantsForBaseRate   int         `json:"occupantsForBaseRate"   orm:"occupants_for_base_rate"  description:"无需增加额外客人金额人数"`
	CreateAt               *gtime.Time `json:"createAt"               orm:"create_at"                description:""`
	UpdateAt               *gtime.Time `json:"updateAt"               orm:"update_at"                description:""`
}

http 传递此参数进行接收数据进行编辑数据的时候 存在解析CoverList字段异常

What did you see happen?

curl 'http://localhost:8001/admin/pmsRoomType/edit' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'Accept-Language: zh-CN,zh;q=0.9,ru;q=0.8' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/json;charset=UTF-8' \
  -H 'Origin: http://localhost:8001' \
  -H 'Referer: http://localhost:8001/' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Site: same-origin' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36' \
  -H 'sec-ch-ua: "Chromium";v="128", "Not;A=Brand";v="24", "Google Chrome";v="128"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "macOS"' \
  -H 'x-language: zh' \
  --data-raw '{"uid":"31dc262f-571a-4b6d-9e67-655ce281cbb6","id":49,"puid":"1644d236-b1f5-43d2-8df6-5e01b797b8a8","cover":"https://apt11-1251002327.cos.ap-tokyo.myqcloud.com/storage/public/2024-06-22/d26hltsywte4gvsjp4.png","name":"Trip Room","basePrice":0,"checkinAt":"16:01","checkoutAt":"10:00","bookingStyle":"","roomStyle":"","occupancy":4,"size":"25","bedrooms":1,"bathrooms":"","cleaningFee":0,"ratePlanId":"","additionalGuestAmounts":2000,"occupantsForBaseRate":2,"createAt":"2024-08-28 19:01:32","updateAt":"2024-09-10 11:02:02","coverList":["http://127.0.0.1:8000/attachment/2024-09-11/d43c8upm8yksjgp1yp.jpg","http://127.0.0.1:8000/attachment/2024-09-11/d43c8uy62sgnuxfdse.jpg","http://127.0.0.1:8000/attachment/2024-09-11/d43c8uy3xxech5bi53.jpg"],"bedType":[{"bedTypeName":"","bedWidth":null,"bedNum":null}]}'

如上请求参数得到的 CoverList 字段变成了

["http://127.0.0.1:8000/attachment/2024-09-11/d43c8upm8yksjgp1yp.jpg":"http://127.0.0.1:8000/attachment/2024-09-11/d43c8uy62sgnuxfdse.jpg"]

What did you expect to see?

能否修改 *gjson.Json 解析一维数组json的能力

@function-gy function-gy added the bug It is confirmed a bug, but don't worry, we'll handle it. label Sep 11, 2024
@Issues-translate-bot Issues-translate-bot changed the title encoding/gjson:struct 定义 gjson 格式接收参数的时候 如果http body 传递的是一个一维数组格式的json情况下 gjson解析有误 encoding/gjson:struct defines the gjson format when receiving parameters. If the http body passes json in a one-dimensional array format, the gjson parsing error occurs. Sep 11, 2024
@niluan304
Copy link
Contributor

能否给出最小的可复现代码?

我写了一个单元测试,结果是正常的:

Unit Test
type PmsRoomType struct {
	Uid                    string      `json:"uid"                    orm:"uid"                      description:"第三方系统的ID"`
	Id                     int         `json:"id"                     orm:"id"                       description:"主键ID"`
	Puid                   string      `json:"puid"                   orm:"puid"                     description:"物业ID"`
	Cover                  string      `json:"cover"                  orm:"cover"                    description:"封面"`
	CoverList              *gjson.Json `json:"coverList"              orm:"cover_list"               description:"照片墙"`
	Name                   string      `json:"name"                   orm:"name"                     description:"房型名称"`
	BasePrice              float64     `json:"basePrice"              orm:"base_price"               description:"最低价格"`
	CheckinAt              string      `json:"checkinAt"              orm:"checkin_at"               description:"入住时间"`
	CheckoutAt             string      `json:"checkoutAt"             orm:"checkout_at"              description:"退房时间"`
	BookingStyle           string      `json:"bookingStyle"           orm:"booking_style"            description:"预订方式"`
	RoomStyle              string      `json:"roomStyle"              orm:"room_style"               description:"房间风格"`
	Occupancy              int         `json:"occupancy"              orm:"occupancy"                description:"占用"`
	Size                   string      `json:"size"                   orm:"size"                     description:"房间大小"`
	Bedrooms               string      `json:"bedrooms"               orm:"bedrooms"                 description:"卧室"`
	Bathrooms              string      `json:"bathrooms"              orm:"bathrooms"                description:"浴室"`
	CleaningFee            float64     `json:"cleaningFee"            orm:"cleaning_fee"             description:"清理费"`
	RatePlanId             string      `json:"ratePlanId"             orm:"rate_plan_id"             description:"费率计划模板ID"`
	AdditionalGuestAmounts float64     `json:"additionalGuestAmounts" orm:"additional_guest_amounts" description:"额外客人金额"`
	OccupantsForBaseRate   int         `json:"occupantsForBaseRate"   orm:"occupants_for_base_rate"  description:"无需增加额外客人金额人数"`
	CreateAt               *gtime.Time `json:"createAt"               orm:"create_at"                description:""`
	UpdateAt               *gtime.Time `json:"updateAt"               orm:"update_at"                description:""`
}

func Test_gjson_Scan(t *testing.T) {
	const raw = `{"uid":"31dc262f-571a-4b6d-9e67-655ce281cbb6","id":49,"puid":"1644d236-b1f5-43d2-8df6-5e01b797b8a8","cover":"https://apt11-1251002327.cos.ap-tokyo.myqcloud.com/storage/public/2024-06-22/d26hltsywte4gvsjp4.png","name":"Trip Room","basePrice":0,"checkinAt":"16:01","checkoutAt":"10:00","bookingStyle":"","roomStyle":"","occupancy":4,"size":"25","bedrooms":1,"bathrooms":"","cleaningFee":0,"ratePlanId":"","additionalGuestAmounts":2000,"occupantsForBaseRate":2,"createAt":"2024-08-28 19:01:32","updateAt":"2024-09-10 11:02:02","coverList":["http://127.0.0.1:8000/attachment/2024-09-11/d43c8upm8yksjgp1yp.jpg","http://127.0.0.1:8000/attachment/2024-09-11/d43c8uy62sgnuxfdse.jpg","http://127.0.0.1:8000/attachment/2024-09-11/d43c8uy3xxech5bi53.jpg"],"bedType":[{"bedTypeName":"","bedWidth":null,"bedNum":null}]}`

	var req *PmsRoomType

	err := gjson.New(raw).Scan(&req)
	if err != nil {
		t.Error(err)
	}

	t.Logf("%s", req.CoverList)
}


//     issue_3768_test.go:44: ["http://127.0.0.1:8000/attachment/2024-09-11/d43c8upm8yksjgp1yp.jpg","http://127.0.0.1:8000/attachment/2024-09-11/d43c8uy62sgnuxfdse.jpg","http://127.0.0.1:8000/attachment/2024-09-11/d43c8uy3xxech5bi53.jpg"]
// --- PASS: Test_gjson_Scan (0.00s)
// PASS

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

2 participants