Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion rust/pact_ffi/src/mock_server/bodies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ fn process_matcher(
let json_value = match obj.get("value") {
Some(inner) => match inner {
Value::Object(ref map) => process_object(map, matching_rules, generators, path.clone(), has_values_matcher),
Value::Array(ref array) => process_array(array, matching_rules, generators, path.clone(), true, skip_matchers),
Value::Array(ref array) => pad_to_min_length(
process_array(array, matching_rules, generators, path.clone(), true, skip_matchers), &rules),
_ => inner.clone()
},
None => Value::Null
Expand Down Expand Up @@ -178,6 +179,24 @@ fn process_matcher(
}
}

// An example array with fewer items than a min-type matcher requires (e.g. one example item
// with `min: 2`) would not match its own rule, so repeat the last item up to the minimum, as the
// Ruby and JVM DSLs do.
fn pad_to_min_length(value: Value, rules: &[MatchingRule]) -> Value {
let min = rules.iter().filter_map(|rule| match rule {
MatchingRule::MinType(min) | MatchingRule::MinMaxType(min, _) => Some(*min),
_ => None
}).max();
match (value, min) {
(Value::Array(mut items), Some(min)) if !items.is_empty() && items.len() < min => {
let last = items[items.len() - 1].clone();
items.resize(min, last);
Value::Array(items)
}
(value, _) => value
}
}

// The rules given to an each-value matcher apply to every value in the collection, but only the
// rule itself is stored in its definition, so any matchers nested in a rule's example would be
// lost. Add them as rules for the values (at `path.*`), the same way the matchers nested in the
Expand Down Expand Up @@ -497,6 +516,36 @@ use pretty_assertions::assert_eq;
expect!(result).to(be_equal_to(json));
}

// pact-net#522
#[test]
fn process_object_pads_a_min_type_array_example_to_the_minimum() {
let json = json!({
"items": { "pact:matcher:type": "type", "min": 2, "value": [ { "id": { "pact:matcher:type": "integer", "value": 1 } } ] },
"range": { "pact:matcher:type": "type", "min": 3, "max": 5, "value": [ "a", "b" ] },
"enough": { "pact:matcher:type": "type", "min": 1, "value": [ "a", "b" ] },
"empty": { "pact:matcher:type": "type", "min": 2, "value": [] }
});
let mut matching_rules = MatchingRuleCategory::default();
let mut generators = Generators::default();
let result = process_object(json.as_object().unwrap(), &mut matching_rules,
&mut generators, DocPath::root(), false);

expect!(result).to(be_equal_to(json!({
"items": [ { "id": 1 }, { "id": 1 } ],
"range": [ "a", "b", "b" ],
"enough": [ "a", "b" ],
"empty": []
})));
expect!(matching_rules.to_v3_json().to_string()).to(be_equal_to(matchingrules_list!{
"body";
"$.items" => [ MatchingRule::MinType(2) ],
"$.items[*].id" => [ MatchingRule::Integer ],
"$.range" => [ MatchingRule::MinMaxType(3, 5) ],
"$.enough" => [ MatchingRule::MinType(1) ],
"$.empty" => [ MatchingRule::MinType(2) ]
}.to_v3_json().to_string()));
}

#[test]
fn process_object_with_matching_rule_test() {
let json = json!({
Expand Down
Loading