You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by GitBox <gi...@apache.org> on 2020/08/25 15:18:49 UTC
[GitHub] [sling-org-apache-sling-scripting-sightly-runtime] raducotescu opened a new pull request #6: SLING-9314 - NPE in ObjectModel.toBoolean(Object) when object.toString() returns null
raducotescu opened a new pull request #6:
URL: https://github.com/apache/sling-org-apache-sling-scripting-sightly-runtime/pull/6
* avoid NPEs from classes which do not correctly override Object#toString()
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-scripting-sightly-runtime] sonarcloud[bot] commented on pull request #6: SLING-9314 - NPE in ObjectModel.toBoolean(Object) when object.toString() returns null
Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] commented on pull request #6:
URL: https://github.com/apache/sling-org-apache-sling-scripting-sightly-runtime/pull/6#issuecomment-680092243
Kudos, SonarCloud Quality Gate passed!
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=BUG)
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=VULNERABILITY) (and [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-si
ghtly-runtime&pullRequest=6&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=SECURITY_HOTSPOT) to review)
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=CODE_SMELL) [0 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=CODE_SMELL)
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/100.png' alt='100.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&metric=new_coverage&view=list) [100.0% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&metric=new_coverage&view=list)
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&metric=new_duplicated_lines_density&view=list)
<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/message_warning.png' alt='warning' width='16' height='16' /> The version of Java (1.8.0_252) you have used to run this analysis is deprecated and we will stop accepting it from October 2020. Please update to at least Java 11.
Read more [here](https://sonarcloud.io/documentation/upcoming/)
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-scripting-sightly-runtime] raducotescu edited a comment on pull request #6: SLING-9314 - NPE in ObjectModel.toBoolean(Object) when object.toString() returns null
Posted by GitBox <gi...@apache.org>.
raducotescu edited a comment on pull request #6:
URL: https://github.com/apache/sling-org-apache-sling-scripting-sightly-runtime/pull/6#issuecomment-680976995
@paul-bjorkstrand, I've applied a slightly modified version of your patch. Regarding the previous `#toString()` call on the passed object, I can concur that it looked like a hack. I don't even remember why it was written like this, but it doesn't really make sense.
For the casting behaviour I've opened a separate issue: https://issues.apache.org/jira/browse/SLING-9696. It's not related to the current one, but I noticed it after reading the specification one more time. There we will use the legacy flag you mentioned.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-scripting-sightly-runtime] sonarcloud[bot] removed a comment on pull request #6: SLING-9314 - NPE in ObjectModel.toBoolean(Object) when object.toString() returns null
Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] removed a comment on pull request #6:
URL: https://github.com/apache/sling-org-apache-sling-scripting-sightly-runtime/pull/6#issuecomment-680092243
Kudos, SonarCloud Quality Gate passed!
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=BUG)
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=VULNERABILITY) (and [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-si
ghtly-runtime&pullRequest=6&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=SECURITY_HOTSPOT) to review)
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=CODE_SMELL) [0 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=CODE_SMELL)
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/100.png' alt='100.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&metric=new_coverage&view=list) [100.0% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&metric=new_coverage&view=list)
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&metric=new_duplicated_lines_density&view=list)
<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/message_warning.png' alt='warning' width='16' height='16' /> The version of Java (1.8.0_252) you have used to run this analysis is deprecated and we will stop accepting it from October 2020. Please update to at least Java 11.
Read more [here](https://sonarcloud.io/documentation/upcoming/)
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-scripting-sightly-runtime] raducotescu merged pull request #6: SLING-9314 - NPE in ObjectModel.toBoolean(Object) when object.toString() returns null
Posted by GitBox <gi...@apache.org>.
raducotescu merged pull request #6:
URL: https://github.com/apache/sling-org-apache-sling-scripting-sightly-runtime/pull/6
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-scripting-sightly-runtime] raducotescu commented on pull request #6: SLING-9314 - NPE in ObjectModel.toBoolean(Object) when object.toString() returns null
Posted by GitBox <gi...@apache.org>.
raducotescu commented on pull request #6:
URL: https://github.com/apache/sling-org-apache-sling-scripting-sightly-runtime/pull/6#issuecomment-680976995
@paul-bjorkstrand, I've applied a slightly modified version of your patch.
For the casting behaviour I've opened a separate issue: https://issues.apache.org/jira/browse/SLING-9696.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-scripting-sightly-runtime] paul-bjorkstrand commented on pull request #6: SLING-9314 - NPE in ObjectModel.toBoolean(Object) when object.toString() returns null
Posted by GitBox <gi...@apache.org>.
paul-bjorkstrand commented on pull request #6:
URL: https://github.com/apache/sling-org-apache-sling-scripting-sightly-runtime/pull/6#issuecomment-680225036
Overall, looks like it will solve the originally reported issue, but after staring at it for a bit, I feel as if the whole `#toString` part is just a hacky way to handle booleans and boolean-like objects (custom objects that happen to return "true"/"false").
We might be better served by making the `String` and `Boolean` cases explicit, and letting normal objects fall to a bottom:
```java
public static boolean toBoolean(Object object) {
if (object == null) {
return false;
}
if (object instanceof Number) {
Number number = (Number) object;
return !(number.doubleValue() == 0.0);
}
if (object instanceof Boolean) {
return Boolean.TRUE.equals(object);
}
if (object instanceof String) {
return StringUtils.isNotBlank((String)object);
}
if (object instanceof Collection) {
return ((Collection) object).size() > 0;
}
if (object instanceof Map) {
return ((Map) object).size() > 0;
}
if (object instanceof Iterable<?>) {
return ((Iterable<?>) object).iterator().hasNext();
}
if (object instanceof Iterator<?>) {
return ((Iterator<?>) object).hasNext();
}
if (object instanceof Optional) {
return toBoolean(((Optional) object).orElse(false));
}
if (object instanceof Object[]) {
return Array.length(object);
}
return true;
}
```
I also broke out the array length check and replaced the cast + `.length` with (java.lang.reflect.)`Array#getLength(Object)`. It is a Hotspot instrinsic, so performance of it should be basically the same as the cast + `.length`. This change came from the difficulty (at a glance) of understanding the final line `!(object instanceof Object[]) || ((Object[]) object).length > 0;` and how it fit into the "default" not null object return value.
There is, though, the backwards-incompatible change, eliminating the ability for custom objects to consider themselves "truthy" by returning the strings `"true"` or `"false"` in their `#toString` methods.
If needed, there could be a flag added (to some HTL service) that could enable a "legacy" mode and that boolean is passed in. That might add the following just above the final return:
```java
if (useLegacyTruthyFalsyToString) {
String s = StringUtils.trim(object.toString());
return Boolean.parseBoolean(s);
}
```
I would question the need for this, though, because [it isn't specified][1]:
> **Attribute value**: optional; evaluated as Boolean (but not type-cased to Boolean when exposed in a variable); evaluates to false if the value is omitted.
"Evaluated as boolean" is quite a broad statement, and could be interpreted to mean "if object is not of a special type (map/iterable/iterator/string/boolean/number), then is `true` if not null. Perhaps this means the specification warrants further clarification, too since the [boolean casting part of the spec][2] also lacks detail on "how to cast an object".
I think the readability is worth the backwards incompatibility (of this not-specified behavior), without the extra complexity of the legacy handling, WDYT?
[1]: https://github.com/adobe/htl-spec/blob/master/SPECIFICATION.md#225-test
[2]: https://github.com/adobe/htl-spec/blob/master/SPECIFICATION.md#1151-boolean
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [sling-org-apache-sling-scripting-sightly-runtime] sonarcloud[bot] commented on pull request #6: SLING-9314 - NPE in ObjectModel.toBoolean(Object) when object.toString() returns null
Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] commented on pull request #6:
URL: https://github.com/apache/sling-org-apache-sling-scripting-sightly-runtime/pull/6#issuecomment-680978381
Kudos, SonarCloud Quality Gate passed!
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=BUG)
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=VULNERABILITY) (and [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-si
ghtly-runtime&pullRequest=6&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=SECURITY_HOTSPOT) to review)
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=CODE_SMELL) [0 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&resolved=false&types=CODE_SMELL)
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/100.png' alt='100.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&metric=new_coverage&view=list) [100.0% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&metric=new_coverage&view=list)
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-scripting-sightly-runtime&pullRequest=6&metric=new_duplicated_lines_density&view=list)
<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/message_warning.png' alt='warning' width='16' height='16' /> The version of Java (1.8.0_252) you have used to run this analysis is deprecated and we will stop accepting it from October 2020. Please update to at least Java 11.
Read more [here](https://sonarcloud.io/documentation/upcoming/)
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org