2222
2323import java .util .Collections ;
2424import java .util .Set ;
25+ import java .util .regex .Pattern ;
2526
2627public class TypeValidator extends BaseJsonValidator implements JsonValidator {
2728 private static final Logger logger = LoggerFactory .getLogger (TypeValidator .class );
29+ private static final String NUMERIC_PATTERN = "-?\\ d+(\\ .\\ d+)?" ;
30+ private static Pattern numericPattern = Pattern .compile (NUMERIC_PATTERN );
31+ private static final String INTEGER_PATTERN = "\\ -?\\ d+" ;
32+ private static Pattern integerPattern = Pattern .compile (INTEGER_PATTERN );
2833
2934 private JsonType schemaType ;
3035 private UnionTypeValidator unionTypeValidator ;
@@ -48,6 +53,9 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
4853 }
4954
5055 JsonType nodeType = TypeFactory .getValueNodeType (node );
56+ // in the case that node type is not the same as schema type, try to convert node to the
57+ // same type of schema. In REST API, query parameters, path parameters and headers are all
58+ // string type and we must convert, otherwise, all schema validations will fail.
5159 if (nodeType != schemaType ) {
5260 if (schemaType == JsonType .ANY ) {
5361 return Collections .emptySet ();
@@ -61,11 +69,73 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
6169 return Collections .emptySet ();
6270 }
6371 }
72+ if (nodeType == JsonType .STRING ) {
73+ if (schemaType == JsonType .INTEGER ) {
74+ if (isInteger (node .textValue ())) {
75+ return Collections .emptySet ();
76+ }
77+ } else if (schemaType == JsonType .BOOLEAN ) {
78+ if (isBoolean (node .textValue ())) {
79+ return Collections .emptySet ();
80+ }
81+ } else if (schemaType == JsonType .NUMBER ) {
82+ if (isNumeric (node .textValue ())) {
83+ return Collections .emptySet ();
84+ }
85+ }
86+ }
87+ return Collections .singleton (buildValidationMessage (at , nodeType .toString (), schemaType .toString ()));
88+ }
89+ return Collections .emptySet ();
90+ }
6491
65- return Collections .singleton (buildValidationMessage (at , nodeType .toString (), schemaType .toString ()));
92+ public static boolean isInteger (String str ) {
93+ if (str == null ) {
94+ return false ;
95+ }
96+ if (str .isEmpty ()) {
97+ return false ;
98+ }
99+ int i = 0 ;
100+ if (str .charAt (0 ) == '-' ) {
101+ if (str .length () == 1 ) {
102+ return false ;
103+ }
104+ i = 1 ;
105+ }
106+ for (; i < str .length (); i ++) {
107+ char c = str .charAt (i );
108+ if (c < '0' || c > '9' ) {
109+ return false ;
110+ }
66111 }
112+ return true ;
113+ }
67114
68- return Collections .emptySet ();
115+ public static boolean isBoolean (String s ) {
116+ return Boolean .parseBoolean (s );
69117 }
70118
119+ public static boolean isNumeric (String str ) {
120+ if (str == null ) {
121+ return false ;
122+ }
123+ if (str .isEmpty ()) {
124+ return false ;
125+ }
126+ int i = 0 ;
127+ if (str .charAt (0 ) == '-' ) {
128+ if (str .length () == 1 ) {
129+ return false ;
130+ }
131+ i = 1 ;
132+ }
133+ for (; i < str .length (); i ++) {
134+ char c = str .charAt (i );
135+ if (c < '0' || c > '9' && c != '.' ) {
136+ return false ;
137+ }
138+ }
139+ return true ;
140+ }
71141}
0 commit comments