Skip to content

Commit

Permalink
fix: regression - generators must support query parameters in q[]= form
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jan 19, 2024
1 parent 0190399 commit 1d9034a
Showing 1 changed file with 123 additions and 6 deletions.
129 changes: 123 additions & 6 deletions rust/pact_models/src/generators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,15 +527,20 @@ impl Generators {
) -> anyhow::Result<()> {
for (k, v) in map {
match v {
&Value::Object(ref map) => match GeneratorCategory::from_str(k) {
Ok(ref category) => match category {
&GeneratorCategory::PATH | &GeneratorCategory::METHOD | &GeneratorCategory::STATUS => {
Value::Object(map) => match &GeneratorCategory::from_str(k) {
Ok(category) => match category {
GeneratorCategory::PATH | GeneratorCategory::METHOD | GeneratorCategory::STATUS => {
self.parse_generator_from_map(category, map, None);
},
_ => for (sub_k, sub_v) in map {
match sub_v {
&Value::Object(ref map) =>
self.parse_generator_from_map(category, map, Some(DocPath::new(sub_k)?)),
&Value::Object(ref map) => {
if *category == GeneratorCategory::QUERY || *category == GeneratorCategory::HEADER {
self.parse_generator_from_map(category, map, Some(DocPath::root().push_field(sub_k).clone()));
} else {
self.parse_generator_from_map(category, map, Some(DocPath::new(sub_k)?));
}
},
_ => warn!("Ignoring invalid generator JSON '{}' -> {:?}", sub_k, sub_v)
}
}
Expand Down Expand Up @@ -1590,9 +1595,81 @@ mod tests {
}

#[test]
fn matchers_from_json_test() {
fn generators_from_json_test() {
let generators = generators_from_json(&Value::Null);
expect!(generators.unwrap().categories.iter()).to(be_empty());

let generators = generators_from_json(&json!({}));
expect!(generators.unwrap().categories.iter()).to(be_empty());

let generators_json = json!({
"generators": {
"path": {
"type": "RandomBoolean"
},
"query": {
"Q1": {
"type": "RandomBoolean"
}
},
"header": {
"Y": {
"type": "RandomBoolean"
}
},
"body": {
"$.animals": {
"type": "RandomBoolean"
},
"$.animals[*].children": {
"type": "RandomBoolean"
}
}
}
});
let generators = generators_from_json(&generators_json).unwrap();
expect!(generators).to(be_equal_to(generators!{
"PATH" => {
"" => Generator::RandomBoolean
},
"QUERY" => {
"$.Q1" => Generator::RandomBoolean
},
"HEADER" => {
"$.Y" => Generator::RandomBoolean
},
"BODY" => {
"$.animals" => Generator::RandomBoolean,
"$.animals[*].children" => Generator::RandomBoolean
}
}));
}

#[test]
fn generators_from_json_supports_query_parameters_with_brackets() {
let generators_json = json!({
"generators": {
"query": {
"Q[]": {
"type": "RandomBoolean"
}
},
"header": {
"Y[]": {
"type": "RandomBoolean"
}
}
}
});
let generators = generators_from_json(&generators_json).unwrap();
expect!(generators).to(be_equal_to(generators!{
"QUERY" => {
"$['Q[]']" => Generator::RandomBoolean
},
"HEADER" => {
"$['Y[]']" => Generator::RandomBoolean
}
}));
}

#[test]
Expand Down Expand Up @@ -1703,6 +1780,46 @@ mod tests {
be_some().value(Generator::ProviderStateGenerator("5".into(), Some(DataType::INTEGER))));
}

#[test]
fn load_from_map_test() {
let mut generators = Generators::default();
let json = json!({});
let json_map = json.as_object().unwrap();
expect!(generators.load_from_map(json_map)).to(be_ok());
expect!(generators.is_empty()).to(be_true());

let mut generators = Generators::default();
let json = json!({ "invalid": {} });
let json_map = json.as_object().unwrap();
expect!(generators.load_from_map(json_map)).to(be_ok());
expect!(generators.is_empty()).to(be_true());

let mut generators = Generators::default();
let json = json!({ "path": []});
let json_map = json.as_object().unwrap();
expect!(generators.load_from_map(json_map)).to(be_ok());
expect!(generators.is_empty()).to(be_true());

let mut generators = Generators::default();
let json = json!({
"path": { "type": "RandomBoolean" },
"query": {
"q": { "type": "RandomBoolean" }
}
});
let json_map = json.as_object().unwrap();
expect!(generators.load_from_map(json_map)).to(be_ok());
expect!(generators.is_empty()).to(be_false());
expect!(generators).to(be_equal_to(generators!{
"path" => {
"" => Generator::RandomBoolean
},
"query" => {
"$.q" => Generator::RandomBoolean
}
}));
}

#[test]
fn generator_to_json_test() {
expect!(Generator::RandomInt(5, 15).to_json().unwrap()).to(be_equal_to(json!({
Expand Down

0 comments on commit 1d9034a

Please sign in to comment.