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

JSON encode in golang fabric chaincode behavior #54

Open
msalimbene opened this issue Oct 20, 2021 · 0 comments
Open

JSON encode in golang fabric chaincode behavior #54

msalimbene opened this issue Oct 20, 2021 · 0 comments

Comments

@msalimbene
Copy link

I've seen several articles with similar errors, but none seems to work for me. I've seen the marbles samples, as well as many others, and still can't pinpoint the error.

I'm in fabric 2.x. The chaincode below works just fine when saving data, but fails when reading with the following message:

ERROR] Error submitting transaction: No valid responses from any peers. Errors:
    peer=org1peer-api.127-0-0-1.nip.io:8080, status=500, message=Error handling success response. Value did not match schema:
1. return: Additional property field1 is not allowed
2. return: Additional property field2 is not allowed
3. return: field1,omitempty is required
4. return: field2,omitempty is required
type Asset struct {
	Field1 string `json:"field1,omitempty"`
	Field2 string `json:"field2,omitempty"`
}

func (c *AssetContract) CreateAsset(ctx contractapi.TransactionContextInterface, assetID string, values string) (bool, error) {
	
	// convert json input to byte array
	txData := []byte(values)

	// convert byte array to HlpAsset struct
	asset := new(asset)
	err = json.Unmarshal(txData, &asset)

	// convert struct back to bytes
	txBytes, err := json.Marshal(asset)

	return true, ctx.GetStub().PutState(assetID, txBytes)
}

func (c *AssetContract) ReadAsset(ctx contractapi.TransactionContextInterface, assetID string) (*Asset, error) {

	txBytes, _ := ctx.GetStub().GetState(assetID)

	// convert byte array to HlpAsset struct
	asset := new(Asset)
	err = json.Unmarshal(txBytes, &asset)

	return asset, nil
}

testing with the following input data:

assetID: "3",
values: "{\"field1\":\"123\",\"field2\":\"a05\"}"

I found this question where this bug is mentioned:

Supplying additional valid data in JSON tag of struct property causes schema to fail.

The bug has been closed for around a year, however, I still experience that behavior. Overriding the JSON property using medatada didn't work either.

Using the struct like this worked perfectly:

type Asset struct {
	Field1 string `json:"field1"`
	Field2 string `json:"field2"`
}
@msalimbene msalimbene changed the title son encode in golang fabric chaincode behavior JSON encode in golang fabric chaincode behavior Oct 20, 2021
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

No branches or pull requests

1 participant