Repository: tefra/xsdata
Pull request #750 (2023-02-21 14:34:58)

feat: Ignore malformed json files in transformer
## 📒 Description

> Write a brief description of your PR.


Resolves #733 

## 🔗 What I've Done

> Write a description of the steps taken to resolve the issue


## 💬 Comments

> A place to write any comments to the reviewer.
>

## 🛫 Checklist

- [ ] Updated docs
- [ ] Added unit-tests
- [ ] [Sample tests](https://github.com/tefra/xsdata-samples) pass
- [ ] [W3C tests](https://github.com/tefra/xsdata-w3c-tests) pass



Patch:
diff --git a/xsdata/codegen/transformer.py b/xsdata/codegen/transformer.py
index b5c36f73..0ecbf951 100644
--- a/xsdata/codegen/transformer.py
+++ b/xsdata/codegen/transformer.py
@@ -174,13 +174,16 @@ class SchemaTransformer:
         for uri in uris:
             input_stream = self.load_resource(uri)
             if input_stream:
-                data = json.load(io.BytesIO(input_stream))
-                logger.info("Parsing document %s", uri)
-                if isinstance(data, dict):
-                    data = [data]
-
-                for obj in data:
-                    classes.extend(DictMapper.map(obj, name, dirname))
+                try:
+                    data = json.load(io.BytesIO(input_stream))
+                    logger.info("Parsing document %s", uri)
+                    if isinstance(data, dict):
+                        data = [data]
+
+                    for obj in data:
+                        classes.extend(DictMapper.map(obj, name, dirname))
+                except ValueError as exc:
+                    logger.warning("JSON load failed for file: %s", uri, exc_info=exc)
 
         self.classes.extend(ClassUtils.reduce_classes(classes))
 


Test patch:
diff --git a/tests/codegen/test_transformer.py b/tests/codegen/test_transformer.py
index a9bbd2a9..6a4202fd 100644
--- a/tests/codegen/test_transformer.py
+++ b/tests/codegen/test_transformer.py
@@ -133,14 +133,15 @@ class SchemaTransformerTests(FactoryTestCase):
         mock_map.assert_has_calls([mock.call(x, "foo") for x in elements])
         mock_reduce_classes.assert_called_once_with(classes_a + classes_c)
 
+    @mock.patch("xsdata.codegen.transformer.logger.warning")
     @mock.patch.object(ClassUtils, "reduce_classes")
     @mock.patch.object(DictMapper, "map")
     @mock.patch.object(SchemaTransformer, "load_resource")
     def test_process_json_documents(
-        self, mock_load_resource, mock_map, mock_reduce_classes
+        self, mock_load_resource, mock_map, mock_reduce_classes, mock_warning
     ):
-        uris = ["foo/a.json", "foo/b.json", "foo/c.json"]
-        resources = [b'{"foo": 1}', None, b'[{"foo": true}]']
+        uris = ["foo/a.json", "foo/b.json", "foo/c.json", "bar.json"]
+        resources = [b'{"foo": 1}', None, b'[{"foo": true}]', b"notjson"]
 
         classes_a = ClassFactory.list(2)
         classes_c = ClassFactory.list(3)
@@ -161,6 +162,9 @@ class SchemaTransformerTests(FactoryTestCase):
             ]
         )
         mock_reduce_classes.assert_called_once_with(classes_a + classes_c)
+        mock_warning.assert_called_once_with(
+            "JSON load failed for file: %s", uris[3], exc_info=mock.ANY
+        )
 
     @mock.patch.object(DtdMapper, "map")
     @mock.patch.object(DtdParser, "parse")