Skip to content

Commit 09d6f1c

Browse files
committed
fix java generator
1 parent b6c88d9 commit 09d6f1c

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

dcrpcgen/java/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ def generate_method(method: dict[str, Any]) -> str:
7777
decode_type(param["schema"])[0] + " " + param["name"] for param in params
7878
)
7979
text += ") throws RpcException {\n"
80-
args = ", ".join([f'"{name}"'] + [param["name"] for param in params])
80+
args = ", ".join(
81+
[f'"{name}"'] + [f'mapper.valueToTree({param["name"]})' for param in params]
82+
)
8183
if result_type == "void":
8284
text += f" transport.call({args});\n"
8385
else:

dcrpcgen/java/templates/BaseTransport.java.j2

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ public abstract class BaseTransport implements Rpc.Transport {
2626
/* Get next Response as raw JSON String from the RPC server */
2727
protected abstract String getResponse();
2828

29-
public void call(String method, Object... params) throws RpcException {
29+
public ObjectMapper getObjectMapper() {
30+
return mapper;
31+
}
32+
33+
public void call(String method, JsonNode... params) throws RpcException {
3034
innerCall(method, params);
3135
}
3236

33-
public <T> T callForResult(TypeReference<T> resultType, String method, Object... params) throws RpcException {
37+
public <T> T callForResult(TypeReference<T> resultType, String method, JsonNode... params) throws RpcException {
3438
try {
3539
JsonNode node = innerCall(method, params);
3640
if (node.isNull()) return null;
@@ -40,7 +44,7 @@ public abstract class BaseTransport implements Rpc.Transport {
4044
}
4145
}
4246

43-
private JsonNode innerCall(String method, Object... params) throws RpcException {
47+
private JsonNode innerCall(String method, JsonNode... params) throws RpcException {
4448
int id;
4549
synchronized (this) {
4650
id = ++requestId;
@@ -101,10 +105,10 @@ public abstract class BaseTransport implements Rpc.Transport {
101105
private static class Request {
102106
private final String jsonrpc = "2.0";
103107
public final String method;
104-
public final Object[] params;
108+
public final JsonNode[] params;
105109
public final int id;
106110

107-
public Request(String method, Object[] params, int id) {
111+
public Request(String method, JsonNode[] params, int id) {
108112
this.method = method;
109113
this.params = params;
110114
this.id = id;

dcrpcgen/java/templates/Rpc.java.j2

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@
22
package {{ package }};
33

44
import com.fasterxml.jackson.core.type.TypeReference;
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
57

68
import {{ package + ".types" }}.*;
79

810
public class Rpc {
911

1012
public interface Transport {
11-
public void call(String method, Object... params) throws RpcException;
12-
public <T> T callForResult(TypeReference<T> resultType, String method, Object... params) throws RpcException;
13+
void call(String method, JsonNode... params) throws RpcException;
14+
<T> T callForResult(TypeReference<T> resultType, String method, JsonNode... params) throws RpcException;
15+
ObjectMapper getObjectMapper();
1316
}
1417

1518
public final Transport transport;
19+
private final ObjectMapper mapper;
1620

1721
public Rpc(Transport transport) {
1822
this.transport = transport;
23+
this.mapper = transport.getObjectMapper();
1924
}
2025

2126
{% for method in methods %}

dcrpcgen/java/templates/SuperClass.java.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
88
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
99

1010
{% if "description" in schema %}{{ create_comment(schema["description"]) }}{% endif -%}
11-
@JsonTypeInfo(use=Id.MINIMAL_CLASS, include=As.PROPERTY, property="kind")
12-
@JsonSubTypes({ {%- for typ in schema["oneOf"] %}{% if loop.index != 1 %}, {% endif %}@Type({{ name }}.{{ get_subtype_name(typ) }}.class){% endfor -%} })
11+
@JsonTypeInfo(use=Id.NAME, include=As.PROPERTY, property="kind")
12+
@JsonSubTypes({ {%- for typ in schema["oneOf"] %}{% if loop.index != 1 %}, {% endif %}@Type(value = {{ name }}.{{ get_subtype_name(typ) }}.class, name="{{ get_subtype_name(typ) }}"){% endfor -%} })
1313
public abstract class {{ name }} {
1414

1515
{% for typ in schema["oneOf"] %}

dcrpcgen/java/utils.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ def create_comment(text: str, indentation: str = "") -> str:
1717

1818
comment = f"{indentation}/**\n"
1919
for line in text.split("\n"):
20-
if line.strip():
21-
comment += f"{indentation} * {line}\n"
22-
else:
23-
comment += f"{indentation} *\n"
20+
comment += f"{indentation} * {line.strip() or '<p>'}\n"
2421
return comment + f"{indentation} */\n"
2522

2623

0 commit comments

Comments
 (0)