Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@mkurz
Copy link
Member

@mkurz mkurz commented Oct 15, 2014

First of all: I created a very simple project to demonstrate the bugs I stumpled into using Play 2.3.5. (After running activator new routesTest play-java I only added some basic routes in conf/routes and added the accompanying actions in app/controllers/Application.java with this commit).

Now Step-by-Step:

1) There are some JavaScript literal binders missing
Have a look at the routes file. When you want to compile this routes file you get following errors:

No JavaScript literal binder found for type play.libs.F.Option[Integer]. Try to implement an implicit JavascriptLiteral for this type.

and

No JavaScript literal binder found for type Long. Try to implement an implicit JavascriptLiteral for this type.

and

No JavaScript literal binder found for type Boolean. Try to implement an implicit JavascriptLiteral for this type.

(You would also get an error for the missing UUID JavaScript literal binder - I already submitted the PR #3508 for this missing UUID JavaScript literal binder yesterday)

So with this PR I added the missing JavaScript literal binders for play.libs.F.Option, java.lang.Long and java.lang.Boolean.

2) NullPointerExceptions occur when assigning null as a default to Java Types in a route
After implementing the missing binders (including #3508), the project will now fail with a NullPointerException in the already existing java.lang.Integer JavaScript literal binder when assigning null as a default in a route (see line 13). If you have a look at my fix, you will see that's because .toString is called without checking if the default value is null before. (FYI: To actually get this NullPointerException you actually have to call the JavaScript route like in line 53 of the Application.java. If you don't call it you won't experience the NullPointerException).

3) When assigning null as a default to a String in a route, it gets converted to a JavaScript "null" String instead of a Javascript null value
See line 9 in the routes file. Without this PR following JavaScript is generated for JavaScript reverse routing (in case you run my test project you could just have a look at /assets/routes.js):

if (id == "null") {
return _wA({method:"GET", url:"/" + "stringNull"})
}

With my PR it correctly becomes:

if (id == null) {
return _wA({method:"GET", url:"/" + "stringNull"})
}

Please also backport this PR and #3508 to 2.3.x (be aware to rename JavascriptLiteral to JavascriptLitteral for the 2.3.x branch).
Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Causes the NullPointerException.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer:

value match {
  case null => "null"
  case _ => "\"" + value + "\""
}

It's clearer to read and saves on a few object allocations.

@mkurz mkurz force-pushed the JavaScriptLiteralBinderFixes branch from a4a1cb6 to be23be8 Compare October 21, 2014 11:09
@mkurz
Copy link
Member Author

mkurz commented Oct 21, 2014

@jroper Updated the PR with your suggestions.
Also backported this fix via PR #3526 for the 2.3.x branch.

jroper added a commit that referenced this pull request Oct 21, 2014
Fix NullPointerException, add missing JavaScript literal binders and correctly convert null in binders
@jroper jroper merged commit ff250f5 into playframework:master Oct 21, 2014
@jroper jroper added this to the 2.3.6 milestone Oct 28, 2014
@mkurz mkurz deleted the JavaScriptLiteralBinderFixes branch February 17, 2016 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants