voxpupuli / json-schema

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

Cached schema from file with :list => true fails after second call to validate #440

Open wasaylor opened 4 years ago

wasaylor commented 4 years ago

Hi,

Experiencing a strange issue with cached schema from file paths. If I have the following block of code:

json = '[{ "foo": "bar" }]'
schema = "schema.json"
options = { :json => true, :list => true }

and the schema.json file contains:

{
  "type": "object",
  "properties": {
    "foo": {
      "type": "string"
    }
  },
  "required": ["foo"]
}

calling JSON::Validator.validate!(schema, json, options) succeeds the first time and JSON::Validator.schemas.keys is:

["file:///redacted/schema.json#"]

calling JSON::Validator.validate!(schema, json, options) a second time raises an error:

JSON::Schema::ValidationError: The property '#/0' of type object did not match the following type: array in schema 7cb19a09-a7b6-55db-9163-2d1595d4f3e4

JSON::Validator.schemas.keys is:

["file:///redacted/schema.json#", "file:///redacted/7cb19a09-a7b6-55db-9163-2d1595d4f3e4#"]

If I look at the two cached schemas in memory schema.json is:

{"type"=>"array", "items"=>{"type"=>"object", "properties"=>{"foo"=>{"type"=>"string"}}, "required"=>["foo"]}}

7cb19a09-a7b6-55db-9163-2d1595d4f3e4 is:

{"type"=>"array", "items"=>{"type"=>"array", "items"=>{"type"=>"object", "properties"=>{"foo"=>{"type"=>"string"}}, "required"=>["foo"]}}}

So it looks like passing :list => true on the second call caches the schema again, making it a list of the already cached list version of the original schema.json file..

For now I'm using :clear_cache => true in the options to prevent this from happening.

(json-schema 2.8.1 on ruby 1.9.3)