You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ossie.apache.org by jb...@apache.org on 2026/09/23 16:28:05 UTC

(ossie) branch main updated: fix(validation): reject empty identifiers and survive nested input (#414)

This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ossie.git


The following commit(s) were added to refs/heads/main by this push:
     new 938a439f fix(validation): reject empty identifiers and survive nested input (#414)
938a439f is described below

commit 938a439f0170abfe702952f6871f2221690becb5
Author: Ogunniran Oluwasijibomi <67...@users.noreply.github.com>
AuthorDate: Wed Sep 23 17:27:58 2026 +0100

    fix(validation): reject empty identifiers and survive nested input (#414)
    
    Empty-string identifiers and pathologically nested input either passed
    validation silently or crashed the validator with a raw traceback.
    
    - Add minLength: 1 to every required identifier string in the schema
      (name on model/dataset/field/metric/relationship, dataset source, and
      relationship from/to). Empty values are rejected with a clear [Schema]
      error before semantic checks run, so a single empty name (which
      duplicate detection never sees) is caught at the contract level.
    - validate_sql_expression: add RecursionError to the first parse
      attempt and report it from the SELECT-wrapped retry, so deeply nested
      SQL yields a diagnostic. Only RecursionError is added, so genuine
      errors such as a non-string expression raising TypeError still surface
      instead of being masked as valid SQL.
    - main(): catch RecursionError from deeply nested YAML so the validator
      exits with a diagnostic instead of a traceback.
    - Tests: schema-level empty-identifier cases (unittest and pytest), a
      non-string-expression case, and deeply nested SQL and YAML cases.
    
    Closes #408
---
 core-spec/ossie-schema.json       |  8 +++++++
 validation/test_validate.py       | 33 ++++++++++++++++++++++++++
 validation/tests/test_validate.py | 49 +++++++++++++++++++++++++++++++++++++++
 validation/validate.py            | 14 ++++++++++-
 4 files changed, 103 insertions(+), 1 deletion(-)

diff --git a/core-spec/ossie-schema.json b/core-spec/ossie-schema.json
index 7b9c9991..a8d2fcbd 100644
--- a/core-spec/ossie-schema.json
+++ b/core-spec/ossie-schema.json
@@ -155,6 +155,7 @@
       "properties": {
         "name": {
           "type": "string",
+          "minLength": 1,
           "description": "Unique identifier for the field within the dataset"
         },
         "expression": {
@@ -193,10 +194,12 @@
       "properties": {
         "name": {
           "type": "string",
+          "minLength": 1,
           "description": "Unique identifier for the dataset"
         },
         "source": {
           "type": "string",
+          "minLength": 1,
           "description": "Reference to underlying physical table/view (database.schema.table) or query"
         },
         "primary_key": {
@@ -245,14 +248,17 @@
       "properties": {
         "name": {
           "type": "string",
+          "minLength": 1,
           "description": "Unique identifier for the relationship"
         },
         "from": {
           "type": "string",
+          "minLength": 1,
           "description": "Dataset on the many side of the relationship"
         },
         "to": {
           "type": "string",
+          "minLength": 1,
           "description": "Dataset on the one side of the relationship"
         },
         "from_columns": {
@@ -290,6 +296,7 @@
       "properties": {
         "name": {
           "type": "string",
+          "minLength": 1,
           "description": "Unique identifier for the metric"
         },
         "expression": {
@@ -321,6 +328,7 @@
       "properties": {
         "name": {
           "type": "string",
+          "minLength": 1,
           "description": "Unique identifier for the semantic model"
         },
         "description": {
diff --git a/validation/test_validate.py b/validation/test_validate.py
index 2519b955..bf1b2c2b 100644
--- a/validation/test_validate.py
+++ b/validation/test_validate.py
@@ -420,6 +420,39 @@ class ValidatorIntegrationTest(unittest.TestCase):
         self.assertEqual(result.returncode, 1)
         self.assertIn("'dialects', 'vendors' were unexpected", result.stdout)
 
+    def test_deeply_nested_yaml_fails_cleanly(self):
+        # Deeply nested flow collections exhaust the recursion limit during
+        # composition, surfacing as RecursionError rather than YAMLError. The
+        # validator must exit with a diagnostic, never a raw traceback.
+        result = self.run_validator("[" * 3000 + "]" * 3000 + "\n")
+
+        self.assertEqual(result.returncode, 1)
+        self.assertNotIn("Traceback", result.stderr)
+        self.assertIn("too deeply nested", result.stdout)
+
+    def test_empty_relationship_endpoint_is_rejected_by_schema(self):
+        # An empty relationship endpoint ("") is invalid data; the schema
+        # (minLength: 1) rejects it end-to-end before semantic checks run,
+        # rather than the model silently passing.
+        result = self.run_validator(
+            "version: 0.2.0.dev0\n"
+            "name: sales\n"
+            "datasets:\n"
+            "  - name: orders\n"
+            "    source: analytics.orders\n"
+            "relationships:\n"
+            "  - name: orders_to_missing\n"
+            "    from: orders\n"
+            "    to: ''\n"
+            "    from_columns: [customer_id]\n"
+            "    to_columns: [id]\n"
+        )
+
+        self.assertEqual(result.returncode, 1)
+        self.assertNotIn("Traceback", result.stderr)
+        self.assertIn("Validation FAILED", result.stdout)
+        self.assertIn("[Schema] relationships -> 0 -> to: '' should be non-empty", result.stdout)
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/validation/tests/test_validate.py b/validation/tests/test_validate.py
index 8583c53c..22a86735 100644
--- a/validation/tests/test_validate.py
+++ b/validation/tests/test_validate.py
@@ -166,6 +166,35 @@ def test_unique_names_are_checked_in_the_root_model() -> None:
     assert errors == ["[Unique] Duplicate dataset name 'orders' in model 'm'"]
 
 
+@pytest.mark.parametrize(
+    ("mutate", "path"),
+    [
+        (lambda doc: doc.update(name=""), "name"),
+        (lambda doc: doc["datasets"][0].update(name=""), "datasets -> 0 -> name"),
+        (lambda doc: doc["datasets"][0].update(source=""), "datasets -> 0 -> source"),
+    ],
+)
+def test_schema_rejects_empty_identifiers(core_schema: dict, mutate, path: str) -> None:
+    # Empty identifiers are invalid data. The schema (minLength: 1) rejects them
+    # up front, so no downstream check has to special-case "" -- and a single
+    # empty name is caught even though it never trips duplicate detection.
+    document = _document([dict(_ORDERS)], [])
+    mutate(document)
+
+    assert _VALIDATE.validate_schema(document, core_schema) == [
+        f"[Schema] {path}: '' should be non-empty"
+    ]
+
+
+def test_schema_rejects_empty_relationship_endpoints(core_schema: dict) -> None:
+    document = _document([dict(_ORDERS), dict(_CUSTOMERS)], [_relationship(to_columns=["id"])])
+    document["relationships"][0]["to"] = ""
+
+    assert _VALIDATE.validate_schema(document, core_schema) == [
+        "[Schema] relationships -> 0 -> to: '' should be non-empty"
+    ]
+
+
 def test_sql_checks_traverse_root_fields_and_metrics(monkeypatch: pytest.MonkeyPatch) -> None:
     seen = []
 
@@ -261,6 +290,26 @@ def test_still_reports_unknown_datasets() -> None:
     ]
 
 
+@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed")
+def test_deeply_nested_sql_reports_a_diagnostic_instead_of_crashing() -> None:
+    # Pathologically nested SQL exhausts sqlglot's recursion limit; the
+    # validator must turn that into a diagnostic rather than propagating
+    # RecursionError and aborting the run.
+    expression = "(" * 5000 + "1" + ")" * 5000
+    result = _VALIDATE.validate_sql_expression(expression, "ANSI_SQL", "ctx")
+
+    assert result == "[SQL] ctx: expression is too deeply nested to parse"
+
+
+@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed")
+def test_non_string_expression_surfaces_instead_of_being_reported_valid() -> None:
+    # Only RecursionError is caught, so a genuine bug -- a non-string expression
+    # reaching the parser -- raises loudly rather than being masked as valid SQL
+    # by the SELECT-wrapped retry.
+    with pytest.raises(TypeError):
+        _VALIDATE.validate_sql_expression(123, "ANSI_SQL", "ctx")
+
+
 def test_tolerates_null_unique_keys() -> None:
     # `unique_keys:` present but empty parses to None; the check must not crash.
     dataset = {"name": "customers", "source": "db.s.customers",
diff --git a/validation/validate.py b/validation/validate.py
index 99b390c6..66548f58 100644
--- a/validation/validate.py
+++ b/validation/validate.py
@@ -292,7 +292,11 @@ def validate_sql_expression(expr: str, dialect: str, context: str) -> str | None
         # Try parsing as expression first (for field expressions like "column_name")
         sqlglot.parse_one(expr, dialect=sqlglot_dialect)
         return None
-    except (ParseError, TokenError):
+    except (ParseError, TokenError, RecursionError):
+        # A bare column reference fails to parse alone; retry it wrapped in
+        # SELECT below. RecursionError (deeply nested input) is included so the
+        # retry reports it instead of crashing, while genuine errors such as a
+        # non-string expr raising TypeError still surface.
         pass
 
     try:
@@ -301,6 +305,10 @@ def validate_sql_expression(expr: str, dialect: str, context: str) -> str | None
         return None
     except (ParseError, TokenError) as e:
         return f"[SQL] {context}: {str(e).split(chr(10))[0]}"
+    except RecursionError:
+        # Deeply nested input exhausts the recursion limit rather than raising a
+        # parser error; report it instead of letting it abort validation.
+        return f"[SQL] {context}: expression is too deeply nested to parse"
 
 
 def validate_sql(data: dict) -> list[str]:
@@ -384,6 +392,10 @@ def main():
         except yaml.YAMLError as e:
             print(f"Error: Invalid YAML: {e}")
             sys.exit(1)
+        except RecursionError:
+            # Deeply nested input surfaces as RecursionError, not YAMLError.
+            print("Error: Invalid YAML: input is too deeply nested to parse")
+            sys.exit(1)
 
     # Run validations
     errors = []