首页 > 解决方案 > 如何处理结构链码返回“不允许附加属性记录”

问题描述

使用“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"`
}

当 Record 为 nil 时,报告错误:“asset_transfer_ledger 链码值不匹配架构:\n 1.return.records:无效类型。预期:数组,给定:null”,然后我像这样更新 PaginatedQueryResult 结构:

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

如果 Records 为 nil,这是可以的,但是当 Record 不是 nil 时,会报错:“Additional property records is not allowed”

标签: hyperledger-fabrichyperledger-chaincode

解决方案


感谢您发布此内容,您让我在代码中发现了一个错误。问题是代码假定 json 标记仅为名称并且不期望,omitempty因此元数据模式最终具有一个属性records,omitempty,因此当提供记录的值时,它在模式中找不到作为有效属性。由于元数据标记会覆盖任何 json 值,因此在修复核心代码之前,现在的解决方案是将名称添加到元数据标记以及 JSON,因此您的结构将变为:

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

请注意,记录位于用于编组目的的 JSON 标记和元数据标记中。

我在这里为这个问题打开了一个 JIRA:https ://jira.hyperledger.org/browse/FABCAG-31


推荐阅读