Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit 14f6a5a

Browse files
hiwayamaHiromasa IWAYAMA
authored andcommitted
bug fix
1 parent 5ee5017 commit 14f6a5a

File tree

7 files changed

+79
-8
lines changed

7 files changed

+79
-8
lines changed

.idea/.name

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/com/github/hiwayama/jsonrpc4j/jsonSchema/JsonRpcSchemaGenerator.java

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
import com.fasterxml.jackson.databind.JsonMappingException;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
6+
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;
7+
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;
8+
import com.fasterxml.jackson.module.jsonSchema.factories.WrapperFactory;
69
import com.fasterxml.jackson.module.jsonSchema.types.ArraySchema;
710
import com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema;
11+
import com.github.hiwayama.jsonrpc4j.jsonSchema.annotations.JsonRpcResponseTitle;
812
import com.github.hiwayama.jsonrpc4j.jsonSchema.annotations.JsonSchemaTitle;
913
import com.googlecode.jsonrpc4j.JsonRpcMethod;
1014
import com.googlecode.jsonrpc4j.JsonRpcParam;
@@ -14,11 +18,39 @@
1418
import java.lang.reflect.ParameterizedType;
1519
import java.lang.reflect.Type;
1620
import java.util.ArrayList;
21+
import java.util.HashMap;
1722
import java.util.List;
23+
import java.util.Map;
1824

1925
public class JsonRpcSchemaGenerator {
20-
private ObjectMapper mapper = new ObjectMapper();
21-
private com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator generator = new com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator(mapper);
26+
private static final Map<String, Class> PRIMITIVE_CLASS_MAP = new HashMap<>();
27+
static {
28+
PRIMITIVE_CLASS_MAP.put("byte", byte.class);
29+
PRIMITIVE_CLASS_MAP.put("short", short.class);
30+
PRIMITIVE_CLASS_MAP.put("int", int.class);
31+
PRIMITIVE_CLASS_MAP.put("long", long.class);
32+
PRIMITIVE_CLASS_MAP.put("float", float.class);
33+
PRIMITIVE_CLASS_MAP.put("double", double.class);
34+
PRIMITIVE_CLASS_MAP.put("boolean", boolean.class);
35+
PRIMITIVE_CLASS_MAP.put("char", char.class);
36+
}
37+
38+
private ObjectMapper mapper;
39+
private JsonSchemaGenerator generator;
40+
41+
public JsonRpcSchemaGenerator() {
42+
this(new ObjectMapper());
43+
}
44+
45+
public JsonRpcSchemaGenerator(ObjectMapper mapper) {
46+
this.mapper = mapper;
47+
this.generator = new JsonSchemaGenerator(mapper);
48+
}
49+
50+
public JsonRpcSchemaGenerator(ObjectMapper mapper, SchemaFactoryWrapper wrapperFactory) {
51+
this(mapper);
52+
this.generator = new JsonSchemaGenerator(mapper, wrapperFactory);
53+
}
2254

2355
private JsonSchema generateRequestSchema(Method methodObj) throws JsonMappingException, ClassNotFoundException {
2456
ObjectSchema schema = new ObjectSchema();
@@ -28,6 +60,11 @@ private JsonSchema generateRequestSchema(Method methodObj) throws JsonMappingExc
2860
if (annotations != null && annotations.length > 0) {
2961
JsonRpcParam paramNameAnno = (JsonRpcParam) annotations[0];
3062
String methodName = paramNameAnno.value();
63+
if (paramType instanceof Class) {
64+
if (((Class) paramType).isPrimitive()) {
65+
schema.putProperty(methodName, generator.generateSchema(PRIMITIVE_CLASS_MAP.get(paramType.getTypeName())));
66+
}
67+
}
3168
if (paramType instanceof ParameterizedType) {
3269
schema.putProperty(methodName, getCollectionSchema((ParameterizedType) paramType));
3370
} else {
@@ -42,14 +79,28 @@ private JsonSchema generateRequestSchema(Method methodObj) throws JsonMappingExc
4279

4380
private JsonSchema generateResponseSchema(Method method) throws JsonMappingException, ClassNotFoundException {
4481
Type returnType = method.getGenericReturnType();
45-
String resClass = null;
82+
83+
JsonSchema resSchema;
4684
if (returnType instanceof Class<?>) {
47-
resClass = returnType.getTypeName();
85+
if (((Class) returnType).isPrimitive()) {
86+
resSchema = generator.generateSchema(PRIMITIVE_CLASS_MAP.get(returnType.getTypeName()));
87+
} else {
88+
resSchema = generator.generateSchema((Class)returnType);
89+
}
4890
} else if(returnType instanceof ParameterizedType) {
4991
ParameterizedType parameterizedType = ((ParameterizedType)returnType);
50-
return getCollectionSchema(parameterizedType);
92+
resSchema = getCollectionSchema(parameterizedType);
93+
} else {
94+
return null;
5195
}
52-
return generator.generateSchema(Class.forName(resClass));
96+
97+
JsonRpcResponseTitle titleAnno = method.getAnnotation(JsonRpcResponseTitle.class);
98+
if (titleAnno != null) {
99+
String schemaTitle = titleAnno.value();
100+
resSchema.asSimpleTypeSchema().setTitle(schemaTitle);
101+
}
102+
103+
return resSchema;
53104
}
54105

55106
private JsonSchema getCollectionSchema(ParameterizedType type) throws ClassNotFoundException, JsonMappingException {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.hiwayama.jsonrpc4j.jsonSchema.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target({ElementType.METHOD})
10+
public @interface JsonRpcResponseTitle {
11+
String value();
12+
}

src/test/java/com/github/hiwayama/jsonrpc4j/jsonSchema/JsonSchemaCreatorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class JsonSchemaCreatorTest {
2020

2121
@Test
2222
public void test() throws IOException, ClassNotFoundException {
23-
for (String name : new String[] {"user.get.json", "user.list.json"}) {
23+
for (String name : new String[] {"user.get.json", "user.list.json", "user.getById.json"}) {
2424
StringBuilder expeted = new StringBuilder();
2525
try (BufferedReader br = new BufferedReader(
2626
new InputStreamReader(JsonSchemaCreatorTest.class.getClassLoader().getResourceAsStream(name)))) {

src/test/java/com/github/hiwayama/jsonrpc4j/jsonSchema/SampleService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.hiwayama.jsonrpc4j.jsonSchema;
22

3+
import com.github.hiwayama.jsonrpc4j.jsonSchema.annotations.JsonRpcResponseTitle;
34
import com.github.hiwayama.jsonrpc4j.jsonSchema.annotations.JsonSchemaTitle;
45
import com.googlecode.jsonrpc4j.JsonRpcMethod;
56
import com.googlecode.jsonrpc4j.JsonRpcParam;
@@ -14,7 +15,12 @@ public interface SampleService {
1415
@JsonSchemaTitle("get single user")
1516
SampleUser get(@JsonRpcParam("id") String userId);
1617

18+
@JsonRpcMethod("user.getById")
19+
@JsonSchemaTitle("get single user by numeric id")
20+
SampleUser getById(@JsonRpcParam("id") long id, @JsonRpcParam("opt") String type);
21+
1722
@JsonRpcMethod("user.list")
1823
@JsonSchemaTitle("get users")
24+
@JsonRpcResponseTitle("users list")
1925
List<SampleUser> getList(@JsonRpcParam("ids") List<String> userIds);
2026
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"method":"user.getById","title":"get single user by numeric id","request":{"type":"object","properties":{"id":{"type":"string","required":true},"opt":{"type":"string","required":true}}},"response":{"type":"object","id":"urn:jsonschema:com:github:hiwayama:jsonrpc4j:jsonSchema:SampleUser","properties":{"name":{"type":"string"},"inner":{"type":"object","id":"urn:jsonschema:com:github:hiwayama:jsonrpc4j:jsonSchema:SampleUser:InnerObj","properties":{"age":{"type":"integer"},"string":{"type":"string"},"userType":{"type":"string","enum":["A1","B1","C1"]}}}}}}

src/test/resources/user.list.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"method":"user.list","title":"get users","request":{"type":"object","properties":{"ids":{"type":"array","required":true,"items":{"type":"string"}}}},"response":{"type":"array","items":{"type":"object","id":"urn:jsonschema:com:github:hiwayama:jsonrpc4j:jsonSchema:SampleUser","properties":{"name":{"type":"string"},"inner":{"type":"object","id":"urn:jsonschema:com:github:hiwayama:jsonrpc4j:jsonSchema:SampleUser:InnerObj","properties":{"age":{"type":"integer"},"string":{"type":"string"},"userType":{"type":"string","enum":["A1","B1","C1"]}}}}}}}
1+
{"method":"user.list","title":"get users","request":{"type":"object","properties":{"ids":{"type":"array","required":true,"items":{"type":"string"}}}},"response":{"type":"array","title":"users list","items":{"type":"object","id":"urn:jsonschema:com:github:hiwayama:jsonrpc4j:jsonSchema:SampleUser","properties":{"name":{"type":"string"},"inner":{"type":"object","id":"urn:jsonschema:com:github:hiwayama:jsonrpc4j:jsonSchema:SampleUser:InnerObj","properties":{"age":{"type":"integer"},"string":{"type":"string"},"userType":{"type":"string","enum":["A1","B1","C1"]}}}}}}}

0 commit comments

Comments
 (0)