Skip to content

fix(charts): reject non-table datasource_type instead of crashing - #43500

Merged
sadpandajoe merged 4 commits into
masterfrom
fix/chart-saved-query-datasource-type
Sep 2, 2026
Merged

sadpandajoe merged 4 commits into
masterfrom
fix/chart-saved-query-datasource-type

Conversation

@rusackas

Copy link
Copy Markdown
Member

SUMMARY

POST /api/v1/chart/ (and PUT /api/v1/chart/<pk>) with datasource_type: "saved_query" crashes with an unhandled AttributeError: 'SavedQuery' object has no attribute 'name', surfaced to API clients as an opaque 500 {"message": "Fatal error"}.

The deeper issue: Slice.datasource only ever resolves the table relationship (superset/models/slice.py), so a chart pointed at any other datasource_type could never actually render even if creation succeeded. That's the case for datasource_type: "query" too — Query happens to have a synthetic .name property (used for CTAS table naming, not a real display name), so it doesn't crash, it just silently produces a permanently broken chart instead.

CreateChartCommand and UpdateChartCommand now reject both up front with the existing DatasourceTypeInvalidError (422), matching the pattern already used for this same class of problem elsewhere in the codebase (explore/utils.py, dataset/duplicate.py), instead of adding a new one-off error type.

Root-caused with help from the discussion on the issue — thanks @endimonan for the minimal repro that ruled out pivot tables as a factor and narrowed this down.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

Not applicable (API error-handling behavior).

TESTING INSTRUCTIONS

pytest tests/unit_tests/commands/chart/create_test.py tests/unit_tests/commands/chart/update_test.py tests/unit_tests/commands/chart/ tests/unit_tests/charts/

Written test-first: each new unit test was confirmed to fail against unfixed code (reproducing the exact AttributeError for saved_query, and the "silently succeeds with a broken chart" behavior for query) before the fix was applied, then confirmed to pass after.

Also added an integration test (tests/integration_tests/charts/api_tests.py::test_create_chart_from_saved_query_rejected_cleanly) that reproduces the original bug report's exact API call shape end-to-end — creates a real SavedQuery row, then asserts POST /api/v1/chart/ against it returns a clean 422 instead of a 500. Couldn't run the integration suite locally (this environment's SQLite test DB isn't migrated), so CI is the authority on that one.

Manual repro before the fix, matching the original report:

curl -X POST "$SUPERSET_API_URL/api/v1/saved_query/" ... --data '{"db_id": 1, "label": "repro", "sql": "SELECT 1"}'
# => 201, id: N
curl -X POST "$SUPERSET_API_URL/api/v1/chart/" ... --data '{"datasource_id": N, "datasource_type": "saved_query", "slice_name": "x", "viz_type": "table"}'
# before: 500 {"message": "Fatal error"}
# after:  422 {"message": {"datasource_type": ["Datasource type is invalid"]}}

ADDITIONAL INFORMATION

Slice.datasource only ever resolves the "table" relationship, so a
chart created (or repointed via update) with datasource_type
"saved_query" or "query" would either crash outright or "succeed" as
a chart that can never actually render:

- "saved_query": SavedQuery has no .name attribute, so validate()
  crashes with an unhandled AttributeError -- surfaced to API clients
  as an opaque 500 "Fatal error" (fixes #29697).
- "query": Query has a synthetic .name property (used for CTAS table
  naming, not a real display name), so this one doesn't crash -- it
  silently creates a permanently broken chart instead.

CreateChartCommand and UpdateChartCommand now reject both up front
with the existing DatasourceTypeInvalidError (422), matching the
pattern already used for this same class of problem in
explore/utils.py and dataset/duplicate.py, rather than adding a new
one-off error type.

Adds unit tests for both commands (TDD: written first against
unfixed code to confirm they reproduce the two distinct failure
modes above, then the fix, then confirmed green) and an integration
test reproducing the original bug report's exact API call shape.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@dosubot dosubot Bot added api Related to the REST API api:charts Related to the REST endpoints of charts change:backend Requires changing the backend labels Aug 25, 2026
@bito-code-review

bito-code-review Bot commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Agent Run #816752

Actionable Suggestions - 0
Review Details
  • Files reviewed - 5 · Commit Range: f750321..f750321
    • superset/commands/chart/create.py
    • superset/commands/chart/update.py
    • tests/integration_tests/charts/api_tests.py
    • tests/unit_tests/commands/chart/create_test.py
    • tests/unit_tests/commands/chart/update_test.py
  • Files skipped - 0
  • Tools
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers an incremental AI Review.

  • /review full - Manually triggers a full AI Review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@github-actions github-actions Bot removed the api Related to the REST API label Aug 25, 2026
Comment thread superset/commands/chart/update.py Resolved
@bito-code-review

Copy link
Copy Markdown
Contributor

The flagged issue is correct. The current implementation in superset/commands/chart/update.py raises DatasourceTypeInvalidError inside the try block, which causes it to be caught and appended to the validation errors. If datasource_type is missing, a DatasourceTypeUpdateRequiredValidationError is already present, and appending the invalid-type error for an empty string overwrites the more relevant required-field message.

To resolve this, you should only perform the DatasourceType check when datasource_type is provided. You can update the guard in superset/commands/chart/update.py as follows:

                if datasource_type and datasource_type != DatasourceType.TABLE:
                    raise DatasourceTypeInvalidError()

This ensures the check only runs when a type is supplied, preserving the existing required-field validation when it is missing. Would you like me to check the rest of the comments on this PR to see if there are other issues to address?

superset/commands/chart/update.py

if datasource_type and datasource_type != DatasourceType.TABLE:
                    raise DatasourceTypeInvalidError()

@codecov

codecov Bot commented Aug 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.75000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 78.99%. Comparing base (6c2fef2) to head (aaa9584).
⚠️ Report is 47 commits behind head on master.

Files with missing lines Patch % Lines
superset/commands/chart/update.py 91.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #43500      +/-   ##
==========================================
+ Coverage   78.93%   78.99%   +0.06%     
==========================================
  Files        2877     2878       +1     
  Lines      165232   165566     +334     
  Branches    38184    38266      +82     
==========================================
+ Hits       130429   130797     +368     
+ Misses      32341    32283      -58     
- Partials     2462     2486      +24     
Flag Coverage Δ
hive 37.95% <25.00%> (-0.06%) ⬇️
mysql 57.70% <68.75%> (-0.05%) ⬇️
postgres 57.74% <68.75%> (-0.05%) ⬇️
presto 39.87% <25.00%> (-0.07%) ⬇️
python 83.68% <93.75%> (+0.11%) ⬆️
sqlite 57.43% <68.75%> (-0.06%) ⬇️
unit 73.79% <93.75%> (+0.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Skip the non-table datasource_type guard entirely when datasource_type
is empty, so the existing "Datasource type is required" message isn't
overwritten by "Datasource type is invalid" for the same field key.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@bito-code-review

bito-code-review Bot commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Agent Run #361309

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: f750321..d4df7ca
    • superset/commands/chart/update.py
    • tests/unit_tests/commands/chart/update_test.py
  • Files skipped - 0
  • Tools
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers an incremental AI Review.

  • /review full - Manually triggers a full AI Review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@sadpandajoe
sadpandajoe requested review from sadpandajoe and a lite review from Copilot August 25, 2026 17:20

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@netlify

netlify Bot commented Aug 26, 2026

Copy link
Copy Markdown

✅ Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit e82adfb
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/6a8e50c692edc700089cc766
😎 Deploy Preview https://deploy-preview-43500--superset-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@bito-code-review

bito-code-review Bot commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Agent Run #c6543b

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: d4df7ca..e82adfb
    • tests/integration_tests/charts/api_tests.py
  • Files skipped - 0
  • Tools
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers an incremental AI Review.

  • /review full - Manually triggers a full AI Review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

Comment thread superset/commands/chart/update.py Resolved
…y from table

A chart update payload that sets datasource_type without also sending
datasource_id skipped the non-table rejection entirely, since the guard
only checked datasource_type when datasource_id was also present. That
left Slice.datasource unable to resolve the chart, since its relationship
only ever joins on datasource_type == 'table'.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@bito-code-review

bito-code-review Bot commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Agent Run #484877

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: e82adfb..aaa9584
    • superset/commands/chart/update.py
    • tests/unit_tests/commands/chart/update_test.py
  • Files skipped - 0
  • Tools
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers an incremental AI Review.

  • /review full - Manually triggers a full AI Review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@sadpandajoe
sadpandajoe merged commit 201a33f into master Sep 2, 2026
73 checks passed
@sadpandajoe
sadpandajoe deleted the fix/chart-saved-query-datasource-type branch September 2, 2026 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api:charts Related to the REST endpoints of charts change:backend Requires changing the backend preset-io size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fatal error when creating a chart from a saved_query via API call

3 participants