I am using Hyperledger Fabric 2.2.0 and fabric-network 2.1 (not that important).
My chaincode is written in Go. So I have some structs which have ,omitempty
in JSON tag. Here is my struct:
type LeaseDetails struct {
EndOfTerm string `json:"endOfTerm"`
Info string `json:"info,omitempty"`
Option string `json:"option,omitempty"`
}
But I am getting the following error as a return value from my chaincode:
peer=peer0.org1.example.com:7051, status=500, message=Error handling success response. Value did not match schema:
1. return.0.leaseDetails: info,omitempty is required
2. return.0.leaseDetails: option,omitempty is required
If I remove ,omitempty
from my struct, and provide default value everything works fine.
In the docs for fabric-contract-api-go
it is mentioned that there is some kind of serializer built upon json marshal/unmarshal, but to me it doesn't seem to detect the ,omitempty
keyword.
Was this intentional? Or am I missing something here?
Thanks in advance
I got the answer on Hyperledger Chat from user @awjh.
This is as intended, the json is compared against the metadata schema. By default all fields are required, using omitempty will mean that the JSON process will remove that field when it has no value. This means a required field will be missing. To fix this add a metadata tag to mark the field as optional
metadata:",optional"
So in my case, the solution is:
type LeaseDetails struct {
EndOfTerm string `json:"endOfTerm"`
Info string `json:"info,omitempty" metadata:",optional"`
Option string `json:"option,omitempty" metadata:",optional"`
}