Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Fields missing even with @Property. Unclear documentation?

未关闭
#232 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
45/100
Issue 类型
文档
描述清晰度
基本清楚
活跃度
停滞
技术栈
java
领域
documentation

调研方向

首先阅读 @Property 注解文档,以及报告所确定的相关路径 DataTypeDefinitionImpl 和 JSONTransactionSerializer。明确说明带注解的字段需要 getter 才会出现在返回的 JSON 中,并更新注解文档,使 Sale 示例的预期序列化行为清晰明确。

由索引模型根据 Issue 内容生成。

描述

Steps to reproduce

I'm looking at version 2.3 but I think the behavior holds for the lastest version.

The following code is based on https://github.com/hyperledger/fabric-samples/tree/main/asset-transfer-basic/chaincode-java.

@DataType
public class Sale {
	@Property
	private String guid = "1";

	public Object getPK() {
		return guid;
	}
}


@Contract
public final class AssetTransfer implements ContractInterface {

    @Transaction(intent = Transaction.TYPE.EVALUATE)
    public Sale getSale(final Context ctx) {
        return new Sale();
    }
}

Run the code on the test-network.

What I expect

When I call

peer chaincode query -C mychannel -n basic -c '{"Args":["getSale"]}'

I expect to see a non-empty output. The output might be {guid:"1"} or {PK:"1"}, I don't know.

Since I already used @Property to annotate a field, I do not expect to see an empty json, i.e., {}.

What actually happened
$ peer chaincode query -C mychannel -n basic -c '{"Args":["getSale"]}'
{}
Discussion

I looked into fabric-chaincode-java. The constructor of DataTypeDefinitionImpl puts "guid" to this.properties. Then we execute JSONTransactionSerializer.toBuffer(). We will run final JSONObject obj = new JSONObject(new JSONObject(value), propNames); where propNames is ["guid"]. JSONObject outputs nothing in this situation.

It looks like to me that in order for field X to show up in the returned JSON, we have to 1. add @Property to field X; 2. make sure field X has a getter named getX.

To prove if I change to

@DataType
public class Sale {
	@Property
	private String guid = "1";

	public Object getGuid() {
		return guid;
	}
}

I get

$ peer chaincode query -C mychannel -n basic -c '{"Args":["getSale"]}'
{"guid":"1"}
Suggestion

I feel this issue could be a documentation oversight as @Property didn't mention getters. If maintainers like, I can add something like

/**
 * Field and parameter level annotation defining a property of the class.
 * 
 * When this annotation applied to a field, make sure the field has a 
 * getter.
 ...
主要语言
Java
星标
322
派生
210
平均合并
15 小时 16 分钟
30 天内合并 PR
29

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

hyperledger/fabric-chaincode-java 的其他 Issue

查看 hyperledger/fabric-chaincode-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。