Merge pull request #2449 from Hamza0101/fix/rest-stroke-side-variables

REST import: variables bound to stroke sides bind each side's weight
This commit is contained in:
BFlat authored and GitHub committed 2026-11-18 19:19:51 +07:00
commit c7f0ad195c
2 files changed
+69

No files matched your search

@@ -188,6 +188,62 @@ fn bound_variables_and_explicit_modes() {
assert!(has(&warnings, "pass the local variables JSON"), "{warnings:?}");
}
/// #2441: a variable bound to one or more stroke sides (`individualStrokeWeights`) binds that
/// side's weight. The live API names the sides `BORDER_TOP_WEIGHT` …, the published spec `top` …;
/// both are read, and neither is reported as skipped.
#[test]
fn variables_bound_to_stroke_sides_bind_each_sides_weight() {
const TOP: &str = "VariableID:9:1";
const RIGHT: &str = "VariableID:9:2";
const BOTTOM: &str = "VariableID:9:3";
const LEFT: &str = "VariableID:9:4";
let rect = |id: &str, name: &str, sides: Value| {
json!({"id": id, "name": name, "type": "RECTANGLE", "relativeTransform": at(0.0, 0.0), "size": {"x": 40, "y": 20},
"strokes": [{"type": "SOLID", "color": rgba(0.0, 0.0, 0.0)}], "strokeWeight": 1,
"individualStrokeWeights": {"top": 1, "right": 1, "bottom": 1, "left": 1},
"boundVariables": {"individualStrokeWeights": sides}})
};
let file = json!({"name": "Sides", "document": {"id": "0:0", "type": "DOCUMENT", "children": [
{"id": "0:1", "name": "Page", "type": "CANVAS", "children": [
rect("1:1", "Row", json!({"BORDER_BOTTOM_WEIGHT": alias(SPACE)})),
rect("1:2", "Card", json!({"BORDER_TOP_WEIGHT": alias(TOP), "BORDER_RIGHT_WEIGHT": alias(RIGHT),
"BORDER_BOTTOM_WEIGHT": alias(BOTTOM), "BORDER_LEFT_WEIGHT": alias(LEFT)})),
rect("1:3", "Spec", json!({"top": alias(TOP), "right": alias(RIGHT), "bottom": alias(BOTTOM), "left": alias(LEFT)})),
rect("1:4", "Colour", json!({"BORDER_LEFT_WEIGHT": alias(BRAND)}))
]}
]}});
// A variable of its own for each of Card's sides, so a side read into another's field fails.
let mut vars = variables_json();
for id in [TOP, RIGHT, BOTTOM, LEFT] {
vars["meta"]["variables"][id] = json!({"id": id, "name": id, "variableCollectionId": THEME, "resolvedType": "FLOAT",
"valuesByMode": {"1:0": 1}, "scopes": ["ALL_SCOPES"]});
}
let (doc, warnings) = import_with(&file, Some(&vars), &no_images).unwrap();
let sides = |name: &str| -> Vec<(String, String)> {
let n = named(&doc, name);
["strokeTopWeight", "strokeRightWeight", "strokeBottomWeight", "strokeLeftWeight"]
.iter()
.filter_map(|f| n.bound.get(*f).map(|v| (f.to_string(), v.clone())))
.collect()
};
let b = |f: &str| (f.to_string(), SPACE.to_string());
let on = |f: &str, v: &str| (f.to_string(), v.to_string());
assert_eq!(sides("Row"), [b("strokeBottomWeight")], "one side bound");
assert_eq!(
sides("Card"),
[on("strokeTopWeight", TOP), on("strokeRightWeight", RIGHT), on("strokeBottomWeight", BOTTOM), on("strokeLeftWeight", LEFT)]
);
assert_eq!(
sides("Spec"),
[on("strokeTopWeight", TOP), on("strokeRightWeight", RIGHT), on("strokeBottomWeight", BOTTOM), on("strokeLeftWeight", LEFT)],
"the spec's names"
);
assert_eq!(sides("Colour"), [], "a colour variable can't be a weight");
assert!(!has(&warnings, "`individualStrokeWeights`"), "{warnings:?}");
assert!(has(&warnings, "type doesn't fit"), "{warnings:?}");
assert!(doc.validate().is_empty(), "{:?}", doc.validate());
}
#[test]
fn embedded_variables_match_separate_ones() {
let mut embedded = file_json();
+13
View File
@@ -427,6 +427,19 @@ fn node_binding<'v>(k: &str, v: &'v Value, n: &Node, out: &mut Vec<(String, &'v
push(f, v.get(key).and_then(alias));
}
}
// Each stroke side's weight (#2441). The live `/nodes` response names the sides
// `BORDER_TOP_WEIGHT` and so on, as `rectangleCornerRadii` names its corners; Figma's
// published OpenAPI spec names them `top` … `left`. Either is read.
"individualStrokeWeights" => {
for (keys, f) in [
(["BORDER_TOP_WEIGHT", "top"], "strokeTopWeight"),
(["BORDER_RIGHT_WEIGHT", "right"], "strokeRightWeight"),
(["BORDER_BOTTOM_WEIGHT", "bottom"], "strokeBottomWeight"),
(["BORDER_LEFT_WEIGHT", "left"], "strokeLeftWeight"),
] {
push(f, keys.iter().find_map(|key| v.get(*key)).and_then(alias));
}
}
"fills" | "strokes" => {
let field = if k == "fills" { "fill" } else { "stroke" };
match v.as_array().and_then(|a| a.first()).and_then(alias) {