Skip to content

Commit 0a4a445

Browse files
Fix default Transaction#info [ECR-3193]: (#904)
Empty string caused panic in the native code, and, subsequently, a lost request. Made #info return an empty object, though, one might argue it will be more difficult to understand why `transactions` returns nothing. That, however, is to be addressed *after* Dynamic Services.
1 parent fae6873 commit 0a4a445

File tree

4 files changed

+70
-6
lines changed

4 files changed

+70
-6
lines changed

exonum-java-binding/CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2828
format instead of the whole message in binary form.
2929
- RocksDB library is no longer required to be installed on Mac or Linux to run
3030
the Exonum Java application. (#902)
31+
32+
### Fixed
33+
- The default [`Transaction#info`][tx-info-07] implementation causing an error on `transaction`
34+
request. It is modified to return an empty object (no info) by default. (#904)
35+
36+
[tx-info-07]: https://exonum.com/doc/api/java-binding-core/0.7.0/com/exonum/binding/transaction/Transaction.html#info()
3137

32-
## [0.6.0]- 2019-05-08
38+
## [0.6.0] - 2019-05-08
3339

3440
**If you are upgrading an existing Java service, consult
3541
the [migration guide](https://github.com/exonum/exonum-java-binding/blob/ejb/v0.6.0/exonum-java-binding/doc/Migration_guide_0.6.md).**

exonum-java-binding/common/src/main/java/com/exonum/binding/common/serialization/json/JsonSerializer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
/**
2929
* Provides {@link Gson} serializer for converting Java objects to Json and vice versa.
3030
* It is configured to serialize Exonum objects in a format, compatible with the core framework
31-
* and light clients (e.g., {@link HashCode} as a hex string).
31+
* and light clients (e.g., {@link HashCode} as a hex string). If needed, a new serializer
32+
* with adapters for service-specific types can be {@linkplain #builder() created}, with
33+
* Exonum types support already included.
3234
*/
3335
public final class JsonSerializer {
3436

@@ -50,8 +52,8 @@ public static GsonBuilder builder() {
5052
}
5153

5254
/**
53-
* Returns preconfigured {@link Gson} instance. Helpful in cases when no additional configuration
54-
* of the Json serializer is required.
55+
* Returns preconfigured {@link Gson} instance. Helpful in cases when no additional
56+
* {@linkplain #builder() configuration} of the Json serializer is required.
5557
*/
5658
public static Gson json() {
5759
return INSTANCE;

exonum-java-binding/core/src/main/java/com/exonum/binding/transaction/Transaction.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,24 @@ public interface Transaction {
4141
void execute(TransactionContext context) throws TransactionExecutionException;
4242

4343
/**
44-
* Returns some information about this transaction in JSON format.
44+
* Returns the information about this transaction in JSON format.
45+
* For example, it is included in the blockchain explorer response to
46+
* a <a href="https://exonum.com/doc/version/0.11/advanced/node-management/#transaction">
47+
* transaction</a> request.
48+
*
49+
* <p>By default, no information is provided. If needed, it can be easily implemented
50+
* with {@linkplain com.exonum.binding.common.serialization.json.JsonSerializer Gson}:
51+
*
52+
* <pre><code>
53+
* &#64;Override
54+
* public String info() {
55+
* return JsonSerializer.json().toJson(this);
56+
* }
57+
* </code></pre>
4558
*/
4659
default String info() {
47-
return "";
60+
// Empty object by default
61+
return "{}";
4862
}
4963

5064
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2019 The Exonum Team
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.exonum.binding.transaction;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import com.google.gson.Gson;
22+
import com.google.gson.reflect.TypeToken;
23+
import java.util.Map;
24+
import org.junit.jupiter.api.Test;
25+
26+
class TransactionTest {
27+
28+
@Test
29+
void infoIsEmptyByDefault() {
30+
Transaction tx = context -> {
31+
// no-op
32+
};
33+
34+
String info = tx.info();
35+
36+
// Check it is correctly deserialized to an empty object
37+
Gson gson = new Gson();
38+
Map<String, ?> deserialized = gson.fromJson(info, new TypeToken<Map<String, ?>>() {
39+
}.getType());
40+
assertThat(deserialized).isEmpty();
41+
}
42+
}

0 commit comments

Comments
 (0)