Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cyclonedx/model/bom.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from .bom_ref import BomRef
from .component import Component
from .contact import OrganizationalContact, OrganizationalEntity
from .definition import Definitions
from .definition import Definitions, _DefinitionsSerializationHelper
from .dependency import Dependable, Dependency
from .license import License, LicenseExpression, LicenseRepository, _LicenseRepositorySerializationHelper
from .lifecycle import Lifecycle, LifecycleRepository, _LifecycleRepositoryHelper
Expand Down Expand Up @@ -680,6 +680,7 @@ def vulnerabilities(self, vulnerabilities: Iterable[Vulnerability]) -> None:
@property
@serializable.view(SchemaVersion1Dot6)
@serializable.view(SchemaVersion1Dot7)
@serializable.type_mapping(_DefinitionsSerializationHelper)
@serializable.xml_sequence(110)
def definitions(self) -> Optional[Definitions]:
"""
Expand All @@ -688,7 +689,7 @@ def definitions(self) -> Optional[Definitions]:
Returns:
`Definitions`
"""
return self._definitions if len(self._definitions.standards) > 0 else None
return self._definitions if self._definitions else None

@definitions.setter
def definitions(self, definitions: Definitions) -> None:
Expand Down
55 changes: 40 additions & 15 deletions cyclonedx/model/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
from .issue import IssueType
from .license import License, LicenseRepository, _LicenseRepositorySerializationHelper
from .model_card import ModelCard
from .patent import PatentAssertion
from .release_note import ReleaseNotes


Expand Down Expand Up @@ -1015,6 +1016,7 @@ def __init__(
swhids: Optional[Iterable[Swhid]] = None,
crypto_properties: Optional[CryptoProperties] = None,
tags: Optional[Iterable[str]] = None,
patent_assertions: Optional[Iterable[PatentAssertion]] = None,
# Deprecated in v1.6
author: Optional[str] = None,
) -> None:
Expand Down Expand Up @@ -1047,6 +1049,7 @@ def __init__(
self.model_card = model_card
self.crypto_properties = crypto_properties
self.tags = tags or []
self.patent_assertions = patent_assertions or []
# spec-deprecated properties below
self.author = author
self.modified = modified
Expand Down Expand Up @@ -1389,7 +1392,28 @@ def copyright(self, copyright: Optional[str]) -> None:
self._copyright = copyright

@property
@serializable.view(SchemaVersion1Dot7)
@serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'patentAssertion')
@serializable.xml_sequence(14)
def patent_assertions(self) -> 'SortedSet[PatentAssertion]':
"""
A list of assertions made regarding patents associated with this component.
Assertions distinguish between ownership, licensing, and other relevant interactions with patents.

.. note::
Introduced in CycloneDX v1.7

Returns:
Set of `PatentAssertion`
"""
return self._patent_assertions

@patent_assertions.setter
def patent_assertions(self, patent_assertions: Iterable[PatentAssertion]) -> None:
self._patent_assertions = SortedSet(patent_assertions)

