33import com .fasterxml .jackson .databind .JsonMappingException ;
44import com .fasterxml .jackson .databind .ObjectMapper ;
55import 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 ;
69import com .fasterxml .jackson .module .jsonSchema .types .ArraySchema ;
710import com .fasterxml .jackson .module .jsonSchema .types .ObjectSchema ;
11+ import com .github .hiwayama .jsonrpc4j .jsonSchema .annotations .JsonRpcResponseTitle ;
812import com .github .hiwayama .jsonrpc4j .jsonSchema .annotations .JsonSchemaTitle ;
913import com .googlecode .jsonrpc4j .JsonRpcMethod ;
1014import com .googlecode .jsonrpc4j .JsonRpcParam ;
1418import java .lang .reflect .ParameterizedType ;
1519import java .lang .reflect .Type ;
1620import java .util .ArrayList ;
21+ import java .util .HashMap ;
1722import java .util .List ;
23+ import java .util .Map ;
1824
1925public 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 {
0 commit comments