diff --git a/NEXT_CHANGELOG.md b/NEXT_CHANGELOG.md index c86d9ac0e8..1508b75baa 100644 --- a/NEXT_CHANGELOG.md +++ b/NEXT_CHANGELOG.md @@ -186,7 +186,7 @@ From the CLI users can run: There are situations where comments and whitespace are not preserved. This may be improved in future. -By [@bryncooke](https://github.com/bryncooke) in https://github.com/apollographql/router/pull/2116 +By [@bryncooke](https://github.com/bryncooke) in https://github.com/apollographql/router/pull/2116, https://github.com/apollographql/router/pull/2162 ### *Experimental* subgraph request retry ([Issue #338](https://github.com/apollographql/router/issues/338), [Issue #1956](https://github.com/apollographql/router/issues/1956)) diff --git a/apollo-router/src/configuration/snapshots/apollo_router__configuration__upgrade__test__move_non_existent_field.snap b/apollo-router/src/configuration/snapshots/apollo_router__configuration__upgrade__test__move_non_existent_field.snap new file mode 100644 index 0000000000..58f3973636 --- /dev/null +++ b/apollo-router/src/configuration/snapshots/apollo_router__configuration__upgrade__test__move_non_existent_field.snap @@ -0,0 +1,7 @@ +--- +source: apollo-router/src/configuration/upgrade.rs +expression: "apply_migration(&json!({ \"should\" : \"stay\" }),\n &Migration::builder().action(Action::Move {\n from: \"obj.field1\".to_string(),\n to: \"new.obj.field1\".to_string(),\n }).description(\"move field1\").build()).expect(\"expected successful migration\")" +--- +{ + "should": "stay" +} diff --git a/apollo-router/src/configuration/upgrade.rs b/apollo-router/src/configuration/upgrade.rs index 9ed15c5ea8..43c8ff40ab 100644 --- a/apollo-router/src/configuration/upgrade.rs +++ b/apollo-router/src/configuration/upgrade.rs @@ -70,22 +70,37 @@ fn apply_migration(config: &Value, migration: &Migration) -> Result { - // Deleting isn't actually supported by protus so we add a magic value to delete later - transformer_builder = transformer_builder.add_action( - Parser::parse(REMOVAL_EXPRESSION, path).expect("migration must be valid"), - ); + if !jsonpath_lib::select(config, &format!("$.{}", path)) + .unwrap_or_default() + .is_empty() + { + // Deleting isn't actually supported by protus so we add a magic value to delete later + transformer_builder = transformer_builder.add_action( + Parser::parse(REMOVAL_EXPRESSION, path).expect("migration must be valid"), + ); + } } Action::Copy { from, to } => { - transformer_builder = transformer_builder - .add_action(Parser::parse(from, to).expect("migration must be valid")); + if !jsonpath_lib::select(config, &format!("$.{}", from)) + .unwrap_or_default() + .is_empty() + { + transformer_builder = transformer_builder + .add_action(Parser::parse(from, to).expect("migration must be valid")); + } } Action::Move { from, to } => { - transformer_builder = transformer_builder - .add_action(Parser::parse(from, to).expect("migration must be valid")); - // Deleting isn't actually supported by protus so we add a magic value to delete later - transformer_builder = transformer_builder.add_action( - Parser::parse(REMOVAL_EXPRESSION, from).expect("migration must be valid"), - ); + if !jsonpath_lib::select(config, &format!("$.{}", from)) + .unwrap_or_default() + .is_empty() + { + transformer_builder = transformer_builder + .add_action(Parser::parse(from, to).expect("migration must be valid")); + // Deleting isn't actually supported by protus so we add a magic value to delete later + transformer_builder = transformer_builder.add_action( + Parser::parse(REMOVAL_EXPRESSION, from).expect("migration must be valid"), + ); + } } } } @@ -258,6 +273,21 @@ mod test { .expect("expected successful migration")); } + #[test] + fn move_non_existent_field() { + insta::assert_json_snapshot!(apply_migration( + &json!({"should": "stay"}), + &Migration::builder() + .action(Action::Move { + from: "obj.field1".to_string(), + to: "new.obj.field1".to_string() + }) + .description("move field1") + .build(), + ) + .expect("expected successful migration")); + } + #[test] fn move_array_element() { insta::assert_json_snapshot!(apply_migration(