-
Notifications
You must be signed in to change notification settings - Fork 16.4k
fix(marshmallow): add compatibility layer for Flask-AppBuilder with marshmallow 4.x #35920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kevin-lann
wants to merge
19
commits into
apache:master
Choose a base branch
from
kevin-lann:marshmallow-upgrade-v4
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f220397
My Local Setup (Revert this commit later)
Austin-X 663922c
Upgrade to Marshmallow >= 4 with Superset able to be booted up and ab…
Austin-X 7b4f545
Revert "My Local Setup (Revert this commit later)"
Austin-X c841166
Reset feature flags back to their original status
Austin-X bc755de
Apply the compatibility patch to address incompatibilities between Fl…
Austin-X bf9fc91
Revert initialization\__init__.py to its initial state
Austin-X dd9186a
Fix whitespace in superset\initialization\__init__.py
Austin-X 2a4339a
Add unit tests
Austin-X 72fd0b6
Rename "marshmallow_fix" to "marshmallow_compatibility"
Austin-X ca17fbb
Move patch fn to app.py
kevin-lann c0fe5d4
empty
kevin-lann fd7d995
Fix validates decorator
kevin-lann 84cddb3
Apply JSON Schema Change
Eyang0612 3fcc002
Style: Add Typing + Remove Redundancy in Code Logic
Eyang0612 e586e16
Allow optional fields to load as None
kevin-lann 4fe8938
Apply some formatting changes (e.g. make all lines <= 88 chars long, …
Austin-X 5ab4b7a
Merge pull request #2 from kevin-lann/feature/marshmallow-upgrade-cle…
Eyang0612 2e6a6d6
Fix ruff and mympy checks
kevin-lann 33c0814
Merge pull request #3 from kevin-lann/feature/marshmallow-upgrade-cle…
kevin-lann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """ | ||
| Marshmallow 4.x Compatibility Module for Flask-AppBuilder 5.0.0 | ||
|
|
||
| This module provides compatibility between Flask-AppBuilder 5.0.0 and | ||
| marshmallow 4.x, specifically handling missing auto-generated fields | ||
| during schema initialization. | ||
| """ | ||
|
|
||
| import logging | ||
| from typing import Any, TYPE_CHECKING | ||
|
|
||
| from marshmallow import fields | ||
|
|
||
| if TYPE_CHECKING: | ||
| import marshmallow | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def patch_marshmallow_for_flask_appbuilder() -> None: | ||
| """ | ||
| Patches marshmallow Schema._init_fields to handle Flask-AppBuilder 5.0.0 | ||
| compatibility with marshmallow 4.x. | ||
|
|
||
| Flask-AppBuilder 5.0.0 automatically generates schema fields that reference | ||
| SQL relationship fields that may not exist in marshmallow 4.x's stricter | ||
| field validation. This patch dynamically adds missing fields as Raw fields | ||
| to prevent KeyError exceptions during schema initialization. | ||
| """ | ||
| import marshmallow | ||
|
|
||
| # Store the original method | ||
| original_init_fields = marshmallow.Schema._init_fields | ||
|
|
||
| def patched_init_fields(self: "marshmallow.Schema") -> Any: | ||
| """Patched version that handles missing declared fields.""" | ||
| max_retries = 10 # Prevent infinite loops in case of unexpected errors | ||
| retries = 0 | ||
|
|
||
| while retries < max_retries: | ||
| try: | ||
| return original_init_fields(self) | ||
| except KeyError as e: | ||
| # Extract the missing field name from the KeyError | ||
| missing_field = str(e).strip("'\"") | ||
|
|
||
| # Initialize declared_fields if it doesn't exist | ||
| if not hasattr(self, "declared_fields"): | ||
| self.declared_fields = {} | ||
|
|
||
| # Only add if it doesn't already exist | ||
| if missing_field not in self.declared_fields: | ||
| # Use Raw field as a safe fallback for unknown auto-generated | ||
| # fields. Allow both load and dump to support both input | ||
| # validation and serialization | ||
| self.declared_fields[missing_field] = fields.Raw( | ||
| allow_none=True, | ||
| load_default=None, # Optional field (defaults to None) | ||
| ) | ||
|
|
||
| logger.debug( | ||
| "Marshmallow compatibility: Added missing field " | ||
| "'%s' as Raw field", | ||
| missing_field, | ||
| ) | ||
|
|
||
| retries += 1 | ||
| # Continue the loop to retry initialization | ||
|
|
||
| # If we've exhausted retries, something is seriously wrong | ||
| raise RuntimeError( | ||
| f"Marshmallow field initialization failed after {max_retries} retries" | ||
| ) | ||
|
|
||
| # Apply the patch | ||
| marshmallow.Schema._init_fields = patched_init_fields |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.