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

fix: wrong way of Unmarshal #4397

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

wangshiben
Copy link

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
Copy link
Author

@kevwan please help review

…onx.Marshal but jsonx.Umarshal works wrong way
Copy link

codecov bot commented Jan 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.57%. Comparing base (8690859) to head (5dbcff4).
Report is 212 commits behind head on master.

Additional details and impacted files
Files with missing lines Coverage Δ
core/mapping/unmarshaler.go 96.30% <100.00%> (-0.19%) ⬇️

... and 7 files with indirect coverage changes

//and also u can find this https://stackoverflow.com/questions/34089750/marshal-byte-to-json-giving-a-strange-string
if fieldType.Elem().Kind() == reflect.Uint8 {
strVal := mapValue.(string)
decodedBytes, err := base64.StdEncoding.DecodeString(strVal)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can we know it's a base64 encoded string?

I recommend you to use string type and then decode it to []byte.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First in that switch case we can knwo that only type string and reflect type slice can setp into , and if the reflect element is uint8(byte) it have the probability that the original format is a byte slice ([]byte), and after encoding, it becomes a string.

So I use base64.StdEncoding.DecodeString try to figure out the []byte.This is better than I wirte a new func to parse string to []byte.

As mentioned above, we have established that valueKind is string. Therefore, within the if statement, I use type assertion to convert the type of value to string. Additionally, if any changes are made inside fillSliceFromString, I would not be able to determine whether the target type for conversion is []byte. Hence, performing the conversion from string to []byte here is the optimal approach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants