Swagger integration in Eclipse Vert.X world. A dynamic Vert.X router, configured with a swagger file and a swagger-codegen plugin to generate a server stub.
Apache License 2.0
86
stars
35
forks
source link
Problem with the data type of parameters' default values #90
In xApiVerticle, a handler that needs to extract the values of a parameter does it by accessing message.body(). It has a problem with not-provided parameters.
For example, in this URL www.example.com/api/loans?status=x&page=1&size=10 the LoansApiVerticle has a listLoansHandler method that tries to extract and check the page parameter like this: if(message.body().getString("page") != null). Now, if the page parameter is not provided (it is optional), the getString method throws an exception and causes the request to fail!
The problem is that when a parameter is not provided via the request, it is set to its default (by the router). In the above-mentioned case, the page parameter is an integer, hence its value is stored as an integer in message.body() but the if checks for a string!
Assuming the Swagger spec is correct, the router should convert the default values to string and then pass them to the handlers. OR to respect the types set in the spec from the beginning (when constructing the message and its body).
I would also suggest, for optional parameters, to first check whether the message.body() contains the parameter and if so, then try to get its value. Something like if(message.body().containsKey("page") && message.body().getString("page") != null). If the key is available, then JsonObject itself has a getInteger which can be used; it does the type checks/casting required. It also provides get operators for many other types, which I would recommand to use.
In xApiVerticle, a handler that needs to extract the values of a parameter does it by accessing
message.body()
. It has a problem with not-provided parameters. For example, in this URL www.example.com/api/loans?status=x&page=1&size=10 the LoansApiVerticle has a listLoansHandler method that tries to extract and check thepage
parameter like this:if(message.body().getString("page") != null)
. Now, if thepage
parameter is not provided (it is optional), the getString method throws an exception and causes the request to fail! The problem is that when a parameter is not provided via the request, it is set to its default (by the router). In the above-mentioned case, thepage
parameter is aninteger
, hence its value is stored as an integer inmessage.body()
but theif
checks for astring
! Assuming the Swagger spec is correct, the router should convert the default values tostring
and then pass them to the handlers. OR to respect the types set in the spec from the beginning (when constructing the message and its body). I would also suggest, for optional parameters, to first check whether the message.body() contains the parameter and if so, then try to get its value. Something likeif(message.body().containsKey("page") && message.body().getString("page") != null)
. If the key is available, then JsonObject itself has agetInteger
which can be used; it does the type checks/casting required. It also provides get operators for many other types, which I would recommand to use.