Skip to content
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

fix: JSON schema union types are no longer conformed into boolean values #2723

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion singer_sdk/helpers/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,17 @@ def is_boolean_type(property_schema: dict) -> bool | None:
return False


def _is_exclusive_boolean_type(property_schema: dict) -> bool:
if "type" not in property_schema:
return False

return (
property_schema["type"] == "boolean"
or property_schema["type"] == ["boolean"]
or set(property_schema["type"]) == {"boolean", "null"}
)


def is_integer_type(property_schema: dict) -> bool | None:
"""Return true if the JSON Schema type is an integer or None if detection fails."""
if "anyOf" not in property_schema and "type" not in property_schema:
Expand Down Expand Up @@ -523,6 +534,6 @@ def _conform_primitive_property( # noqa: PLR0911
if isinstance(elem, bytes):
# for BIT value, treat 0 as False and anything else as True
return elem != b"\x00" if is_boolean_type(property_schema) else elem.hex()
if is_boolean_type(property_schema):
if _is_exclusive_boolean_type(property_schema):
return None if elem is None else elem != 0
return elem
9 changes: 9 additions & 0 deletions tests/core/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,16 @@ def test_conform_primitives():

assert _conform_primitive_property(None, {"type": "boolean"}) is None
assert _conform_primitive_property(0, {"type": "boolean"}) is False
assert (
_conform_primitive_property(
0, {"anyOf": [{"type": "boolean"}, {"type": "integer"}]}
)
== 0
)
assert _conform_primitive_property(0, {"type": ["boolean", "integer"]}) == 0
assert _conform_primitive_property(1, {"type": "boolean"}) is True
assert _conform_primitive_property(1, {"type": ["boolean", "null"]}) is True
assert _conform_primitive_property(1, {"type": ["boolean"]}) is True


@pytest.mark.parametrize(
Expand Down
Loading