Skip to content

Commit 9077581

Browse files
committed
[FAB-15823] Fix getStringPayload npe
getStringPayload will now return null if the payload is null instead of throwing a null pointer exception Also fixes build break introduced with new Context argument on transaction methods Change-Id: I15dc4403a7a4782de6a22da7885d4b5baf40e4d7 Signed-off-by: James Taylor <[email protected]>
1 parent 7a60cb6 commit 9077581

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/Chaincode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
package org.hyperledger.fabric.shim;
88

9+
import static java.nio.charset.StandardCharsets.UTF_8;
10+
911
import java.util.HashMap;
1012
import java.util.Map;
1113

12-
import static java.nio.charset.StandardCharsets.UTF_8;
13-
1414
/**
1515
* Defines methods that all chaincodes must implement.
1616
*/
@@ -70,7 +70,7 @@ public byte[] getPayload() {
7070
}
7171

7272
public String getStringPayload() {
73-
return new String(payload, UTF_8);
73+
return (payload==null) ? null : new String(payload, UTF_8);
7474
}
7575

7676
/**

fabric-chaincode-shim/src/test/java/contract/SampleContract.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ public class SampleContract implements ContractInterface {
2525
static public int t1Invoked = 0;
2626
static public int i1Invoked = 0;
2727

28+
@Transaction
29+
public String t5(Context ctx) {
30+
doSomeWork();
31+
System.out.println("SampleContract::T5 Done");
32+
return null;
33+
}
34+
2835
@Transaction(name = "t4")
2936
public String tFour(Context ctx) {
3037

fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContractRouterTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.hamcrest.Matchers.equalTo;
1010
import static org.hamcrest.Matchers.is;
1111
import static org.hamcrest.Matchers.notNullValue;
12+
import static org.hamcrest.Matchers.nullValue;
1213
import static org.hamcrest.Matchers.startsWith;
1314
import static org.junit.Assert.assertThat;
1415

@@ -178,6 +179,32 @@ public void testInvokeTxnThatDoesNotExist() {
178179
assertThat(SampleContract.t1Invoked, is(0));
179180
}
180181

182+
@Test
183+
public void testInvokeTxnThatReturnsNullString() {
184+
ContractRouter r = new ContractRouter(new String[] { "-a", "127.0.0.1:7052", "-i", "testId" });
185+
r.findAllContracts();
186+
ChaincodeStub s = new ChaincodeStubNaiveImpl();
187+
188+
List<String> args = new ArrayList<>();
189+
args.add("samplecontract:t5");
190+
args.add("asdf");
191+
((ChaincodeStubNaiveImpl) s).setStringArgs(args);
192+
193+
SampleContract.beforeInvoked = 0;
194+
SampleContract.afterInvoked = 0;
195+
SampleContract.doWorkInvoked = 0;
196+
SampleContract.t1Invoked = 0;
197+
198+
Chaincode.Response response = r.invoke(s);
199+
assertThat(response, is(notNullValue()));
200+
assertThat(response.getStatus(), is(Chaincode.Response.Status.SUCCESS));
201+
assertThat(response.getStringPayload(), is(nullValue()));
202+
assertThat(SampleContract.beforeInvoked, is(1));
203+
assertThat(SampleContract.afterInvoked, is(1));
204+
assertThat(SampleContract.doWorkInvoked, is(1));
205+
assertThat(SampleContract.t1Invoked, is(0));
206+
}
207+
181208
@Test
182209
public void testInvokeTxnThatThrowsAnException() {
183210
ContractRouter r = new ContractRouter(new String[] { "-a", "127.0.0.1:7052", "-i", "testId" });

fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/ContractDefinitionTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.lang.reflect.Method;
1313
import java.security.Permission;
1414

15+
import org.hyperledger.fabric.contract.Context;
1516
import org.hyperledger.fabric.contract.ContractInterface;
1617
import org.hyperledger.fabric.contract.ContractRuntimeException;
1718
import org.hyperledger.fabric.contract.annotation.Contract;
@@ -89,7 +90,7 @@ public void duplicateTransaction() throws NoSuchMethodException, SecurityExcepti
8990
ContractDefinition cf = new ContractDefinitionImpl(SampleContract.class);
9091

9192
ContractInterface contract = new SampleContract();
92-
Method m = contract.getClass().getMethod("t2", new Class[] {});
93+
Method m = contract.getClass().getMethod("t2", new Class[] { Context.class });
9394

9495
thrown.expect(ContractRuntimeException.class);
9596
thrown.expectMessage("Duplicate transaction method t2");

0 commit comments

Comments
 (0)