Skip to content
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

Schema validation should not clear cache #82

Closed
windlass opened this issue Oct 5, 2013 · 3 comments
Closed

Schema validation should not clear cache #82

windlass opened this issue Oct 5, 2013 · 3 comments

Comments

@windlass
Copy link

windlass commented Oct 5, 2013

It looks like $ref schemas are not useable when schema validation is enabled. The following schema is valid, but is only useable to validate documents if schema validation is disabled:

{ 
  "type": "object"
  "definitions": {
    "test": {
      "type": "string"
    }
  },
  "properties": {
    "foo": { "$ref": "#/definitions/test" }
  }
}

The problem seems to be that when validated with :validate_schema, the schema is first validated. Then, the cache is cleared, which has a side-effect of removing the schema for #/definitions/test as well has the schemas for schema itself. Then, when you validate a document against it the $ref fails to resolve.

One easy workaround for this (but not necessarily the right fix) would be to turn schema caching on before validating:

JSON::Validator.cache_schemas = true

This causes the cache to be retained between validating the schema and the data, so the $ref URI resolves. But since this is probably the desired behavior whenever doing a schema validation plus a validation against the schema, it's probably better to implicitly set @@cache_schemas = true when schema validation is requested.

@windlass
Copy link
Author

windlass commented Oct 5, 2013

It occurs to me that another approach may be to retain the current caching behavior, but to selectively purge the cache of the "schema schemas"; i.e. the ones used to validate the schema itself, after validating the schema.

@dkowis
Copy link

dkowis commented Oct 23, 2013

More interestingly, this doesn't happen all the time.

The full schema from http://json-schema.org/example2.html doesn't fail the $ref resolving problem.

@hoxworth
Copy link
Contributor

hoxworth commented Jan 5, 2014

This should be fixed in 2.1.9. Clearing of the schema cache will become a validation option, which will explicitly be set to false for schema validation. The old cache_schemas= method will be deprecated and removed in a future version.

@hoxworth hoxworth closed this as completed Jan 5, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants