-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Stricter parseValue coercion for String, Int, Float, Boolean #3553
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
da2bce1
Add error strings
dondonz b7a648c
Add stricter string parseValue coercion
dondonz 0336f9b
Add stricter Boolean parseValue coercion
dondonz 2aecb85
Add stricter float parseValue coercion
dondonz ea7661f
Add back stricter parseValue coercion for Int
dondonz 8dfdcc4
Fix tests
dondonz b7dc516
Tidy up
dondonz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,13 +67,12 @@ private Boolean serializeImpl(@NotNull Object input, @NotNull Locale locale) { | |
|
|
||
| @NotNull | ||
| private Boolean parseValueImpl(@NotNull Object input, @NotNull Locale locale) { | ||
| Boolean result = convertImpl(input); | ||
| if (result == null) { | ||
| if (!(input instanceof Boolean)) { | ||
| throw new CoercingParseValueException( | ||
| i18nMsg(locale, "Boolean.notBoolean", typeName(input)) | ||
| i18nMsg(locale, "Boolean.unexpectedRawValueType", typeName(input)) | ||
| ); | ||
| } | ||
| return result; | ||
| return (Boolean) input; | ||
| } | ||
|
|
||
| private static boolean parseLiteralImpl(@NotNull Object input, @NotNull Locale locale) { | ||
|
|
@@ -129,7 +128,7 @@ public Boolean parseLiteral(@NotNull Object input) { | |
|
|
||
| @Override | ||
| @Deprecated | ||
| public Value valueToLiteral(@NotNull Object input) { | ||
| public @NotNull Value<?> valueToLiteral(@NotNull Object input) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IDEA complained so I'm fixing these up while I'm here |
||
| return valueToLiteralImpl(input, Locale.getDefault()); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,19 +55,19 @@ class ScalarsBooleanTest extends Specification { | |
| Scalars.GraphQLBoolean.getCoercing().serialize(value, GraphQLContext.default, Locale.default) == result | ||
|
|
||
| where: | ||
| value | result | ||
| true | true | ||
| "false" | false | ||
| "true" | true | ||
| "True" | true | ||
| 0 | false | ||
| 1 | true | ||
| -1 | true | ||
| new Long(42345784398534785l) | true | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A bunch of constructors were deprecated in Java 9. Now we're on Java 11 let's update these |
||
| new Double(42.3) | true | ||
| new Float(42.3) | true | ||
| Integer.MAX_VALUE + 1l | true | ||
| Integer.MIN_VALUE - 1l | true | ||
| value | result | ||
| true | true | ||
| "false" | false | ||
| "true" | true | ||
| "True" | true | ||
| 0 | false | ||
| 1 | true | ||
| -1 | true | ||
| Long.valueOf(42345784398534785l) | true | ||
| Double.valueOf(42.3) | true | ||
| Float.valueOf(42.3) | true | ||
| Integer.MAX_VALUE + 1l | true | ||
| Integer.MIN_VALUE - 1l | true | ||
| } | ||
|
|
||
| @Unroll | ||
|
|
@@ -76,19 +76,19 @@ class ScalarsBooleanTest extends Specification { | |
| Scalars.GraphQLBoolean.getCoercing().serialize(value) == result // Retain deprecated method for test coverage | ||
|
|
||
| where: | ||
| value | result | ||
| true | true | ||
| "false" | false | ||
| "true" | true | ||
| "True" | true | ||
| 0 | false | ||
| 1 | true | ||
| -1 | true | ||
| new Long(42345784398534785l) | true | ||
| new Double(42.3) | true | ||
| new Float(42.3) | true | ||
| Integer.MAX_VALUE + 1l | true | ||
| Integer.MIN_VALUE - 1l | true | ||
| value | result | ||
| true | true | ||
| "false" | false | ||
| "true" | true | ||
| "True" | true | ||
| 0 | false | ||
| 1 | true | ||
| -1 | true | ||
| Long.valueOf(42345784398534785l) | true | ||
| Double.valueOf(42.3) | true | ||
| Float.valueOf(42.3) | true | ||
| Integer.MAX_VALUE + 1l | true | ||
| Integer.MIN_VALUE - 1l | true | ||
| } | ||
|
|
||
| @Unroll | ||
|
|
@@ -131,27 +131,6 @@ class ScalarsBooleanTest extends Specification { | |
| false | false | ||
| } | ||
|
|
||
| @Unroll | ||
| def "parseValue parses non-Boolean input #value"() { | ||
| expect: | ||
| Scalars.GraphQLBoolean.getCoercing().parseValue(value, GraphQLContext.default, Locale.default) == result | ||
|
|
||
| where: | ||
| value | result | ||
| true | true | ||
| "false" | false | ||
| "true" | true | ||
| "True" | true | ||
| 0 | false | ||
| 1 | true | ||
| -1 | true | ||
| new Long(42345784398534785l) | true | ||
| new Double(42.3) | true | ||
| new Float(42.3) | true | ||
| Integer.MAX_VALUE + 1l | true | ||
| Integer.MIN_VALUE - 1l | true | ||
| } | ||
|
|
||
| @Unroll | ||
| def "parseValue throws exception for invalid input #value"() { | ||
| when: | ||
|
|
@@ -160,8 +139,19 @@ class ScalarsBooleanTest extends Specification { | |
| thrown(CoercingParseValueException) | ||
|
|
||
| where: | ||
| value | _ | ||
| new Object() | _ | ||
| value | _ | ||
| new Object() | _ | ||
| "false" | _ | ||
| "true" | _ | ||
| "True" | _ | ||
| 0 | _ | ||
| 1 | _ | ||
| -1 | _ | ||
| Long.valueOf(42345784398534785l) | _ | ||
| Double.valueOf(42.3) | _ | ||
| Float.valueOf(42.3) | _ | ||
| Integer.MAX_VALUE + 1l | _ | ||
| Integer.MIN_VALUE - 1l | _ | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This PR is a reversion of PR #3186, plus little bits of tidying up