ysinghc opened a new pull request, #35798:
URL: https://github.com/apache/superset/pull/35798
fix(charts): validate query_context metadata to prevent incomplete chart data
### SUMMARY
This PR addresses a bug where charts could be saved with incomplete
`query_context` metadata (missing `datasource` and `queries` fields),
causing 422 errors when attempting to load chart data.
**Problem:**
- Charts were being saved with `query_context` missing required fields
- This caused runtime errors: `"QueryContextFactory.create() missing 2
required keyword-only arguments: 'datasource' and 'queries'"`
- Errors only surfaced when loading chart data, not at save time
- Led to broken dashboards and poor user experience
**Solution:**
- Added `validate_query_context_metadata()` validator to ensure structural
integrity
- Validates that when `query_context` is provided (not `None`), it must
contain:
- `datasource` field with data source information
- `queries` field with query objects array
- Applied validator to both `ChartPostSchema` and `ChartPutSchema`
- Maintains full backwards compatibility: `None` values are still allowed
**Changes:**
1. `superset/utils/schema.py`: New `validate_query_context_metadata()`
function
2. `superset/charts/schemas.py`: Updated validation in `ChartPostSchema` and
`ChartPutSchema`
3. `tests/unit_tests/charts/test_schemas.py`: Added comprehensive test
coverage
**Validation Behavior:**
- Valid `query_context` with `datasource` and `queries`: Pass
- `None`/`null` query_context: Pass (backwards compatible)
- Empty object `{}`: Rejected
- Missing `datasource` field: Rejected
- Missing `queries` field: Rejected
- Invalid JSON: Rejected
### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
<!--- Skip this if not applicable -->
### TESTING INSTRUCTIONS
<!--- Required! What steps can be taken to manually verify the changes? -->
test_chart_post_schema_query_context_validation() - Validates POST endpoint
test_chart_put_schema_query_context_validation() - Validates PUT endpoint
Tests cover: valid data, None values, missing fields, empty objects, invalid
JSON
### ADDITIONAL INFORMATION
<!--- Check any relevant boxes with "x" -->
<!--- HINT: Include "Fixes #nnn" if you are fixing an existing issue -->
- [x] Has associated issue: #35774
- [ ] Required feature flags:
- [ ] Changes UI
- [ ] Includes DB Migration (follow approval process in
[SIP-59](https://github.com/apache/superset/issues/13351))
- [ ] Migration is atomic, supports rollback & is backwards-compatible
- [ ] Confirm DB migration upgrade and downgrade tested
- [ ] Runtime estimates and downtime expectations provided
- [ ] Introduces new feature or API
- [ ] Removes existing feature or API
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]