go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

Wrong in UnmarshalJsonReader

Open wangshiben opened this issue 1 year ago • 3 comments

Describe the bug I faced a minor problem when I used it. API's JSON response can't used in API's JSON request.It's improtent for some P2P communicate Server.

To Reproduce Steps to reproduce the behavior, if applicable:

  1. The api code is

    type request {
     Bytes []byte `json:"bytes"`
     Name  string `json:"name"`
    

}

type response {}

service template { @handler postDataTest post /test (request) returns (response) // this API just simply fmt.Printf("receive :%s",request)

@handler getJsonData
get /get returns (request)

}


2. `/get` API returns Struct like this,or some other []byte like Hash256 result, pubKey....

```go
types.Request{
		Bytes: []byte("11122344wsss"),
		Name:  "test",
	}
  1. I visit /get and pass get result into /test

  2. some error happend fullName: bytes, error: string: MTExMjIzNDR3c3Nz, error: `invalid character 'M' looking for beginning of value```

Expected behavior

Logic code in /test print "["11122344wsss","test"]" in console

Screenshots

Environments (please complete the following information):

  • OS: Windows10
  • go-zero version 1.2.1
  • goctl version 1.7.2

More description I fixed it, and I alse make a pr. You can review it in the test file

wangshiben avatar Sep 28 '24 07:09 wangshiben

Would you please give a demo code to reproduce this issue?

kevwan avatar Oct 02 '24 13:10 kevwan

  1. make a main.api ,and code as follow
syntax = "v1"

info (
	title:   "type title here"
	desc:    "type desc here"
	author:  "type author here"
	email:   "type email here"
	version: "type version here"
)

type request {
	Bytes []byte `json:"bytes"`
	Name  string `json:"name"`
}

type response {}
service template {
	@handler postDataTest
	post /test (request) returns (response)

	@handler getJsonData
	get /get returns (request)
}
  1. run the bash like to make a go-zero file
goctl api go --api  main.api --dir ./ --style=goZero
  1. and code in the internal/logic/getJsonDataLogic.go ,add this
func (l *GetJsonDataLogic) GetJsonData() (resp *types.Request, err error) {
	return &types.Request{
		Bytes: []byte("11122344wsss"),
		Name:  "test",
	}, nil
}

  1. then visit /get,I got image
 {
        "bytes": "MTExMjIzNDR3c3Nz",
        "name": "test"
   }
  1. pass reslut to /test,like this:

image

then ,error happend

and you can run in https://github.com/zeromicro/go-zero/pull/4397/files (the test func)

by the way I also probably fix it ,and I also make a pr ( https://github.com/zeromicro/go-zero/pull/4397)

wangshiben avatar Oct 02 '24 15:10 wangshiben

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Oct 04 '25 02:10 github-actions[bot]