I am creating a web application which parses URLS for route parameters for client look ups.
Example: www.example.com#userId=5
Ignore security loop holes on that.
I noticed that when navigating to the page, it would call: onRouteParams(Map params)
in PolymerDart. This would have the list of parameters. I would then say something like:
int id = params.containsKey("userId") ? params["userId"] : -1;
It would assign it to there. I then attempted to print out the variables
#userId=5
userId: 5, Type: int
id: 5, idType: int
and I changed the passed in parameter to be hodor
#userId=hodor
userId: hodor, Type: String
id: hodor, idType: String
This issue I had noticed, creates an issue because you are going to carry out code related to it being an int, but done on a string. So the converted javascript is going to not be solid at all.
In Dartium the issue is caught:
VM367:1 EXCEPTION: NoSuchMethodError: Class 'String' has no instance method '<='.
Receiver: "hodor"
Tried calling: <=(0)
So this makes me curious if I am doing something wrong.
When dealing with the build as per the documents it will handle Self->Self understanding. But when dealing with URLs this is something that is outside of the scope.
So if we are reliant on something public, such as URL params, will that function onRouteParams or other have to be designed more so like this?
onRouteParams(Map params){
int id = params.containsKey("userId") ? params["userId"] : -1;
if( ! id is int ){
#error in logic because type of id was overridden to string
}
if (id == -1){
#user id not in params, follow perscribed workflow.
}
}
A base fundamental of logic would say the parameters of a function are validated. Here it isnt really the case because it is valid, it is a MAP. The issue that i was curious about is that when you do assignment, it should validate that it is what it is suppose to be.
Something like:
int id = params["id"];
should be parsed to javascript as: (like this polyfill: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger)
if (Number.isInteger(params["id"]){
var id = params["id"];
}else{
throw new TypeError("not castable string attempting to cast to int");
}
or similar.
This is an error i have run into which upon going back on happens all throughout application code and could be an issue throughout other peoples applications unknowning.
If you have to ask "Why would someone accidently put foo into the url param" the logic reasoning is that if they want to save the URL and paste it somewhere else, they might accidently tap the v one too many times, so the last parameter would be: www.example.com#userId=5v
which would instantly cast userId to a string and causing this kind of issue to happen.
I was looking into strong mode, and how it builds the application for dart2js, how routed parameters are defined, etc. Seems that this issue isn't really addressed.
All of the tags are relevant because maybe there is some aspect I am missing that someone else has figured out. It definitely seems like a dart typing issue which came to light due to routing.