Fix bugs in json_utils.py: logic errors, unpacking crashes, and mangl… #37589
+29
−6
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.
Fix json_utils.py object validation logic and mangled error messages
This PR addresses critical bugs in Apache Beam's YAML JSON utility that led to silent validation passes or internal crashes when handling object-type schemas.
Changes: Corrected Object Validation: Fixed logic in _validate_compatible where weak_schema == 'object' was always false (comparing dict to string). It now correctly checks weak_schema['type'] == 'object'.
Fixed Iteration Crash: Added .items() to dictionary iteration in _validate_compatible to prevent ValueError: not enough values to unpack. Restored F-Strings: Added missing f prefixes to multiple ValueError and TypeError calls, ensuring error messages like Expected object type, got {json_type} correctly interpolate variables.
Fixed Formatting Bug: Corrected a ValueError in _validate_compatible
where a boolean expression was passed to a format string expecting the type names.
Added Regression Tests: Updated json_utils_test.py with tests verifying that incompatible object schemas are caught and that error messages are properly formatted.
unit tests implemented.
fixes #37576