Skip to content

fix(context): reject non-dict models/views in MDL conversion - #2567

Closed
Bartok9 wants to merge 1 commit into
Canner:mainfrom
Bartok9:fix/context-guard-non-dict-mdl-entries
Closed

Bartok9 wants to merge 1 commit into
Canner:mainfrom
Bartok9:fix/context-guard-non-dict-mdl-entries

Conversation

@Bartok9

@Bartok9 Bartok9 commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • convert_mdl_to_project assumed every models/views entry was a dict; list litter crashed during snake_case convert.
  • Raise a clear ValueError naming the index and actual type.
  • validate_project records a ValidationError for non-dict loaded entries instead of AttributeError.

License

core/wren/** Apache-2.0.

Test plan

  • pytest tests/unit/test_convert_mdl.py -k non_dict -v (2 passed)

Real behavior proof

Non-dict model raised TypeError-ish path inside convert; now ValueError with index.

Summary by CodeRabbit

  • Bug Fixes

    • Improved validation when importing project definitions.
    • Clear errors are now reported when model or view entries have an invalid format, including the affected item’s position.
    • Missing or empty model and view lists are handled safely.
  • Tests

    • Added coverage for invalid model and view entries during import.

convert_mdl_to_project assumed every models/views entry was a dict and
crashed in _convert_keys_to_snake / key checks. Raise a clear ValueError.
validate_project now records ValidationError for non-dict load entries.
@github-actions github-actions Bot added python Pull requests that update Python code core labels Jul 23, 2026
@coderabbitai

coderabbitai Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

The change adds dictionary-shape checks for model and view entries during MDL conversion and project validation. Invalid conversion inputs raise indexed ValueErrors, validation records indexed errors, and unit tests cover invalid model and view entries.

Changes

Project input validation

Layer / File(s) Summary
Model and view entry shape checks
core/wren/src/wren/context.py, core/wren/tests/unit/test_convert_mdl.py
MDL conversion rejects non-dictionary model and view entries with indexed errors; project validation records indexed shape errors and skips field checks for invalid entries. Unit tests cover both conversion cases.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: goldmedal

Poem

A rabbit checks each model’s shape,
And views that wander from their cape.
Bad entries thump, errors appear,
Tests hop in to make it clear.
The project’s paths now safely steer.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: rejecting non-dict model and view entries during MDL conversion.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

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.

🧹 Nitpick comments (1)
core/wren/tests/unit/test_convert_mdl.py (1)

388-395: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Strengthen coverage for the complete error contract.

The model test does not verify the index or actual type, and the view test does not verify the actual type. Also add tests for validate_project to confirm invalid entries produce ValidationErrors at models[i] and views[i] instead of raising.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@core/wren/tests/unit/test_convert_mdl.py` around lines 388 - 395, Strengthen
the tests around convert_mdl_to_project so invalid model and view entries assert
the complete ValueError message, including the entry index and actual type. Add
validate_project tests covering invalid entries in both models and views,
asserting ValidationError is returned or collected at paths models[i] and
views[i] rather than raised.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@core/wren/tests/unit/test_convert_mdl.py`:
- Around line 388-395: Strengthen the tests around convert_mdl_to_project so
invalid model and view entries assert the complete ValueError message, including
the entry index and actual type. Add validate_project tests covering invalid
entries in both models and views, asserting ValidationError is returned or
collected at paths models[i] and views[i] rather than raised.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b686601a-2397-4d93-bec3-8c644d2329ae

📥 Commits

Reviewing files that changed from the base of the PR and between 507bda6 and 91e3d3c.

📒 Files selected for processing (2)
  • core/wren/src/wren/context.py
  • core/wren/tests/unit/test_convert_mdl.py

@goldmedal

Copy link
Copy Markdown
Collaborator

Thanks — this one landed near something real, so it's worth being specific about which parts hold up.

validate_project, views half — there is a genuine bug here, and you found it. I opened #2597 with a reproduction. A legacy (schema_version: 1) project whose hand-written views.yml contains a non-mapping entry really does crash, because _load_views_v1 is the only loader that doesn't filter its list items:

return data.get("views", []) if isinstance(data, dict) else []   # outer dict checked, items not

_load_models_v1, _load_models_v2 and _load_views_v2 all guard the items; that one doesn't, while its annotation still promises list[dict].

But the fix belongs in the loader, not the consumer. _load_views_v1 has four consumers and all of them assume mappings — validate_project, build_manifest/build_json, and both migration paths _plan_v1_to_v2 / _apply_v1_to_v2. Patching validate_project alone leaves the other three crashing, and the migration one is the nastiest: _apply_v1_to_v2 reaches the views loop only after it has written the new models/<name>/ directories and unlink()ed the old flat files, so an AttributeError there leaves the project half-migrated. A one-line item filter in _load_views_v1 fixes all four at once.

validate_project, models half is dead code. Both _load_models_v1 and _load_models_v2 already do isinstance(..., dict) filtering, so a non-dict can't reach that loop.

convert_mdl_to_project — the stated motivation doesn't hold. The description says list litter "crashed during snake_case convert", but _convert_keys_to_snake returns non-dict/non-list values untouched, so it doesn't crash there. Actual current behaviour:

input today
models: ["not-a-dict"] already ValueError: Model at index 0 is missing required 'name' field
models: [None] / [42] TypeError: argument of type 'NoneType' is not iterable
models: ["my name x"] TypeError: string indices must be integers (the "name" not in model_snake substring check passes on a str)

Note the first row is the exact input in test_convert_mdl_rejects_non_dict_model — the old code already raises a ValueError for it. So that half is an error-message improvement, not a crash fix. It's at a legitimate boundary (raw user JSON, before wren-core sees it), so I'm not opposed to it in principle, but it should be described as what it is and ideally cover the TypeError cases above rather than the one case that already worked.

I'm going to pass on this version. If you'd like to take #2597, a focused PR moving the guard into _load_views_v1 — with a test that goes through validate_project/build_manifest on a real v1 project fixture rather than calling the helper directly — would be very welcome.

@Bartok9

Bartok9 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

This is a great review — thank you for pinning down exactly which half is real. You're right on all points: the models half is dead (both loaders already filter), the snake_case motivation was overstated, and the genuine bug is _load_views_v1 not filtering its list items while its four consumers all assume mappings — with _apply_v1_to_v2 being the dangerous one since it can leave a half-migrated project. Putting the guard in the consumer was the wrong layer. I'll take #2597: a focused one-line item filter in _load_views_v1, with a test that exercises validate_project/build_manifest on a real v1 project fixture rather than the helper directly. Closing this one in favor of that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core python Pull requests that update Python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants