jhthorsen / json-validator

:cop: Validate data against a JSON schema
https://metacpan.org/release/JSON-Validator
56 stars 58 forks source link

t/jv-boolean.t throws an exception #149

Closed karenetheridge closed 5 years ago

karenetheridge commented 5 years ago
: [ether@aquavit git/JSON-Validator]$; perl -Ilib -d:Confess t/jv-boolean.t 
ok 1 - valid: {"nick":true}
ok 2 - errors: /nick: Expected boolean - got number.
ok 3 - errors: /nick: Expected boolean - got number.
ok 4 - errors: /nick: Expected boolean - got string.
Attempt to free unreferenced scalar: SV 0x7f80cbc3ab48 at t/Helper.pm line 35.
    main::validate_ok(HASH(0x7f80cb8033e0), HASH(0x7f80cb803500)) called at t/jv-boolean.t line 15
ok 5 - valid: {"nick":null}
ok 6 - errors: /nick: Expected boolean - got BoolTestFail.
ok 7 - valid: false
ok 8 - valid: true
ok 9 - errors: /: Expected boolean - got string.
ok 10 - errors: /: Expected object - got null.
ok 11 - valid: {"nick":1000}
ok 12 - valid: {"nick":0.5}
1..12

The test "passes", but clearly has an issue in here somewhere. I am guessing an eval{} is swallowing it?

jhthorsen commented 5 years ago

I’m pretty sure this is a warning?

I’m not to interested into figuring it out, since it seems like a special case in the test.

https://medium.com/booking-com-development/the-dreaded-attempt-to-free-unreferenced-scalar-b71ce968e204

I don’t mind a fix though :) (And I will look into it, if it affects production code)

karenetheridge commented 5 years ago

I think it's an exception, that is just turning into a warning because of an eval{} somewhere, but I don't know what is causing it. I see it all the time in Mojo code; there's something about how Mojolicious constructs things that seems to tickle this somehow. I will look into it more on Monday!

perlpunk commented 5 years ago

Hi,

I happen to have a Mac available, so I just tried it out and reduced it to the following snippet, showing that Cpanel::JSON::XS is the cause here:

use Cpanel::JSON::XS ();
my $coder = Cpanel::JSON::XS->new;
$coder->allow_blessed->convert_blessed;
my $data = {nick => bless({}, 'BoolTestOk')};
my $json = $coder->encode($data);

package BoolTestOk;
use overload '""' => sub {1};

Happens with Cpanel::JSON::XS 4.08 and 4.09, haven't tried other versions so far.

Taking out the overload makes it go away.

perlpunk commented 5 years ago

I created https://github.com/rurban/Cpanel-JSON-XS/issues/124

jhthorsen commented 5 years ago

That’s awesome! Thank you @perlpunk 👍

perlpunk commented 5 years ago

@karenetheridge would be interesting though in which other situations you see this. This test case seems to be a rather unusual thing.

karenetheridge commented 5 years ago

nice sleuthing!!

karenetheridge commented 5 years ago

Upgrading to Cpanel::JSON::XS 4.10 fixes this issue.