diff --git a/src/py_avro_schema/_schemas.py b/src/py_avro_schema/_schemas.py index e5c07d9..8ecd499 100644 --- a/src/py_avro_schema/_schemas.py +++ b/src/py_avro_schema/_schemas.py @@ -405,13 +405,16 @@ def handles_type(cls, py_type: Type) -> bool: @register_schema -class DictAsJSONSchema(Schema): - """An Avro string schema representing a Python Dict[str, Any] or List[Dict[str, Any]] assuming JSON serialization""" +class TypeAsJSONSchema(Schema): + """ + An Avro string schema representing a Python Dict[str, Any], List[Dict[str, Any]] or List[Any] assuming + JSON serialization + """ @classmethod def handles_type(cls, py_type: Type) -> bool: """Whether this schema class can represent a given Python class""" - return _is_dict_str_any(py_type) or _is_list_dict_str_any(py_type) + return is_logically_json(py_type) def data(self, names: NamesType) -> JSONObj: """Return the schema data""" @@ -1280,6 +1283,17 @@ def _is_list_dict_str_any(py_type: Type) -> bool: return False +def _is_list_any(py_type: Type) -> bool: + """Return whether a given type is ``List[Any]``""" + origin = get_origin(py_type) + return inspect.isclass(origin) and issubclass(origin, list) and get_args(py_type) == (Any,) + + +def is_logically_json(py_type: Type) -> bool: + """Returns whether a given type is logically a JSON and can be serialized as such""" + return _is_list_any(py_type) or _is_list_dict_str_any(py_type) or _is_dict_str_any(py_type) + + def _is_class(py_type: Any, of_types: Union[Type, Tuple[Type, ...]], include_subclasses: bool = True) -> bool: """Return whether the given type is a (sub) class of a type or types""" py_type = _type_from_annotated(py_type) diff --git a/tests/test_logicals.py b/tests/test_logicals.py index 71d9fe4..67a10e0 100644 --- a/tests/test_logicals.py +++ b/tests/test_logicals.py @@ -285,3 +285,9 @@ def test_list_json_logical_bytes_field(): "logicalType": "json", } assert_schema(py_type, expected) + + +def test_list_json_logical_list_any(): + py_type = List[Any] + expected = {"type": "bytes", "logicalType": "json"} + assert_schema(py_type, expected)