how do to with fabric chaincode return “Additional property records is not allowed”

564 views Asked by At

I got a error when I write chaincode with "github.com/hyperledger/fabric-contract-api-go/contractapi"

type PaginatedQueryResult struct {
   Records             []asset `json:"records"`   
   FetchedRecordsCount int32  `json:"fetchedRecordsCount"`   
   Bookmark            string `json:"bookmark"`   
   Completed           bool   `json:"completed"`
}

when the Record is nil, report error: " asset_transfer_ledger chaincode Value did not match schema:\n 1. return.records: Invalid type. Expected: array, given: null", then I update PaginatedQueryResult struct like that:

type PaginatedQueryResult struct {
   Records             []asset `json:"records,omitempty" metadata:",optional" `  
   FetchedRecordsCount int32  `json:"fetchedRecordsCount"`   
   Bookmark            string `json:"bookmark"`   
   Completed           bool   `json:"completed"`
}

if Records is nil, this is ok, but when Record is not nil, get a error: "Additional property records is not allowed"

1

There are 1 answers

0
awjh On BEST ANSWER

thanks for posting this you have lead me to find an error in the code. The issue is that the code assumes the json tag to be the name only and doesn't expect ,omitempty so the metadata schema ends up having a property records,omitempty so when a value for records is supplied its not found in the schema as a valid property. Since the metadata tag overrides any json value the solution now until the core code is fixed is the add the name to your metadata tag as well as the JSON, your struct would therefore become:

type PaginatedQueryResult struct {
   Records             []asset `json:"records,omitempty" metadata:"records,optional" `  
   FetchedRecordsCount int32  `json:"fetchedRecordsCount"`   
   Bookmark            string `json:"bookmark"`   
   Completed           bool   `json:"completed"`
}

Note that records is in the JSON tag for marshalling purposes and the metadata tag.

I have opened a JIRA for this issue here: https://jira.hyperledger.org/browse/FABCAG-31