@@ -182,7 +182,6 @@ def json_normalize(cls, o: LifecycleRepository, *,
182182 ** __ : Any ) -> Any :
183183 if len (o ) == 0 :
184184 return None
185-
186185 return [json_loads (li .as_json ( # type:ignore[union-attr]
187186 view_ = view )) for li in o ]
188187
@@ -192,12 +191,13 @@ def json_denormalize(cls, o: List[Dict[str, Any]],
192191 repo = LifecycleRepository ()
193192 for li in o :
194193 if 'phase' in li :
195- repo .add (PredefinedLifecycle .from_json (li )) # type:ignore[attr-defined]
194+ repo .add (PredefinedLifecycle .from_json ( # type:ignore[attr-defined]
195+ li ))
196196 elif 'name' in li :
197- repo .add (NamedLifecycle .from_json (li )) # type:ignore[attr-defined]
197+ repo .add (NamedLifecycle .from_json ( # type:ignore[attr-defined]
198+ li ))
198199 else :
199200 raise CycloneDxDeserializationException (f'unexpected: { li !r} ' )
200-
201201 return repo
202202
203203 @classmethod
@@ -208,33 +208,27 @@ def xml_normalize(cls, o: LifecycleRepository, *,
208208 ** __ : Any ) -> Optional [Element ]:
209209 if len (o ) == 0 :
210210 return None
211-
212211 elem = Element (element_name )
213212 for li in o :
214213 elem .append (li .as_xml ( # type:ignore[union-attr]
215214 view_ = view , as_string = False , element_name = 'lifecycle' , xmlns = xmlns ))
216-
217215 return elem
218216
219217 @classmethod
220218 def xml_denormalize (cls , o : Element ,
221219 default_ns : Optional [str ],
222220 ** __ : Any ) -> LifecycleRepository :
223221 repo = LifecycleRepository ()
224-
225- for li in o :
226- tag = li .tag if default_ns is None else li .tag .replace (f'{{{ default_ns } }}' , '' )
227-
228- if tag == 'lifecycle' :
229- stages = list (li )
230-
231- predefined_lifecycle = next ((el for el in stages if 'phase' in el .tag ), None )
232- named_lifecycle = next ((el for el in stages if 'name' in el .tag ), None )
233- if predefined_lifecycle is not None :
234- repo .add (PredefinedLifecycle .from_xml (li , default_ns )) # type:ignore[attr-defined]
235- elif named_lifecycle is not None :
236- repo .add (NamedLifecycle .from_xml (li , default_ns )) # type:ignore[attr-defined]
222+ ns_map = {'bom' : default_ns or '' }
223+ # Do not iterate over `o` and do not check for expected `.tag` of items.
224+ # This check could have been done by schema validators before even deserializing.
225+ for li in o .iterfind ('bom:lifecycle' , ns_map ):
226+ if li .find ('bom:phase' , ns_map ) is not None :
227+ repo .add (PredefinedLifecycle .from_xml ( # type:ignore[attr-defined]
228+ li , default_ns ))
229+ elif li .find ('bom:name' , ns_map ) is not None :
230+ repo .add (NamedLifecycle .from_xml ( # type:ignore[attr-defined]
231+ li , default_ns ))
237232 else :
238- raise CycloneDxDeserializationException (f'unexpected: { li !r} ' )
239-
233+ raise CycloneDxDeserializationException (f'unexpected content: { li !r} ' )
240234 return repo
0 commit comments