ysinghc opened a new pull request, #36076:
URL: https://github.com/apache/superset/pull/36076
### SUMMARY
This PR adds comprehensive validation for the `query_context` field in both
`ChartPostSchema` and `ChartPutSchema` to ensure that when provided, it
contains the required metadata fields (`datasource` and `queries`) needed for
chart data retrieval.
**Changes:**
- Created a new `validate_query_context_metadata()` function in
`superset/utils/schema.py` that validates the structure of query_context JSON
- Updated `ChartPostSchema` to use the new validator (previously used
generic `validate_json`)
- Updated `ChartPutSchema` to add the new validator (previously had no
validation)
- Added comprehensive unit tests for the new validator in
`tests/unit_tests/utils/test_schema.py`
- Added integration tests in `tests/unit_tests/charts/test_schemas.py` to
verify schema-level validation
**Rationale:**
Previously, the `query_context` field only validated that the value was
valid JSON, but didn't ensure it contained the necessary structure for chart
operations. This could lead to runtime errors when charts are rendered with
incomplete query context data. The new validation ensures data integrity at the
API level.
### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A - Backend validation change with no UI impact
### TESTING INSTRUCTIONS
1. **Run the unit tests:**
```bash
pytest tests/unit_tests/utils/test_schema.py -v
pytest
tests/unit_tests/charts/test_schemas.py::test_chart_post_schema_query_context_validation
-v
pytest
tests/unit_tests/charts/test_schemas.py::test_chart_put_schema_query_context_validation
-v
```
2. **Manual API testing (optional):**
- Start Superset in development mode
- Try to create a chart (POST `/api/v1/chart/`) with invalid
query_context (missing `datasource` or `queries`)
- Verify that the API returns a validation error
- Try with valid query_context containing both fields - should succeed
- Try with `null` query_context - should succeed (field is nullable)
3. **Verify pre-commit hooks pass:**
```bash
git add .
pre-commit run
```
### ADDITIONAL INFORMATION
- [X] Has associated issue: Fixes #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
- [x] 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]