Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 9 additions & 4 deletions crates/engine/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,8 +820,9 @@ struct ImportItem<T> {
extra: HashMap<String, Value>,
}

/// Check declared versions and model contents before import can discard data.
/// Missing versions are legacy files. The stricter field checks apply only to file import.
/// Check declared versions, required capabilities (`requires`, as a project's; see
/// [`crate::project::CAPABILITIES`]) and model contents before import can discard data. Missing
/// versions are legacy files. The stricter field checks apply only to file import.
pub(crate) fn check_file(v: &Value) -> Result<(), String> {
let graph = v.get("graph");
// A model by its schema, a web tool by its format, even when a newer RustGIS changed what it
Expand All @@ -838,14 +839,18 @@ pub(crate) fn check_file(v: &Value) -> Result<(), String> {
return Err("this model or web tool has an unsupported version. Update RustGIS to import it.".into());
}
}
crate::project::check_requires(v.get("requires"), "model or web tool", "import")?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latent round-trip gap (confidence: medium, not reachable today). check_file accepts a known capability in requires, but nothing carries that list past import. model.import passes only name and graph/steps to model.new (cmd/sharing.rs:181-185), Model has no field for it, and the writer at model.rs:970 emits only $schema, version, name, graph. So once a capability is added to CAPABILITIES, a file that requires it imports, and the next save or export silently drops the requirement. That is the loss kept_as_written guards against for projects.

It does nothing yet because CAPABILITIES is empty, so any non-empty list is refused. Before the first capability is added, either keep requires on the imported model, or refuse a known capability that the model can't keep. A test with a stub known capability would pin this down.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changed. This matches projects since #1975: a format 2 project that requires only known capabilities opens and saves again as format 1 without requires. CAPABILITIES is the list this RustGIS reads and saves again without loss. So when a capability is added, that same change makes the writer emit it whenever the saved model uses the feature, and a model that doesn't use it needs nothing listed. Keeping an imported list wouldn't help, since it would go stale as soon as the model is edited. The pairing is said where CAPABILITIES is documented (docs/maintaining.md › Saved files and settings: "added to CAPABILITIES where it is read").

if graph.is_none() && v.get("steps").is_none() {
return Err("not a RustGIS model or web tool (no graph or steps)".into());
}
if graph.is_some() && v.get("steps").is_some() {
return Err("a model file cannot contain both a graph and steps. Choose one representation before importing it.".into());
}
let fields: &[&str] =
if graph.is_some() { &["$schema", "version", "name", "graph"] } else { &["format", "version", "name", "steps", "parameters"] };
let fields: &[&str] = if graph.is_some() {
&["$schema", "version", "requires", "name", "graph"]
} else {
&["format", "version", "requires", "name", "steps", "parameters"]
};
if v.as_object().is_some_and(|file| file.keys().any(|key| !fields.contains(&key.as_str()))) {
return Err("model file has unsupported fields. Update RustGIS to import this model.".into());
}
Expand Down
28 changes: 22 additions & 6 deletions crates/engine/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,19 +913,19 @@ impl<'de> serde::de::Visitor<'de> for RequiresVisitor {
}

