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

Passing in a fragment with an even number of tokens can lead to undefined method `validate' for #<Hash...> #265

Closed
ckundo opened this issue Sep 15, 2015 · 10 comments

Comments

@ckundo
Copy link

ckundo commented Sep 15, 2015

We're seeing an exception when validating a json response from a Rails controller. It seems to fail on every other token in the JSON fragment path:

schema_options = {
  fragment: "#/schemas/v1.reports.index.response/purchases",
  strict: true
}
data = JSON.parse(response.body)

JSON::Validator.fully_validate(
  "schema.json",
  data,
  schema_options,
)

Raises:

# => ...undefined method `validate' for #<Hash...>

But for example, this fragment does not raise the exception:

"#/schemas/v1.reports.index.response/purchases/items"

We have monkey-patched this as follows:

module JSON
  class Validator
    def schema_from_fragment(base_schema, fragment)
      # ...

      fragments.each do |f|
        if base_schema.is_a?(JSON::Schema) #test if fragment is a JSON:Schema instance
          if !base_schema.schema.has_key?(f)
            raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
          end

          # HERE:
          if base_schema.schema[f].is_a? Hash
            base_schema = JSON::Schema.new(base_schema.schema[f], schema_uri, @options[:version])
          else
            base_schema = base_schema.schema[f]
          end
        elsif base_schema.is_a?(Hash)
          # ...

That is, this line https://github.com/ruby-json-schema/json-schema/blob/master/lib/json-schema/validator.rb#L91 should always create a new JSON::Schema.

@rickreilly
Copy link

+1

@RST-J
Copy link
Contributor

RST-J commented Oct 1, 2015

Would you please provide a test case, example schema and data which fails (I don't doubt your bug report)?

@jrgns
Copy link

jrgns commented Jan 16, 2016

I'm seeing the same issue, but it's not due to the number of tokens. It happens when the second last token points to a proper schema, and the last token points to a Hash.

The bug exists because the resulting base_schema get's converted into a JSON::Schema in all the if clauses except the first one: https://github.com/jrgns/json-schema/blob/master/lib/json-schema/validator.rb#L89

@jrgns
Copy link

jrgns commented Jan 16, 2016

This might be by design, though, since you can use the fragment to point to something that is not a schema like a property or a required array.

The reason I ran across it is that I'm trying to validate both the input and the output of an API call using the hyper-schema:

JSON::Validator.validate!('schema.json', input, strict: true, fragment: '#/definitions/users/links/0/schema')
JSON::Validator.validate!('schema.json', output, strict: true, fragment: '#/definitions/users/links/0/targetSchema')

My schema looks like the ones described here

@jrgns
Copy link

jrgns commented Jan 16, 2016

Probably also related to #217

@jrgns
Copy link

jrgns commented Jan 16, 2016

PR #217 will fix this issue.

@aledalgrande
Copy link

aledalgrande commented May 21, 2016

Just got into this today. Getting same error when an array is used in the fragment (which is every test if you follow a prmd schema style).

+1

@iainbeeston
Copy link
Contributor

I'll try to sort this out once and for all tomorrow...

@iainbeeston
Copy link
Contributor

Just a quick update on this. At the moment we're maintaining two different code paths for resolving fragments: one for the :fragment option and one for $ref properties. I'm investigating whether I can refactor so that the :fragment option reuses the code for handling $ref properties (which is more tested and I'd argue more robust). Unfortunately it's more work than just fixing :fragment but I believe it's the right choice for the long-term

iainbeeston pushed a commit to iainbeeston/json-schema that referenced this issue Jul 5, 2017
iainbeeston pushed a commit to iainbeeston/json-schema that referenced this issue Jul 5, 2017
@iainbeeston
Copy link
Contributor

This should be fixed now, I'll try to release shortly, once I have a few more bugfixes merged as well

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

6 participants