Class: JSONSchemer::Fuzz::Keyword::OneOf

Inherits:
Object
  • Object
show all
Defined in:
lib/json_schemer/fuzz/keyword/one_of.rb

Class Method Summary collapse

Class Method Details

.invalid_params(attributes) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/json_schemer/fuzz/keyword/one_of.rb', line 6

def invalid_params(attributes)
  one_of = attributes["oneOf"]
  raise "No oneOf keyword given: #{attributes}" unless one_of
  raise "oneOf keyword given, but had no schemas: #{attributes["oneOf"]}" unless one_of.any?

  generated_params = []

  # Either all params need to be invalid, or all need to be valid.
  # For simplicity, we'll start with all invalid,
  #   but this could be extended to sometimes return all valid.
  one_of.each do |schema|
    temp_params = JSONSchemer::Fuzz.generate(schema).reject do |param|
      ::JSONSchemer.schema(attributes).validate(param)
    end
    generated_params.concat(temp_params)
  end

  raise "failed to generate invalid_params for schema: #{attributes}" if generated_params.empty?
  [generated_params.uniq.sample]
end

.valid_param(attributes) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/json_schemer/fuzz/keyword/one_of.rb', line 27

def valid_param(attributes)
  one_of = attributes["oneOf"]
  raise "No oneOf keyword given: #{attributes}" unless one_of
  raise "oneOf keyword given, but had no schemas: #{attributes["oneOf"]}" unless one_of.any?

  generated_params = []

  # Only one of the oneOf can be valid, the others must not be, as it is an XOR
  # If one of the oneOf's is required,
  #   then then match for that schema cannot also match the other schemas.
  required_schema_index = one_of.index { |oof| oof["required"] }
  required_schema = one_of.delete_at(required_schema_index)
  if required_schema
    valid_required_param = JSONSchemer::Fuzz.default_param(required_schema).detect do |param|
      ::JSONSchemer.schema(attributes).validate(param)
    end
    if valid_required_param
      generated_params << valid_required_param
    else
      one_of.each do |schema|
        temp_param = JSONSchemer::Fuzz.generate(schema).detect do |param|
          ::JSONSchemer.schema(attributes).validate(param)
        end
        if temp_param
          generated_params << temp_param
        else
          # Allow to drop through to attempt to find another part of the oneOf to satisfy
        end
      end
    end
  end

  return generated_params.uniq.sample if generated_params.any?

  one_of.each do |schema|
    generated_params.concat(JSONSchemer::Fuzz.generate(schema).select do |param|
      ::JSONSchemer.schema(attributes).validate(param)
    end)
  end

  raise "failed to generate invalid_params for schema: #{attributes}" if generated_params.empty?
  generated_params.uniq.sample
end