hyperledger-fabric - 如何处理结构链码返回“不允许附加属性记录”
问题描述
使用“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”
解决方案
感谢您发布此内容,您让我在代码中发现了一个错误。问题是代码假定 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
推荐阅读
- c# - 如何生成唯一的Hashcode?
- firebase - 尝试在 Xamarin 表单 iOS 应用程序中添加 Crashlytics
- c# - 无法将参数值从 Binary 转换为 Byte[]。-- 带有 SQL 时间戳参数的 SqlDataSource
- java - Undertow 处理程序使所有堆栈非阻塞
- python - 计算表中的行数
- reactjs - 如何在 Jest 和 Enzyme 中执行无状态组件内的函数以进行测试
- python - From x_1.0 import y - 如何处理带有版本的导入?
- angular - Angular - 等待 ngIf 准备好以避免其中 elementRef 出现未定义的错误
- c# - 重新部署 tizen 应用程序后记住权限
- string - 将分隔字符串转换为行