voxpupuli / json-schema

Ruby JSON Schema Validator
MIT License
1.52k stars 242 forks source link

JSON::Validator.fully_validate(filename, data) fails when trying to parse filename as JSON #366

Closed faucct closed 7 years ago

faucct commented 7 years ago

Hello, I have noticed that this code fails with JSON::ParserError though it should have fallen back to reading from file:

require 'json'
require 'json-schema'
require 'oj_mimic_json'

JSON::Validator.fully_validate('test.json', {})

I have dug deeper and found that the explanation of that. When I require your gem, it requires multi_json and makes it use its default adapter. This makes it cache JSON::ParserError here. After that I require oj_mimic_json which overrides JSON::ParserError here. Now when JSON.parse throws an error, MultiJSON.load fails to wrap it inside MultiJSON::ParseError and as a result you fail to rescue it here and don't even try to read the file. I am not sure which gem is most guilty here, so I will mention this issue there too. The problem can be reproduced without your gem:

require 'json'
require 'multi_json'
MultiJson.use nil
require 'oj_mimic_json'

# Throws JSON::ParserError instead of MultiJSON::ParseError
MultiJson.load('test.json')
RST-J commented 7 years ago

We know that there is a problem with mulit-json. There is a PR ( #339 ) which will remove multi-json. Can you uninstall multi-json? If multi-json is not installed, it should work. If you cannot uninstall multi-json because another project needs it, then use a custom gemset. This of course won't help if you need multi-json in the very same project.

faucct commented 7 years ago

Actually my PR https://github.com/ohler55/oj/pull/320 in oj was accepted, so it makes the issue fixed. Closing.