Skip to content

fix(marshmallow): add compatibility layer for Flask-AppBuilder with marshmallow 4.x - #35920

Merged
rusackas merged 13 commits into
apache:masterfrom
kevin-lann:marshmallow-upgrade-v4
Jul 8, 2026
Merged

rusackas merged 13 commits into
apache:masterfrom
kevin-lann:marshmallow-upgrade-v4

Conversation

@kevin-lann

@kevin-lann kevin-lann commented Oct 31, 2025 •

Copy link
Copy Markdown
Contributor

SUMMARY

Adds the Marshmallow 4 compatibility work needed by Superset's backend schema layer.

  1. Adds a small compatibility shim for Flask-AppBuilder-generated Marshmallow schemas.
    Marshmallow 4 is stricter about declared_fields, and FAB can reference auto-generated relationship field names that are not explicitly declared. The new compatibility layer pre-stubs those FAB-style missing fields as Raw(allow_none=True, load_default=None) before _init_fields runs, which avoids schema initialization failures while still letting genuine non-FAB schema errors surface.

  2. Updates theme schema sanitization to work correctly with Marshmallow 4.
    The theme POST/PUT schemas now persist sanitized json_data through post_load using a context variable, so sanitized theme payloads are what ultimately get loaded and stored.

  3. Applies small Marshmallow-4-compatible signature cleanup in model helpers and adds unit coverage for the compatibility layer and theme schema behavior.

TESTING INSTRUCTIONS

  • Run:
    • pytest tests/unit_tests/test_marshmallow_compatibility.py
    • pytest tests/unit_tests/themes/api_test.py
  • Validate that the compatibility patch is idempotent and that FAB-style missing schema fields are stubbed before Marshmallow field initialization.
  • Validate that theme create/update schema loads return sanitized json_data when unsafe theme token values are provided.

ADDITIONAL INFORMATION

  • Has associated issue: Fixes issue Make Superset compatible with marshmallow>=4.0.0 #33162
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • 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

Credit to

@Austin-X @Eyang0612 @PDOracle @MasahisaSekita

@korbit-ai

korbit-ai Bot commented Oct 31, 2025

Copy link
Copy Markdown

Based on your review schedule, I'll hold off on reviewing this PR until it's marked as ready for review. If you'd like me to take a look now, comment /korbit-review.

Your admin can change your review schedule in the Korbit Console

@kevin-lann
kevin-lann marked this pull request as ready for review November 3, 2025 14:03
@dosubot dosubot Bot added the change:backend Requires changing the backend label Nov 3, 2025
@kevin-lann

Copy link
Copy Markdown
Contributor Author

/korbit-review

@korbit-ai korbit-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've completed my review and didn't find any issues.

Check out our docs on how you can make Korbit work best for you and your team.

Loving Korbit!? Share us on LinkedIn Reddit and X

korbit-ai[bot]
korbit-ai Bot previously requested changes Nov 3, 2025

@korbit-ai korbit-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review by Korbit AI

Korbit automatically attempts to detect when you fix issues in new commits.
Category Issue Status
Performance Redundant JSON serialization without caching ▹ view
Functionality Sanitized data stored but not consumed ▹ view
Functionality Missing import for Length validator ▹ view
Performance Module-level patch execution adds import overhead ▹ view
Functionality Marshmallow patch applied at module level ▹ view
Performance Inefficient retry loop with redundant field processing ▹ view
Design Unmanaged Monkey-Patching of External Library ▹ view
Design Improper Logging Mechanism ▹ view
Functionality Unreliable KeyError message parsing ▹ view
Functionality Unsafe declared_fields modification ▹ view
Files scanned ​
File Path Reviewed
superset/advanced_data_type/schemas.py ✅
superset/marshmallow_compatibility.py ✅
superset/themes/schemas.py ✅
superset/app.py ✅
superset/db_engine_specs/databend.py ✅
superset/reports/schemas.py ✅
superset/charts/schemas.py ✅

Explore our documentation to understand the languages and file types we support and the files we ignore.

Check out our docs on how you can make Korbit work best for you and your team.

Loving Korbit!? Share us on LinkedIn Reddit and X

Comment thread superset/themes/schemas.py
Comment thread superset/themes/schemas.py
Comment thread superset/charts/schemas.py Outdated
Comment thread superset/app.py Outdated
Comment thread superset/app.py Outdated
Comment thread superset/marshmallow_compatibility.py Outdated
Comment thread superset/marshmallow_compatibility.py Outdated
Comment thread superset/marshmallow_compatibility.py Outdated
Comment thread superset/marshmallow_compatibility.py Outdated
Comment thread superset/marshmallow_compatibility.py Outdated
@kevin-lann

Copy link
Copy Markdown
Contributor Author

@mistercrunch Hi, could you re-run workflows on this PR? Also lmk if you have any concerns/suggestions about the compatibility changes here. Much appreciated 🙏

@rusackas

Copy link
Copy Markdown
Member

re-running workflows 🤞

@rusackas

Copy link
Copy Markdown
Member

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.

Pull Request Overview

This PR upgrades marshmallow from version 3.x to 4.x to resolve compatibility issues with Flask-AppBuilder 5.0.0. The upgrade addresses a critical initialization error where Flask-AppBuilder auto-generates schema fields for SQLAlchemy relationships that don't pass marshmallow 4.x's stricter field validation.

Key Changes:

  • Introduces a compatibility layer that patches marshmallow's Schema._init_fields to dynamically add missing auto-generated fields as Raw fields
  • Updates all marshmallow schemas to follow 4.x conventions (validator signatures, field parameters, metadata usage)
  • Upgrades marshmallow to >=4.0.0 and marshmallow-sqlalchemy to 1.4.2

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
superset/marshmallow_compatibility.py New module that patches marshmallow to handle Flask-AppBuilder compatibility by dynamically adding missing fields
superset/app.py Applies the marshmallow compatibility patch during application initialization
tests/unit_tests/test_marshmallow_compatibility.py Comprehensive unit tests for the compatibility module
superset/themes/schemas.py Migrates validators to accept **kwargs parameter and replaces self.context with ContextVar
superset/reports/schemas.py Updates validator signatures and replaces default with load_default
superset/models/helpers.py Updates validator signature to include **kwargs
superset/databases/schemas.py Updates validator signature to include **kwargs
superset/charts/schemas.py Migrates field parameters: description to metadata, minLength to validate=Length(), min to validate=Range()
superset/db_engine_specs/databend.py Migrates description to metadata and default to dump_default
superset/advanced_data_type/schemas.py Updates JSON schema with dump_default (needs correction)
pyproject.toml Updates marshmallow dependency from >=3.0, <4 to >=4
requirements/base.in Updates marshmallow-sqlalchemy from <1.4.1 to >=1.4.2
requirements/base.txt Locks marshmallow at 4.0.0 and marshmallow-sqlalchemy at 1.4.2
requirements/development.txt Locks marshmallow at 4.0.0 and marshmallow-sqlalchemy at 1.4.2

Comment thread superset/themes/schemas.py
Comment thread superset/themes/schemas.py
Comment thread superset/marshmallow_compatibility.py Outdated
Comment thread superset/marshmallow_compatibility.py Outdated
Comment thread superset/marshmallow_compatibility.py Outdated
Comment thread superset/themes/schemas.py
Comment thread superset/advanced_data_type/schemas.py Outdated
Comment thread superset/advanced_data_type/schemas.py Outdated
@kevin-lann

Copy link
Copy Markdown
Contributor Author

@rusackas The tests/checks should be all fixed now (hopefully). Could you rerun once again?

@dpgaspar
dpgaspar self-requested a review November 24, 2025 20:44
@codecov

codecov Bot commented Nov 28, 2025 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.66667% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.48%. Comparing base (a90c8e0) to head (1c04644).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
superset/themes/schemas.py 87.50% 3 Missing ⚠️
superset/marshmallow_compatibility.py 83.33% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master   #35920   +/-   ##
=======================================
  Coverage   64.48%   64.48%           
=======================================
  Files        2661     2661           
  Lines      145642   145665   +23     
  Branches    33613    33613           
=======================================
+ Hits        93912    93930   +18     
- Misses      50029    50032    +3     
- Partials     1701     1703    +2     
Flag Coverage Δ
hive 39.24% <46.66%> (+<0.01%) ⬆️
mysql 58.01% <86.66%> (+0.01%) ⬆️
postgres 58.08% <86.66%> (+<0.01%) ⬆️
presto 40.82% <46.66%> (+<0.01%) ⬆️
python 59.51% <86.66%> (+<0.01%) ⬆️
sqlite 57.73% <86.66%> (+0.01%) ⬆️
unit 100.00% <ø> (ø)

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.

@kevin-lann

kevin-lann commented Nov 28, 2025 •

Copy link
Copy Markdown
Contributor Author

The above commit should fix the remaining checks 33c0814

Screenshot 2025-11-28 at 4 39 25 PM

Comment thread superset/charts/schemas.py Outdated
Comment thread superset/marshmallow_compatibility.py Outdated
Comment thread superset/themes/schemas.py
Comment thread superset/themes/schemas.py

@bito-code-review bito-code-review 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.

Code Review Agent Run #c3fd87

Actionable Suggestions - 3
  • tests/unit_tests/test_marshmallow_compatibility.py - 3
