|
26 | 26 | from sortedcontainers import SortedSet |
27 | 27 |
|
28 | 28 | from .._internal.compare import ComparableTuple as _ComparableTuple |
29 | | -from ..exception.serialization import CycloneDxDeserializationException |
30 | 29 | from ..schema import SchemaVersion |
31 | 30 | from ..schema.schema import SchemaVersion1Dot4, SchemaVersion1Dot5, SchemaVersion1Dot6 |
32 | 31 | from . import ExternalReference, HashType, _HashTypeRepositorySerializationHelper |
@@ -308,7 +307,8 @@ def json_denormalize(cls, o: Union[List[Dict[str, Any]], Dict[str, Any]], |
308 | 307 | services = map(lambda s: Service.from_json( # type:ignore[attr-defined] |
309 | 308 | s), o.get('services', ())) |
310 | 309 | elif isinstance(o, Iterable): |
311 | | - tools = map(lambda t: Tool.from_json(t), o) # type:ignore[attr-defined] |
| 310 | + tools = map(lambda t: Tool.from_json( # type:ignore[attr-defined] |
| 311 | + t), o) |
312 | 312 | return ToolRepository(components=components, services=services, tools=tools) |
313 | 313 |
|
314 | 314 | @classmethod |
@@ -349,23 +349,19 @@ def xml_denormalize(cls, o: Element, *, |
349 | 349 | prop_info: 'ObjectMetadataLibrary.SerializableProperty', |
350 | 350 | ctx: Type[Any], |
351 | 351 | **kwargs: Any) -> ToolRepository: |
352 | | - tools = [] |
| 352 | + ns_map = {'bom': default_ns or ''} |
| 353 | + # Do not iterate over `o` and do not check for expected `.tag` of items. |
| 354 | + # This check could have been done by schema validators before even deserializing. |
| 355 | + tools = None |
353 | 356 | components = None |
354 | 357 | services = None |
355 | | - for e in o: |
356 | | - tag = e.tag if default_ns is None else e.tag.replace(f'{{{default_ns}}}', '') |
357 | | - if tag == 'tool': |
358 | | - tools.append(Tool.from_xml( # type:ignore[attr-defined] |
359 | | - e, default_ns)) |
360 | | - elif tag == 'components': |
361 | | - components = map(lambda s: Component.from_xml( # type:ignore[attr-defined] |
362 | | - s, default_ns), e) |
363 | | - elif tag == 'services': |
364 | | - services = map(lambda s: Service.from_xml( # type:ignore[attr-defined] |
365 | | - s, default_ns), e) |
366 | | - else: |
367 | | - raise CycloneDxDeserializationException(f'unexpected: {e!r}') |
368 | | - return ToolRepository( |
369 | | - tools=tools, |
370 | | - components=components, |
371 | | - services=services) |
| 358 | + ts = o.findall('bom:tool', ns_map) |
| 359 | + if len(ts) > 0: |
| 360 | + tools = map(lambda t: Tool.from_xml( # type:ignore[attr-defined] |
| 361 | + t, default_ns), ts) |
| 362 | + else: |
| 363 | + components = map(lambda c: Component.from_xml( # type:ignore[attr-defined] |
| 364 | + c, default_ns), o.iterfind('./bom:components/bom:component', ns_map)) |
| 365 | + services = map(lambda s: Service.from_xml( # type:ignore[attr-defined] |
| 366 | + s, default_ns), o.iterfind('./bom:services/bom:service', ns_map)) |
| 367 | + return ToolRepository(components=components, services=services, tools=tools) |
0 commit comments