-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Binders for OptionalInt, OptionalLong, OptionalDouble #10825
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the pull request.
Please see my inside the code diff: You need to add the javascriptUnbind
methods for each binder.
Plus two more things 👇
First you also need to add JavascriptLiteral
s for each OptionalXXX type. Like we have for Optional
here:
playframework/core/play/src/main/scala/play/api/mvc/Binders.scala
Lines 258 to 262 in 9852822
/** | |
* Convert a Java Optional to Javascript literal (use "null" for an empty Optional) | |
*/ | |
implicit def literalJavaOption[T](implicit jsl: JavascriptLiteral[T]): JavascriptLiteral[Optional[T]] = | |
(value: Optional[T]) => value.asScala.map(jsl.to(_)).getOrElse("null") |
E.g. for OptionalInt
you have to add:
/**
* Convert a Java OptionalInt to Javascript literal (use "null" for an empty OptionalInt)
*/
implicit def literalJavaOptionalInt: JavascriptLiteral[OptionalInt] =
(value: OptionalInt) => value.asScala.map(_.toString).getOrElse("null")
Same for OptionalLong
and OptionalDouble
please.
Second, please add tests, I think that should be here: https://github.com/playframework/playframework/tree/master/dev-mode/sbt-plugin/src/sbt-test/play-sbt-plugin/routes-compiler-routes-compilation
E.g. look in the conf/routes
file, there is a route for an Optional
param, which points to this action method.
Finally a test is defined here.
Please do similar tests for OptionalInt
, OptionalDouble
and OptionalLong
.
Also it would be good to add a test with default values, see here.
Bonus: You could also add tests for the Javascript reverse routes (to test the literals you add, etc.) here: https://github.com/playframework/playframework/blob/28f19894deae8712682352acf53d99eb4a24da46/dev-mode/sbt-plugin/src/sbt-test/play-sbt-plugin/routes-compiler-routes-compilation/tests/assets/JavaScriptRouterSpec.js Just re-use the routes you will add for the tests above.
(Also, for this to work I think you need to the reverse routes here:
Lines 15 to 24 in 9852822
.JavaScriptReverseRouter( | |
"jsRoutes", | |
None, | |
"localhost", | |
Assets.versioned, | |
Application.index, | |
Application.post, | |
Application.withParam, | |
Application.takeBool | |
) |
Thanks!
Unfortunately, I could not run tests locally because of error:
|
it("should add default parameters to the query string", function() { | ||
var data = jsRoutes.controllers.Application.takeOptionalIntWithDefault(); | ||
assert.equal("/take-joptint-d?x=123", data.url); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about this test, is it right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be like:
it("should generate a url for default parameters", function() {
var data = jsRoutes.controllers.Application.takeOptionalIntWithDefault();
assert.equal("/take-joptint-d", data.url);
});
Or:
it("should add non-native JavaScript parameters to the query string", function() {
var data = jsRoutes.controllers.Application.takeOptionalInt(123);
assert.equal("/take-joptint?x=123", data.url);
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be like:
Yeah, I think your comment is right. However, what do you think of changing the description of the tests a bit and add three more, like:
it("should generate a url for an OptionalInt parameter (which has a default)", function() {
var data = jsRoutes.controllers.Application.takeOptionalIntWithDefault();
assert.equal("/take-joptint-d", data.url);
});
it("should generate a url for an OptionalInt parameter (which has a default) when default param value is passed", function() {
var data = jsRoutes.controllers.Application.takeOptionalIntWithDefault(123);
assert.equal("/take-joptint-d", data.url);
});
it("should add an OptionalInt parameter (which has a default) to the query string", function() {
var data = jsRoutes.controllers.Application.takeOptionalIntWithDefault(987);
assert.equal("/take-joptint-d?x=987", data.url);
});
it("should generate a url for an OptionalInt parameter", function() {
var data = jsRoutes.controllers.Application.takeOptionalInt();
assert.equal("/take-joptint", data.url);
});
it("should add an OptionalInt parameter to the query string", function() {
var data = jsRoutes.controllers.Application.takeOptionalInt(123);
assert.equal("/take-joptint?x=123", data.url);
});
I don't know what test is failing. Travis does not write that |
dev-mode/sbt-plugin/src/sbt-test/play-sbt-plugin/routes-compiler-routes-compilation/conf/routes
Outdated
Show resolved
Hide resolved
...lugin/src/sbt-test/play-sbt-plugin/routes-compiler-routes-compilation/tests/RouterSpec.scala
Show resolved
Hide resolved
...lugin/src/sbt-test/play-sbt-plugin/routes-compiler-routes-compilation/tests/RouterSpec.scala
Outdated
Show resolved
Hide resolved
What's wrong now? -_-
|
...test/play-sbt-plugin/routes-compiler-routes-compilation/tests/assets/JavaScriptRouterSpec.js
Show resolved
Hide resolved
@mkurz any updates? |
@Mergifyio backport 2.8.x |
Command
|
Command
|
Binders for OptionalInt, OptionalLong, OptionalDouble (backport #10825)
Pull Request Checklist
Helpful things
Fixes
Fixes #10816
Purpose
Add binders for Java
OptionalInt
,OptionalLong
,OptionalDouble
References
#10816