Skip to content

Commit

Permalink
fix: JSON schema union types are no longer conformed into boolean val…
Browse files Browse the repository at this point in the history
…ues (#2723)

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

* Do conform the most obvious cases

* Make sure it is tested

* Test one more case
  • Loading branch information
edgarrmondragon authored Oct 16, 2024
1 parent 48fc646 commit 925664b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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

0 comments on commit 925664b

Please sign in to comment.