/// The capabilities a document requires that this RustGIS doesn't know, each once, or why its
/// `requires` can't be read.
fn unknown_capabilities(requires: &Requires, kind: &str) -> Result<Vec<String>, String> {
/// `requires` can't be read (`open`: what that refusal says to update RustGIS for).
fn unknown_capabilities(requires: &Requires, kind: &str, open: &str) -> Result<Vec<String>, String> {
let names = match requires {
Requires::Nothing => return Ok(Vec::new()),
Requires::Names(names) => names,
Requires::TooMany => {
return Err(format!(
"this {kind} lists more than {MAX_REQUIRES} things it needs (`requires`), more than this RustGIS reads. It may be damaged, or from a newer RustGIS: update RustGIS to open it."
"this {kind} lists more than {MAX_REQUIRES} things it needs (`requires`), more than this RustGIS reads. It may be damaged, or from a newer RustGIS: update RustGIS to {open} it."
));
}
Requires::NotNames => {
return Err(format!(
"this {kind} lists what it needs (`requires`) as something other than names, so this RustGIS can't tell whether it reads it. It may be damaged, or from a newer RustGIS: update RustGIS to open it."
"this {kind} lists what it needs (`requires`) as something other than names, so this RustGIS can't tell whether it reads it. It may be damaged, or from a newer RustGIS: update RustGIS to {open} it."
));
}
};
Expand Down Expand Up @@ -982,15 +982,31 @@ pub(crate) fn check_format(text: &str, kind: &str) -> Result<(), String> {
if header.format > u64::from(READ_FORMAT) {
return Err(format!("this {kind} was saved by a newer RustGIS (format {}). Update RustGIS to open it.", header.format));
}
let unknown = unknown_capabilities(&header.requires, kind)?;
requires_known(&header.requires, kind, "open")
}

/// Refuse a document whose `requires` (`None` when it has none) lists a capability this RustGIS
/// doesn't know, naming them, as [`check_format`] does: for files read as JSON values, such as
/// models and web tools. `open` is what the refusal says to update RustGIS for ("import").
pub(crate) fn check_requires(requires: Option<&serde_json::Value>, kind: &str, open: &str) -> Result<(), String> {
let requires = match requires {
None => Requires::Nothing,
Some(v) => Requires::deserialize(v).map_err(|e| format!("not a RustGIS {kind}: {e}"))?,
};
requires_known(&requires, kind, open)
}

/// Refuse what `requires` lists that this RustGIS doesn't know, each name said once.
fn requires_known(requires: &Requires, kind: &str, open: &str) -> Result<(), String> {
let unknown = unknown_capabilities(requires, kind, open)?;
if !unknown.is_empty() {
let mut said: Vec<String> = Vec::new();
for name in unknown.iter().map(|n| capability_said(n)) {
if !said.contains(&name) {
said.push(name);
}
}
return Err(format!("this {kind} needs what this RustGIS can't read: {}. Update RustGIS to open it.", crate::names_said(&said)));
return Err(format!("this {kind} needs what this RustGIS can't read: {}. Update RustGIS to {open} it.", crate::names_said(&said)));
}
Ok(())
}
Expand Down
27 changes: 22 additions & 5 deletions crates/engine/tests/workflows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ fn model_import_refuses_unsupported_files_without_changing_session() {
(json!({"version": 2, "steps": "unreadable future steps"}), "unsupported version"),
(json!({"version": null, "graph": {"nodes": []}}), "unsupported version"),
(json!({"version": "1", "steps": []}), "unsupported version"),
(
json!({"version": "1.0.0", "requires": ["futureLoops"], "graph": "unreadable future graph"}),
"can't read: futureLoops. Update RustGIS to import it",
),
(json!({"version": 1, "requires": ["futureSteps", "futureSteps"], "steps": "unreadable"}), "can't read: futureSteps. Update"),
(
json!({"requires": "futureLoops", "steps": []}),
"other than names, so this RustGIS can't tell whether it reads it. It may be damaged, or from a newer RustGIS: update RustGIS to import it",
),
(json!({"graph": {"nodes": []}, "description": "Keep this"}), "file has unsupported fields"),
(json!({"steps": [], "future": true}), "file has unsupported fields"),
(json!({"graph": {"nodes": []}, "steps": [{"tool": "buffer", "params": {}}]}), "both a graph and steps"),
Expand Down Expand Up @@ -140,7 +149,11 @@ fn model_import_refuses_unsupported_files_without_changing_session() {
{
let error = s.run("model.import", &params).unwrap_err();
assert!(error.contains(message), "{error}");
if message == "unsupported version" || message.starts_with("node ") || message.contains("unsupported fields") {
if message == "unsupported version"
|| message.starts_with("node ")
|| message.contains("unsupported fields")
|| message.contains("can't read")
{
assert!(error.contains("Update RustGIS"), "{error}");
}
assert_eq!(serde_json::to_value(&s.project).unwrap(), before);
Expand All @@ -152,7 +165,8 @@ fn model_import_refuses_unsupported_files_without_changing_session() {
}
}

/// Actual graph and web-tool writers still round-trip, with or without their legacy version marker.
/// Actual graph and web-tool writers still round-trip, with or without their legacy version marker,
/// and with a list of required capabilities this version has.
#[test]
fn model_import_preserves_supported_and_legacy_files() {
use base64::Engine;
Expand Down Expand Up @@ -190,10 +204,13 @@ fn model_import_preserves_supported_and_legacy_files() {
{
let file: Value = serde_json::from_str(&text).unwrap();
assert_eq!(file["version"], if index == 0 { json!("1.0.0") } else { json!(1) });
for legacy in [false, true] {
// As written, without the legacy version marker, and requiring nothing this version lacks.
for variant in ["current", "legacy", "requires"] {
let mut file = file.clone();
if legacy {
file.as_object_mut().unwrap().remove("version");
match variant {
"legacy" => drop(file.as_object_mut().unwrap().remove("version")),
"requires" => file["requires"] = json!([]),
_ => {}
}
let text = file.to_string();
let path = scratch.path(if index == 0 { "graph.model.json" } else { "steps.rgistool" });
Expand Down
4 changes: 2 additions & 2 deletions docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ When a future change would cause an older reader to misinterpret or lose meaning
information on resaving, name it in the document's `requires` and add the name to `CAPABILITIES`
where it is read; a format beyond `READ_FORMAT` is for a change a capability can't describe. This
includes additive fields that parse successfully but disappear when an older writer saves. Task,
presentation, notebook and model documents don't check `requires` yet: a change to them that an
older reader would lose needs that check first (see
presentation and notebook documents, and models inside a task's steps, don't check `requires` yet:
a change to them that an older reader would lose needs that check first (see
[maintaining](maintaining.md#saved-files-and-settings)). For a style file that needs metadata,
the future writer must use an object rather than the legacy array; readers with this guard refuse
its newer format before interpreting it.
Expand Down
7 changes: 4 additions & 3 deletions docs/maintaining.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ same files. New and changed code follows these rules; not every existing reader
misread.
- Where a change allows it, reading a new version ships at least one release before anything
writes it.
- A project, map or layer file lists in `requires` each capability it needs that an older reader
would lose or misread: a reader refuses one it doesn't know, naming it. Task, presentation,
notebook and model readers don't check `requires` yet. A release reads
- A project, map or layer file, a model or a web tool lists in `requires` each capability it needs
that an older reader would lose or misread: a reader refuses one it doesn't know, naming it.
Task, presentation and notebook readers, and models inside a task's steps, don't check
`requires` yet. A release reads
`.rgis` format 2 before any writes it (`READ_FORMAT` and `CAPABILITIES` in
`crates/engine/src/project.rs`), so what is added to format 2 after that comes with a capability
name, added to `CAPABILITIES` where it is read.
Expand Down
Loading