hyperledger / hyperledger/fabric-chaincode-java

Fields missing even with @Property. Unclear documentation?

オープン
#232 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Java
スター
323
フォーク
210
平均マージ
8時間 1分
マージ済み PR(30日)
8

説明

### 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.

```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](https://hyperledger-fabric.readthedocs.io/en/release-2.3/test_network.html).

### 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

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

### Discussion

I looked into fabric-chaincode-java. [The constructor of DataTypeDefinitionImpl](https://github.com/hyperledger/fabric-chaincode-java/blob/4ea0f715a88799a893eda5883ed5eb7254d72e48/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/DataTypeDefinitionImpl.java#L65) puts "guid" to `this.properties`. Then we execute [JSONTransactionSerializer.toBuffer()](https://github.com/hyperledger/fabric-chaincode-java/blob/4ea0f715a88799a893eda5883ed5eb7254d72e48/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/execution/JSONTransactionSerializer.java#L95). 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

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

public Object getGuid() {
return guid;
}
}
```
I get
```console
$ 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

```java
/**
* 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.
...
```

コントリビューションガイド

コントリビューションガイドを開く

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。