Skip to content

Comments

Add marshmallow 4.x support with backward compatibility#326

Merged
gtmanfred merged 11 commits intoplangrid:masterfrom
mockodin:mashmallow_4x_support
Feb 2, 2026
Merged

Add marshmallow 4.x support with backward compatibility#326
gtmanfred merged 11 commits intoplangrid:masterfrom
mockodin:mashmallow_4x_support

Conversation

@mockodin
Copy link
Contributor

@mockodin mockodin commented Jan 24, 2026

Summary

This PR adds support for marshmallow 4.x while maintaining backward compatibility with marshmallow 3.x.

Changes

Core Library

  • flask_rebar/compat.py: Added \MARSHMALLOW_VERSION_MAJOR\ constant and \is_schema_ordered()\ helper function to handle the removal of the \ordered\ attribute in marshmallow 4.x
  • flask_rebar/swagger_generation/marshmallow_to_swagger.py: Updated \get_schema_fields()\ and \get_required()\ to use the new \is_schema_ordered()\ helper
  • setup.py: Updated marshmallow version requirement from <4\ to <5\

Test Fixes

  • tests/test_rebar.py: Made marshmallow-objects tests conditional since the library is archived and incompatible with marshmallow 4.x
  • tests/swagger_generation/test_marshmallow_to_swagger.py:
    • Added skipif decorator for tests using 'self'\ nested reference (removed in 4.x)
    • Compare required fields as sets to handle ordering differences
    • Import \MARSHMALLOW_VERSION_MAJOR\ from compat module
  • tests/test_validation.py: Relaxed error message assertion to handle format differences between versions

CI

  • .github/workflows/pullrequests.yml: Added marshmallow 4.0.0, 4.0.1, and latest 4.x to the test matrix

Testing

  • All 122 tests pass with marshmallow 4.x (1 skipped - marshmallow-objects)
  • All 133 tests pass with marshmallow 3.x
  • Verified cross-platform compatibility via Docker (Linux)

Breaking Changes in Marshmallow 4.x Addressed

  1. \Schema.ordered\ attribute removed Added \is_schema_ordered()\ helper
  2. 'self'\ nested reference removed Skip affected tests on 4.x
  3. \marshmallow-objects\ incompatible Made tests conditional

- Add MARSHMALLOW_VERSION_MAJOR constant for version detection
- Add is_schema_ordered() helper to handle removed 'ordered' attribute
- Update get_schema_fields() and get_required() to use is_schema_ordered()
- Bump marshmallow version constraint from <4 to <5

In marshmallow 4.x, the 'ordered' Meta option was removed because
field order is now always preserved (Python 3.7+ dict ordering).
The is_schema_ordered() helper abstracts this difference:
- 3.x: checks schema.ordered attribute (default False = sort fields)
- 4.x: always returns True (fields preserve insertion order)
marshmallow-objects is an archived library incompatible with marshmallow
4.x (passes 'context' to Schema.__init__() which was removed).

- Import marshmallow_objects conditionally with try/except
- Test instantiation to detect runtime incompatibility
- Define MARSHMALLOW_OBJECTS_AVAILABLE flag
- Move Model class definitions inside conditional block
- Create conditional test case lists for parametrized tests

Users on marshmallow 3.x can still use marshmallow-objects.
- Remove 'parametrize' import causing source reading issues
- Split test_additional_properties into 4 individual tests
- Add skipif for test_self_referential_nested_pre_3_3 (uses removed
  'self' nested reference in 4.x)
- Compare 'required' field as set since order varies between versions
- Add MARSHMALLOW_VERSION constant for version-specific behavior
Error message format for datetime validation changed between versions.
Check for 'isoformat' substring rather than exact message match.
Test against:
- marshmallow==4.0.0
- marshmallow==4.0.1
- marshmallow>4.0.1 (latest)
@mockodin mockodin force-pushed the mashmallow_4x_support branch from c1dfbdf to 21206a6 Compare January 24, 2026 04:03
@mockodin mockodin mentioned this pull request Jan 24, 2026
@gtmanfred
Copy link
Contributor

Howdy, thanks for opening this. I will try and get a review in before the end of the week.

@gtmanfred gtmanfred added the enhancement New feature or request label Jan 26, 2026
gtmanfred and others added 3 commits January 29, 2026 15:48
To avoid pip install marshmallow>4 piping the output of `pip install marshmallow` to a file called `4`.
gtmanfred and others added 3 commits February 2, 2026 11:59
Co-authored-by: Michael Franklin <michael@illusional.net>
@gtmanfred gtmanfred enabled auto-merge (rebase) February 2, 2026 21:14
@gtmanfred gtmanfred merged commit d6e5bd0 into plangrid:master Feb 2, 2026
55 of 147 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants