Repository navigation
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
40f5fdf
Upgrade to Marshmallow >= 4 with Superset able to be booted up and ab…
Austin-X 416ba17
Apply the compatibility patch to address incompatibilities between Fl…
Austin-X 4194c43
Add unit tests
Austin-X 6130614
Rename "marshmallow_fix" to "marshmallow_compatibility"
Austin-X 0d0b541
Move patch fn to app.py
kevin-lann 501d258
empty
kevin-lann 5b67792
Fix validates decorator
kevin-lann 04c6bf2
Apply JSON Schema Change
Eyang0612 0b07fe3
Allow optional fields to load as None
kevin-lann 9995940
Fix ruff
kevin-lann 5258a00
address bot review
kevin-lann a1d81d0
remove duplicate patch call in app.py
kevin-lann 1c04644
fix unit test
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| # 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. | ||
|
|
||
| """Unit tests for the marshmallow compatibility shim.""" | ||
|
|
||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
| from marshmallow import fields, Schema | ||
|
|
||
| from superset.marshmallow_compatibility import patch_marshmallow_for_flask_appbuilder | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def restore_schema_init_fields(): | ||
| """Restore Schema._init_fields after each test.""" | ||
| original_method = Schema._init_fields | ||
| try: | ||
| yield | ||
| finally: | ||
| Schema._init_fields = original_method | ||
|
|
||
|
|
||
| class TestMarshmallowCompatibility: | ||
| """Test cases for the marshmallow 4.x compatibility module.""" | ||
|
|
||
| def test_patch_marshmallow_for_flask_appbuilder_applies_patch(self): | ||
| """Test that the patch function correctly replaces Schema._init_fields.""" | ||
| original_method = Schema._init_fields | ||
|
|
||
| patch_marshmallow_for_flask_appbuilder() | ||
|
|
||
| assert callable(Schema._init_fields) | ||
| assert getattr(Schema._init_fields, "__superset_compat_patched__", False) | ||
| if not getattr(original_method, "__superset_compat_patched__", False): | ||
| assert Schema._init_fields != original_method | ||
|
|
||
| def test_patch_functionality_with_real_schema_creation(self): | ||
| """Test that the patch works with actual schema creation scenarios.""" | ||
| patch_marshmallow_for_flask_appbuilder() | ||
|
|
||
| class TestSchema(Schema): | ||
| name = fields.Str() | ||
| age = fields.Int() | ||
|
|
||
| schema = TestSchema() | ||
| assert "name" in schema.declared_fields | ||
| assert "age" in schema.declared_fields | ||
| assert isinstance(schema.declared_fields["name"], fields.Str) | ||
| assert isinstance(schema.declared_fields["age"], fields.Int) | ||
|
|
||
| def test_patch_handles_schema_with_no_fields(self): | ||
| """Test that the patch works with schemas that have no declared fields.""" | ||
| patch_marshmallow_for_flask_appbuilder() | ||
|
|
||
| class EmptySchema(Schema): | ||
| pass | ||
|
|
||
| schema = EmptySchema() | ||
| assert hasattr(schema, "declared_fields") | ||
|
|
||
| def test_patch_pre_stubs_missing_fab_fields_before_initialization(self): | ||
| """Test that FAB-looking missing fields are stubbed before init runs.""" | ||
|
|
||
| def fake_init_fields(self: Schema): | ||
| assert "missing_field" in self.declared_fields | ||
| return "initialized" | ||
|
|
||
| with patch.object(Schema, "_init_fields", fake_init_fields): | ||
| patch_marshmallow_for_flask_appbuilder() | ||
|
|
||
| schema = object.__new__(Schema) | ||
| schema.declared_fields = {} | ||
| schema.opts = type( | ||
| "Opts", | ||
| (), | ||
| {"fields": ("missing_field",), "additional": ()}, | ||
| )() | ||
|
|
||
| result = Schema._init_fields(schema) | ||
|
|
||
| assert result == "initialized" | ||
| assert isinstance(schema.declared_fields["missing_field"], fields.Raw) | ||
| assert schema.declared_fields["missing_field"].allow_none is True | ||
| assert schema.declared_fields["missing_field"].load_default is None | ||
|
|
||
| def test_raw_field_creation_and_configuration(self): | ||
| """Test that Raw fields can be created with the expected configuration.""" | ||
| # Test creating a Raw field with our configuration | ||
| raw_field = fields.Raw(allow_none=True, load_default=None) | ||
|
|
||
| assert isinstance(raw_field, fields.Raw) | ||
| assert raw_field.allow_none is True | ||
| assert raw_field.load_default is None | ||
|
|
||
| def test_schema_declared_fields_manipulation(self): | ||
| """Test that we can manipulate schema declared_fields.""" | ||
|
|
||
| class TestSchema(Schema): | ||
| existing_field = fields.Str() | ||
|
|
||
| schema = TestSchema() | ||
|
|
||
| # Verify initial state | ||
| assert "existing_field" in schema.declared_fields | ||
| assert isinstance(schema.declared_fields["existing_field"], fields.Str) | ||
|
|
||
| # Test adding a new field | ||
| schema.declared_fields["new_field"] = fields.Raw( | ||
| allow_none=True, load_default=None | ||
| ) | ||
|
|
||
| # Verify the new field was added | ||
| assert "new_field" in schema.declared_fields | ||
| assert isinstance(schema.declared_fields["new_field"], fields.Raw) | ||
| assert schema.declared_fields["new_field"].allow_none is True | ||
| assert schema.declared_fields["new_field"].load_default is None | ||
|
|
||
| def test_flask_appbuilder_field_names_list(self): | ||
| """Test that we have the correct list of Flask-AppBuilder field names.""" | ||
| # Common Flask-AppBuilder auto-generated field names that our fix handles | ||
| expected_fab_fields = [ | ||
| "permission_id", | ||
| "view_menu_id", | ||
| "db_id", | ||
| "chart_id", | ||
| "dashboard_id", | ||
| "user_id", | ||
| ] | ||
|
|
||
| # Verify these are strings (field names) | ||
| for field_name in expected_fab_fields: | ||
| assert isinstance(field_name, str) | ||
| assert len(field_name) > 0 | ||
| assert "_id" in field_name | ||
|
|
||
| def test_patch_function_is_callable(self): | ||
| """Test that the patch function can be called without errors.""" | ||
| patch_marshmallow_for_flask_appbuilder() | ||
| patched_method = Schema._init_fields | ||
|
|
||
| patch_marshmallow_for_flask_appbuilder() | ||
| patch_marshmallow_for_flask_appbuilder() | ||
|
|
||
| assert Schema._init_fields is patched_method | ||
|
|
||
| def test_marshmallow_schema_basic_functionality(self): | ||
| """Test basic marshmallow schema functionality still works.""" | ||
|
|
||
| class UserSchema(Schema): | ||
| name = fields.Str(required=True) | ||
| email = fields.Email() | ||
| age = fields.Int(validate=lambda x: x > 0) | ||
|
|
||
| schema = UserSchema() | ||
|
|
||
| # Test serialization | ||
| data = {"name": "John Doe", "email": "john@example.com", "age": 30} | ||
| result = schema.load(data) | ||
| assert result["name"] == "John Doe" | ||
| assert result["email"] == "john@example.com" | ||
| assert result["age"] == 30 | ||
|
|
||
| from marshmallow import ValidationError | ||
|
|
||
| # Test validation - missing required field should raise error | ||
| with pytest.raises(ValidationError): | ||
| schema.load({"email": "john@example.com", "age": 30}) # Missing name | ||
|
kevin-lann marked this conversation as resolved.
|
||
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.
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.