Skip to content

Commit d7c5f60

Browse files
committed
- normalize decoder usage of jsonRPC all fire the same error
1 parent 823dfbb commit d7c5f60

File tree

7 files changed

+28
-40
lines changed

7 files changed

+28
-40
lines changed

modules/jooby-avaje-jsonb/src/main/java/io/jooby/internal/avaje/jsonb/AvajeJsonRpcDecoder.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,37 @@
55
*/
66
package io.jooby.internal.avaje.jsonb;
77

8+
import java.lang.reflect.Type;
9+
810
import io.avaje.jsonb.JsonType;
911
import io.avaje.jsonb.Jsonb;
12+
import io.jooby.exception.MissingValueException;
13+
import io.jooby.exception.TypeMismatchException;
1014
import io.jooby.jsonrpc.JsonRpcDecoder;
11-
import io.jooby.jsonrpc.JsonRpcErrorCode;
12-
import io.jooby.jsonrpc.JsonRpcException;
1315

1416
public class AvajeJsonRpcDecoder<T> implements JsonRpcDecoder<T> {
1517

1618
private final Jsonb jsonb;
19+
private final Type type;
1720
private final JsonType<T> typeAdapter;
1821

19-
public AvajeJsonRpcDecoder(Jsonb jsonb, JsonType<T> typeAdapter) {
22+
public AvajeJsonRpcDecoder(Jsonb jsonb, Type type) {
2023
this.jsonb = jsonb;
21-
this.typeAdapter = typeAdapter;
24+
this.type = type;
25+
this.typeAdapter = jsonb.type(type);
2226
}
2327

2428
@Override
2529
public T decode(String name, Object node) {
2630
try {
2731
if (node == null) {
28-
return null;
32+
throw new MissingValueException(name);
2933
}
3034
// Convert the Map/List/primitive back to JSON, then to the target type
3135
// This leverages Avaje's exact mappings without needing a tree traversal model
32-
String json = jsonb.toJson(node);
33-
return typeAdapter.fromJson(json);
36+
return typeAdapter.fromJson(jsonb.toJson(node));
3437
} catch (Exception x) {
35-
throw new JsonRpcException(
36-
JsonRpcErrorCode.INVALID_PARAMS,
37-
"Invalid params: unable to map parameter '" + name + "'",
38-
x);
38+
throw new TypeMismatchException(name, type, x);
3939
}
4040
}
4141
}

modules/jooby-avaje-jsonb/src/main/java/io/jooby/internal/avaje/jsonb/AvajeJsonRpcParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public AvajeJsonRpcParser(Jsonb jsonb) {
2222

2323
@Override
2424
public <T> JsonRpcDecoder<T> decoder(Type type) {
25-
return new AvajeJsonRpcDecoder<>(jsonb, jsonb.type(type));
25+
return new AvajeJsonRpcDecoder<>(jsonb, type);
2626
}
2727

2828
@Override

modules/jooby-avaje-jsonb/src/main/java/io/jooby/internal/avaje/jsonb/AvajeJsonRpcReader.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,10 @@ public String nextString(String name) {
9999

100100
@Override
101101
public <T> T nextObject(String name, JsonRpcDecoder<T> decoder) {
102-
Object val = require(name);
102+
var val = require(name);
103103
return decoder.decode(name, val);
104104
}
105105

106106
@Override
107-
public void close() {
108-
// Nothing to close for in-memory collections
109-
}
107+
public void close() {}
110108
}

modules/jooby-jackson/src/main/java/io/jooby/internal/jackson/JacksonJsonRpcDecoder.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
import com.fasterxml.jackson.databind.JavaType;
1111
import com.fasterxml.jackson.databind.JsonNode;
1212
import com.fasterxml.jackson.databind.ObjectMapper;
13+
import io.jooby.exception.MissingValueException;
14+
import io.jooby.exception.TypeMismatchException;
1315
import io.jooby.jsonrpc.JsonRpcDecoder;
14-
import io.jooby.jsonrpc.JsonRpcErrorCode;
15-
import io.jooby.jsonrpc.JsonRpcException;
1616

1717
public class JacksonJsonRpcDecoder<T> implements JsonRpcDecoder<T> {
1818

@@ -27,15 +27,12 @@ public JacksonJsonRpcDecoder(ObjectMapper mapper, Type type) {
2727
@Override
2828
public T decode(String name, Object node) {
2929
try {
30-
if (node == null || ((JsonNode) node).isNull()) {
31-
return null;
30+
if (node == null || ((JsonNode) node).isNull() || ((JsonNode) node).isMissingNode()) {
31+
throw new MissingValueException(name);
3232
}
3333
return mapper.treeToValue((JsonNode) node, javaType);
3434
} catch (Exception x) {
35-
throw new JsonRpcException(
36-
JsonRpcErrorCode.INVALID_PARAMS,
37-
"Invalid params: unable to map parameter '" + name + "'",
38-
x);
35+
throw new TypeMismatchException(name, javaType, x);
3936
}
4037
}
4138
}

modules/jooby-jackson/src/main/java/io/jooby/internal/jackson/JacksonJsonRpcReader.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,10 @@ public String nextString(String name) {
107107

108108
@Override
109109
public <T> T nextObject(String name, JsonRpcDecoder<T> decoder) {
110-
JsonNode node = requireNode(name);
110+
var node = requireNode(name);
111111
return decoder.decode(name, node);
112112
}
113113

114114
@Override
115-
public void close() {
116-
// No network resources to release for an in-memory JsonNode tree
117-
}
115+
public void close() {}
118116
}

modules/jooby-jackson3/src/main/java/io/jooby/internal/jackson3/JacksonJsonRpcDecoder.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
import java.lang.reflect.Type;
99

10+
import io.jooby.exception.MissingValueException;
11+
import io.jooby.exception.TypeMismatchException;
1012
import io.jooby.jsonrpc.JsonRpcDecoder;
11-
import io.jooby.jsonrpc.JsonRpcErrorCode;
12-
import io.jooby.jsonrpc.JsonRpcException;
1313
import tools.jackson.databind.JavaType;
1414
import tools.jackson.databind.JsonNode;
1515
import tools.jackson.databind.ObjectMapper;
@@ -27,15 +27,12 @@ public JacksonJsonRpcDecoder(ObjectMapper mapper, Type type) {
2727
@Override
2828
public T decode(String name, Object node) {
2929
try {
30-
if (node == null || ((JsonNode) node).isNull()) {
31-
return null;
30+
if (node == null || ((JsonNode) node).isNull() || ((JsonNode) node).isMissingNode()) {
31+
throw new MissingValueException(name);
3232
}
3333
return mapper.treeToValue((JsonNode) node, javaType);
3434
} catch (Exception x) {
35-
throw new JsonRpcException(
36-
JsonRpcErrorCode.INVALID_PARAMS,
37-
"Invalid params: unable to map parameter '" + name + "'",
38-
x);
35+
throw new TypeMismatchException(name, javaType, x);
3936
}
4037
}
4138
}

modules/jooby-jackson3/src/main/java/io/jooby/internal/jackson3/JacksonJsonRpcReader.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,10 @@ public String nextString(String name) {
107107

108108
@Override
109109
public <T> T nextObject(String name, JsonRpcDecoder<T> decoder) {
110-
JsonNode node = requireNode(name);
110+
var node = requireNode(name);
111111
return decoder.decode(name, node);
112112
}
113113

114114
@Override
115-
public void close() {
116-
// No network resources to release for an in-memory JsonNode tree
117-
}
115+
public void close() {}
118116
}

0 commit comments

Comments
 (0)