Additional Suggestions - 2
  • superset/marshmallow_compatibility.py - 2
    • Private member access violation · Line 49-49
      Accessing private member `_init_fields` (lines 49 and 92) violates encapsulation. Consider if this is necessary or if a public API alternative exists. Note: Similar issue exists at line 92.
      Code suggestion
       @@ -48,6 +48,7 @@
            import marshmallow
       
            # Store the original method
      +    # Note: Accessing private member is necessary for Flask-AppBuilder 5.0.0 compatibility
            original_init_fields = marshmallow.Schema._init_fields
    • Try-except block in loop · Line 56-56
      The `try`-`except` block inside the `while` loop (lines 56-85) incurs performance overhead. Consider restructuring to avoid exception handling in loops.
Review Details
  • Files reviewed - 12 · Commit Range: f220397..e2e5d09
    • pyproject.toml
    • requirements/base.in
    • requirements/base.txt
    • requirements/development.txt
    • superset/app.py
    • superset/charts/schemas.py
    • superset/databases/schemas.py
    • superset/marshmallow_compatibility.py
    • superset/models/helpers.py
    • superset/reports/schemas.py
    • superset/themes/schemas.py
    • tests/unit_tests/test_marshmallow_compatibility.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

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

  • /review - 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 evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

Comment thread tests/unit_tests/test_marshmallow_compatibility.py
@rusackas

Copy link
Copy Markdown
Member

@kevin-lann running CI 🤞

Still some (reasonable sounding) bot feedback if you're able to address or respond to it.

@rusackas

rusackas commented Jun 1, 2026

Copy link
Copy Markdown
Member

@kevin-lann let us know if you're able to assess and apply/debunk the bot comments, give this a rebase to resolve conflicts, and get pre-commit to pass. We'd love to merge it.

@kevin-lann

Copy link
Copy Markdown
Contributor Author

@rusackas hey, sounds great, will look at shortly. Been away from this for a bit and missed your previous message, my bad!

@netlify

netlify Bot commented Jun 1, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit 7e20aa4
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/6a1e20e35019000008446656
😎 Deploy Preview https://deploy-preview-35920--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.

@kevin-lann

kevin-lann commented Jun 2, 2026 •

Copy link
Copy Markdown
Contributor Author

@rusackas Cleaned up all the leftover bits and pieces.

  • Merged in master, resolved conflicts
  • Addressed bot reviews
  • Fixed pre-commit check (ruff was failing) ff53d44

The previous CI run also had unit tests failing but seems like it was due to it being cancelled so I assume this is not an issue.
Screenshot 2026-06-01 at 8 18 50 PM

Let me know if there's anything else with this you'd like addressed

@bito-code-review

bito-code-review Bot commented Jun 2, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Agent Run #dd1af0

Actionable Suggestions - 0
Filtered by Review Rules

Bito filtered these suggestions based on rules created automatically for your feedback. Manage rules.

  • tests/unit_tests/test_marshmallow_compatibility.py - 1
  • superset/charts/schemas.py - 1
Review Details
  • Files reviewed - 5 · Commit Range: e2e5d09..7e20aa4
    • tests/unit_tests/test_marshmallow_compatibility.py
    • superset/charts/schemas.py
    • superset/marshmallow_compatibility.py
    • superset/themes/schemas.py
    • tests/unit_tests/themes/api_test.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

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

  • /review - 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 evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

@rusackas

Copy link
Copy Markdown
Member

@kevin-lann this looks in good shape after your cleanup — just conflicting now. I'll rebase your branch to clear it, then ping @dpgaspar and @Antonio-RiveroMartnez to review the marshmallow 4.x compat layer.

@kevin-lann
kevin-lann force-pushed the marshmallow-upgrade-v4 branch from 7e20aa4 to 5258a00 Compare June 25, 2026 14:44
@kevin-lann

Copy link
Copy Markdown
Contributor Author

@rusackas hey - appreciate it. Just rebased with master actually, conflicts are gone.

@bito-code-review

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

Copy link
Copy Markdown
Contributor

Code Review Agent Run #710057

Actionable Suggestions - 0
Review Details
  • Files reviewed - 5 · Commit Range: 40f5fdf..1c04644
    • superset/marshmallow_compatibility.py
    • superset/models/helpers.py
    • superset/themes/schemas.py
    • tests/unit_tests/test_marshmallow_compatibility.py
    • tests/unit_tests/themes/api_test.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

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

  • /review - 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 evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

@rusackas rusackas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kevin-lann, LGTM. Swapping the mm4-removed Schema.context write-back for a ContextVar + @post_load is the right shape, and since field validators run before post_load the sanitized value lands correctly. Nice that it also clears the latent AttributeError the old self.context[...] path would hit under mm4.

Mind updating the PR description though? It still points at a marshmallow_fix.py/app.py change that isn't in this diff.

@kevin-lann

Copy link
Copy Markdown
Contributor Author

Of course, updated. Appreciate the look over @rusackas 🙏

@rusackas
rusackas merged commit e65b2c4 into apache:master Jul 8, 2026
66 of 68 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:backend Requires changing the backend size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants