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

additionalProperties is tested and implemented incorrectly #321

Closed
dpatti opened this issue Apr 4, 2016 · 3 comments
Closed

additionalProperties is tested and implemented incorrectly #321

dpatti opened this issue Apr 4, 2016 · 3 comments

Comments

@dpatti
Copy link
Contributor

dpatti commented Apr 4, 2016

This schema is being used by these tests. This schema doesn't make sense to me. My understanding is that additionalProperties is supposed to be a boolean value false or a schema describing what each additional property's value should validate against.

The reason these tests are concerning is because they fail when I tried to fix the following bit in AdditionalPropertiesAttribute, which reads:

JSON::Schema.new(addprop[key] || addprop, current_schema.uri, validator)

What I believe this line should do is instantiate a new schema using the value of the additionalProperties object, but we're trying to access [key] on it. This demonstrates why this is a problem:

schema = {
  properties: {
    foo: {
      type: "string",
    }
  },
  additionalProperties: {
    type: "number",
  }
}

p JSON::Validator.validate(schema, { foo: "one", two: 2, three: 3 }) # => true
p JSON::Validator.validate(schema, { foo: "one", two: 2, three: 3, type: 4 }) # => NoMethodError

With backtrace:

/opt/rubies/2.2.4/lib/ruby/gems/2.2.0/gems/json-schema-2.5.0/lib/json-schema/schema/validator.rb:22:in `validate': undefined method `each' for "number":String (NoMethodError)
  from /opt/rubies/2.2.4/lib/ruby/gems/2.2.0/gems/json-schema-2.5.0/lib/json-schema/schema.rb:33:in `validate'
  from /opt/rubies/2.2.4/lib/ruby/gems/2.2.0/gems/json-schema-2.5.0/lib/json-schema/attributes/additionalproperties.rb:18:in `block in validate'
  from /opt/rubies/2.2.4/lib/ruby/gems/2.2.0/gems/json-schema-2.5.0/lib/json-schema/attributes/additionalproperties.rb:16:in `each'

I want to say this is wrong, but I'm not exactly sure how I should go about fixing something like this. It seems like it would change behavior if people relied on it working the way it is tested. Here's the commit I blamed this back to: 7fba5303

dpatti added a commit to Genius/json-schema that referenced this issue Apr 14, 2016
@iainbeeston
Copy link
Contributor

I've started looking at this. The issue occurs whenever you have a property name in the data that matches a property defined on your additionalProperties object (in this case having a property with the name type in both additionalProperties and in your json data is causing the issue). I haven't got a fix yet but I'm working on it

@dpatti
Copy link
Contributor Author

dpatti commented Apr 15, 2016

@iainbeeston Unless I'm mistaken, you should never be defining properties on additionalProperties. It's either a boolean or a schema. https://github.com/ruby-json-schema/json-schema/blob/master/resources/draft-04.json#L93-L99

@iainbeeston
Copy link
Contributor

Hi @dpatti I believe I've fixed this in #360 and released the fix in json-schema 2.7.0. Using that version of the gem, the example above works without errors.

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

2 participants