Skip to content

Commit 3e6b313

Browse files
committed
fix errors in dynamic component being silent
fixes #314
1 parent 06e55b0 commit 3e6b313

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
```
1212
- Casting values from one type to another using the `::` operator is only supported by PostgreSQL. SQLPage versions before 0.20.5 would silently convert all casts to the `CAST(... AS ...)` syntax, which is supported by all databases. Since 0.20.5, SQLPage started to respect the original `::` syntax, and pass it as-is to the database. This broke existing SQLPage websites that used the `::` syntax with databases other than PostgreSQL. For backward compatibility, this version of SQLPage re-establishes the previous behavior, converts `::` casts on non-PostgreSQL databases to the `CAST(... AS ...)` syntax, but will display a warning in the logs.
1313
- In short, if you saw an error like `Error: unrecognized token ":"` after upgrading to 0.20.5, this version should fix it.
14+
- The `dynamic` component now properly displays error messages when its properties are invalid. There used to be a bug where errors would be silently ignored, making it hard to debug invalid dynamic components.
1415

1516
## 0.20.5 (2024-05-07)
1617

‎src/dynamic_component.rs‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,19 @@ impl Iterator for DynamicComponentIterator {
4040
}
4141

4242
fn expand_dynamic_stack(stack: &mut Vec<anyhow::Result<JsonValue>>) {
43-
while let Some(Ok(mut next)) = stack.pop() {
44-
if let Some(properties) = extract_dynamic_properties(&mut next) {
43+
while let Some(mut next) = stack.pop() {
44+
let dyn_props = next
45+
.as_mut()
46+
.ok()
47+
.and_then(|mut v| extract_dynamic_properties(&mut v));
48+
if let Some(properties) = dyn_props {
49+
// if the properties contain new (nested) dynamic components, push them onto the stack
4550
stack.extend(dynamic_properties_to_vec(properties));
4651
} else {
47-
stack.push(Ok(next));
52+
// If the properties are not dynamic, push the row back onto the stack
53+
stack.push(next);
54+
// return at the first non-dynamic row
55+
// we don't support non-dynamic rows after dynamic rows nested in the same array
4856
return;
4957
}
5058
}
@@ -79,12 +87,8 @@ fn dynamic_properties_to_result_vec(
7987
mut properties_obj: JsonValue,
8088
) -> anyhow::Result<Vec<JsonValue>> {
8189
if let JsonValue::String(s) = properties_obj {
82-
properties_obj = serde_json::from_str::<JsonValue>(&s).with_context(|| {
83-
format!(
84-
"Unable to parse the 'properties' property of the dynamic component as JSON.\n\
85-
Invalid json: {s}"
86-
)
87-
})?;
90+
properties_obj = serde_json::from_str::<JsonValue>(&s)
91+
.with_context(|| format!("Invalid json in dynamic component properties: {s}"))?;
8892
}
8993
match properties_obj {
9094
obj @ JsonValue::Object(_) => Ok(vec![obj]),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
select 'dynamic' as component; -- missing "properties" column
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
select 'dynamic' as component,
2+
'{ "this object": "has a forbidden comma" , }' as properties; -- this is invalid JSON, and should cause an error

0 commit comments

Comments
 (0)