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

fix: wrong way of Unmarshal

Open wangshiben opened this issue 1 year ago • 1 comments

I faced a minor problem when I used it. for examlpe:

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)
}

and get returns

&types.Request{
		Bytes: []byte("11122344wsss"),
		Name:  "test",
	}

It's ok when I visit /get api ,but when I put /get response (the json data of request) into /test error happend:

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

and I notice that ,there use json.Marshal() to make the response but use slef custom mapping.UnmarshalJsonReader to Unmarshal json data

so I just fix it (core in mapping.processFieldNotFromString).

You can see test in TestUnmarshalJsonReaderMultiArray

wangshiben avatar Sep 27 '24 15:09 wangshiben

@kevwan please help review

wangshiben avatar Sep 28 '24 02:09 wangshiben

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 94.56%. Comparing base (8690859) to head (c7683bf). Report is 226 commits behind head on master.

Additional details and impacted files
Files with missing lines Coverage Δ
core/mapping/unmarshaler.go 96.28% <100.00%> (-0.20%) :arrow_down:

... and 7 files with indirect coverage changes

codecov[bot] avatar Jan 12 '25 17:01 codecov[bot]