@property
@serializable.xml_sequence(15)
def cpe(self) -> Optional[str]:
"""
Specifies a well-formed CPE name that conforms to the CPE 2.2 or 2.3 specification.
Expand All @@ -1406,7 +1430,7 @@ def cpe(self, cpe: Optional[str]) -> None:

@property
@serializable.type_mapping(PackageUrlSH)
@serializable.xml_sequence(15)
@serializable.xml_sequence(16)
def purl(self) -> Optional[PackageURL]:
"""
Specifies the package-url (PURL).
Expand All @@ -1428,7 +1452,7 @@ def purl(self, purl: Optional[PackageURL]) -> None:
@serializable.view(SchemaVersion1Dot6)
@serializable.view(SchemaVersion1Dot7)
@serializable.xml_array(serializable.XmlArraySerializationType.FLAT, child_name='omniborId')
@serializable.xml_sequence(16)
@serializable.xml_sequence(17)
def omnibor_ids(self) -> 'SortedSet[OmniborId]':
"""
Specifies the OmniBOR Artifact ID. The OmniBOR, if specified, MUST be valid and conform to the specification
Expand All @@ -1449,7 +1473,7 @@ def omnibor_ids(self, omnibor_ids: Iterable[OmniborId]) -> None:
@serializable.view(SchemaVersion1Dot6)
@serializable.view(SchemaVersion1Dot7)
@serializable.xml_array(serializable.XmlArraySerializationType.FLAT, child_name='swhid')
@serializable.xml_sequence(17)
@serializable.xml_sequence(18)
def swhids(self) -> 'SortedSet[Swhid]':
"""
Specifies the Software Heritage persistent identifier (SWHID). The SWHID, if specified, MUST be valid and
Expand All @@ -1472,7 +1496,7 @@ def swhids(self, swhids: Iterable[Swhid]) -> None:
@serializable.view(SchemaVersion1Dot5)
@serializable.view(SchemaVersion1Dot6)
@serializable.view(SchemaVersion1Dot7)
@serializable.xml_sequence(18)
@serializable.xml_sequence(19)
def swid(self) -> Optional[Swid]:
"""
Specifies metadata and content for ISO-IEC 19770-2 Software Identification (SWID) Tags.
Expand All @@ -1488,7 +1512,7 @@ def swid(self, swid: Optional[Swid]) -> None:

@property
@serializable.view(SchemaVersion1Dot0) # todo: Deprecated in v1.3
@serializable.xml_sequence(19)
@serializable.xml_sequence(20)
def modified(self) -> bool:
return self._modified

Expand All @@ -1506,7 +1530,7 @@ def modified(self, modified: bool) -> None:
@serializable.view(SchemaVersion1Dot5)
@serializable.view(SchemaVersion1Dot6)
@serializable.view(SchemaVersion1Dot7)
@serializable.xml_sequence(20)
@serializable.xml_sequence(21)
def pedigree(self) -> Optional[Pedigree]:
"""
Component pedigree is a way to document complex supply chain scenarios where components are created,
Expand All @@ -1530,7 +1554,7 @@ def pedigree(self, pedigree: Optional[Pedigree]) -> None:
@serializable.view(SchemaVersion1Dot6)
@serializable.view(SchemaVersion1Dot7)
@serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'reference')
@serializable.xml_sequence(21)
@serializable.xml_sequence(22)
def external_references(self) -> 'SortedSet[ExternalReference]':
"""
Provides the ability to document external references related to the component or to the project the component
Expand All @@ -1552,7 +1576,7 @@ def external_references(self, external_references: Iterable[ExternalReference])
@serializable.view(SchemaVersion1Dot6)
@serializable.view(SchemaVersion1Dot7)
@serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'property')
@serializable.xml_sequence(22)
@serializable.xml_sequence(23)
def properties(self) -> 'SortedSet[Property]':
"""
Provides the ability to document properties in a key/value store. This provides flexibility to include data not
Expand All @@ -1569,7 +1593,7 @@ def properties(self, properties: Iterable[Property]) -> None:

@property
@serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'component')
@serializable.xml_sequence(23)
@serializable.xml_sequence(24)
def components(self) -> "SortedSet['Component']":
"""
A list of software and hardware components included in the parent component. This is not a dependency tree. It
Expand All @@ -1591,7 +1615,7 @@ def components(self, components: Iterable['Component']) -> None:
@serializable.view(SchemaVersion1Dot5)
@serializable.view(SchemaVersion1Dot6)
@serializable.view(SchemaVersion1Dot7)
@serializable.xml_sequence(24)
@serializable.xml_sequence(25)
@serializable.type_mapping(_ComponentEvidenceSerializationHelper)
def evidence(self) -> Optional[ComponentEvidence]:
"""
Expand All @@ -1611,7 +1635,7 @@ def evidence(self, evidence: Optional[ComponentEvidence]) -> None:
@serializable.view(SchemaVersion1Dot5)
@serializable.view(SchemaVersion1Dot6)
@serializable.view(SchemaVersion1Dot7)
@serializable.xml_sequence(25)
@serializable.xml_sequence(26)
def release_notes(self) -> Optional[ReleaseNotes]:
"""
Specifies optional release notes.
Expand All @@ -1629,7 +1653,7 @@ def release_notes(self, release_notes: Optional[ReleaseNotes]) -> None:
@serializable.view(SchemaVersion1Dot5)
@serializable.view(SchemaVersion1Dot6)
@serializable.view(SchemaVersion1Dot7)
@serializable.xml_sequence(26)
@serializable.xml_sequence(27)
@serializable.json_name('modelCard')
@serializable.xml_name('modelCard')
def model_card(self) -> Optional[ModelCard]:
Expand All @@ -1648,7 +1672,7 @@ def model_card(self, model_card: Optional[ModelCard]) -> None:
# @property
# ...
# @serializable.view(SchemaVersion1Dot5)
# @serializable.xml_sequence(23)
# @serializable.xml_sequence(24)
# def data(self) -> ...:
# ... # TODO since CDX1.5
#
Expand All @@ -1659,7 +1683,7 @@ def model_card(self, model_card: Optional[ModelCard]) -> None:
@property
@serializable.view(SchemaVersion1Dot6)
@serializable.view(SchemaVersion1Dot7)
@serializable.xml_sequence(30)
@serializable.xml_sequence(31)
def crypto_properties(self) -> Optional[CryptoProperties]:
"""
Cryptographic assets have properties that uniquely define them and that make them actionable for further
Expand All @@ -1681,7 +1705,7 @@ def crypto_properties(self, crypto_properties: Optional[CryptoProperties]) -> No
@serializable.view(SchemaVersion1Dot6)
@serializable.view(SchemaVersion1Dot7)
@serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'tag')
@serializable.xml_sequence(31)
@serializable.xml_sequence(32)
def tags(self) -> 'SortedSet[str]':
"""
Textual strings that aid in discovery, search, and retrieval of the associated object.
Expand Down Expand Up @@ -1741,6 +1765,7 @@ def __comparable_tuple(self) -> _ComparableTuple:
_ComparableTuple(self.components), self.evidence, self.release_notes, self.modified,
_ComparableTuple(self.authors), _ComparableTuple(self.omnibor_ids), self.manufacturer,
self.crypto_properties, _ComparableTuple(self.tags), self.model_card, self.is_external,
_ComparableTuple(self.patent_assertions),
))

def __eq__(self, other: object) -> bool:
Expand Down
72 changes: 68 additions & 4 deletions cyclonedx/model/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import re
from collections.abc import Iterable
from json import loads as json_loads
from typing import TYPE_CHECKING, Any, Optional, Union
from xml.etree.ElementTree import Element # nosec B405

import py_serializable as serializable
from sortedcontainers import SortedSet
Expand All @@ -26,8 +28,10 @@
from .._internal.compare import ComparableTuple as _ComparableTuple
from ..exception.model import InvalidCreIdException
from ..exception.serialization import SerializationOfUnexpectedValueException
from ..schema.schema import SchemaVersion1Dot7
from . import ExternalReference, Property
from .bom_ref import BomRef
from .patent import PatentOrFamily, _PatentsRepositorySerializationHelper

if TYPE_CHECKING: # pragma: no cover
from typing import TypeVar
Expand Down Expand Up @@ -598,9 +602,11 @@ class Definitions:

def __init__(
self, *,
standards: Optional[Iterable[Standard]] = None
standards: Optional[Iterable[Standard]] = None,
patents: Optional[Iterable[PatentOrFamily]] = None,
) -> None:
self.standards = standards or ()
self.patents = patents or ()

@property
@serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'standard')
Expand All @@ -616,12 +622,35 @@ def standards(self) -> 'SortedSet[Standard]':
def standards(self, standards: Iterable[Standard]) -> None:
self._standards = SortedSet(standards)

@property
@serializable.view(SchemaVersion1Dot7)
@serializable.type_mapping(_PatentsRepositorySerializationHelper)
@serializable.xml_sequence(2)
def patents(self) -> 'SortedSet[PatentOrFamily]':
"""
The list of patents and patent families.

.. note::
Introduced in CycloneDX v1.7

Returns:
A SortedSet of :class:`cyclonedx.model.patent.Patent` and :class:`cyclonedx.model.patent.PatentFamily`
"""
return self._patents

@patents.setter
def patents(self, patents: Iterable[PatentOrFamily]) -> None:
self._patents = SortedSet(patents)

def __bool__(self) -> bool:
return len(self._standards) > 0
return len(self._standards) > 0 or len(self._patents) > 0

def __comparable_tuple(self) -> _ComparableTuple:
# all properties are optional - so need to apply all, in hope that one is unique
return _ComparableTuple(self._standards)
return _ComparableTuple((
_ComparableTuple(self._standards),
_ComparableTuple(self._patents),
))

def __eq__(self, other: object) -> bool:
if isinstance(other, Definitions):
Expand All @@ -637,4 +666,39 @@ def __hash__(self) -> int:
return hash(self.__comparable_tuple())

def __repr__(self) -> str:
return f'<Definitions standards={self.standards!r} >'
return f'<Definitions standards={self.standards!r}, patents={self.patents!r} >'


class _DefinitionsSerializationHelper(serializable.helpers.BaseHelper):
"""THIS CLASS IS NON-PUBLIC API

Some parts of :class:`Definitions` are available in later schema versions only.
This helper makes sure, that no empty `definitions` are rendered, if none of its parts is available in a view.
"""

@classmethod
def json_normalize(cls, o: Definitions, *,
view: Optional[type[serializable.ViewType]],
**__: Any) -> Optional[dict[str, Any]]:
data: dict[str, Any] = json_loads(o.as_json(view_=view)) # type:ignore[attr-defined]
return data or None

@classmethod
def json_denormalize(cls, o: dict[str, Any], **__: Any) -> Definitions:
return Definitions.from_json(o) # type:ignore[attr-defined,no-any-return]

@classmethod
def xml_normalize(cls, o: Definitions, *,
element_name: str,
view: Optional[type[serializable.ViewType]],
xmlns: Optional[str],
**__: Any) -> Optional[Element]:
elem: Element = o.as_xml( # type:ignore[attr-defined]
view_=view, as_string=False, element_name=element_name, xmlns=xmlns)
return elem if len(elem) > 0 else None

@classmethod
def xml_denormalize(cls, o: Element, *,
default_ns: Optional[str],
**__: Any) -> Definitions:
return Definitions.from_xml(o, default_ns) # type:ignore[attr-defined,no-any-return]
Loading