From ea342d81134e9cd9e701cf5e268ad32b68f65abc Mon Sep 17 00:00:00 2001 From: Nare <56021123+N4R3@users.noreply.github.com> Date: Fri, 4 Dec 2026 14:57:13 +0100 Subject: [PATCH] feat: add patents and patent assertions (CDX 1.7) Add the CycloneDX 1.7 patent data models: - `cyclonedx.model.patent` with `Patent`, `PatentFamily`, `PatentAssertion`, `PriorityApplication` and enums `PatentLegalStatus`, `PatentAssertionType` - `Definitions.patents` - `Component.patent_assertions` and `Service.patent_assertions` All of these are rendered for CycloneDX >= 1.7 only. `Bom.definitions` is no longer rendered empty, when none of its parts is available in the target schema version. Part of #903 Assisted-by: AI coding assistant (reviewed and tested by the author) Signed-off-by: Nare <56021123+N4R3@users.noreply.github.com> --- cyclonedx/model/bom.py | 5 +- cyclonedx/model/component.py | 55 +- cyclonedx/model/definition.py | 72 +- cyclonedx/model/patent.py | 979 ++++++++++++++++++ cyclonedx/model/service.py | 34 +- docs/responsibilities_capabilities.rst | 1 + tests/_data/models.py | 117 ++- .../enum_PatentAssertionType-1.7.json.bin | 106 ++ .../enum_PatentAssertionType-1.7.xml.bin | 92 ++ .../enum_PatentLegalStatus-1.7.json.bin | 102 ++ .../enum_PatentLegalStatus-1.7.xml.bin | 79 ++ ..._with_definitions_patents_only-1.0.xml.bin | 4 + ..._with_definitions_patents_only-1.1.xml.bin | 4 + ...with_definitions_patents_only-1.2.json.bin | 10 + ..._with_definitions_patents_only-1.2.xml.bin | 6 + ...with_definitions_patents_only-1.3.json.bin | 10 + ..._with_definitions_patents_only-1.3.xml.bin | 6 + ...with_definitions_patents_only-1.4.json.bin | 10 + ..._with_definitions_patents_only-1.4.xml.bin | 6 + ...with_definitions_patents_only-1.5.json.bin | 20 + ..._with_definitions_patents_only-1.5.xml.bin | 10 + ...with_definitions_patents_only-1.6.json.bin | 20 + ..._with_definitions_patents_only-1.6.xml.bin | 10 + ...with_definitions_patents_only-1.7.json.bin | 30 + ..._with_definitions_patents_only-1.7.xml.bin | 19 + .../get_bom_with_patents-1.0.xml.bin | 10 + .../get_bom_with_patents-1.1.xml.bin | 9 + .../get_bom_with_patents-1.2.json.bin | 32 + .../get_bom_with_patents-1.2.xml.bin | 21 + .../get_bom_with_patents-1.3.json.bin | 32 + .../get_bom_with_patents-1.3.xml.bin | 21 + .../get_bom_with_patents-1.4.json.bin | 31 + .../get_bom_with_patents-1.4.xml.bin | 20 + .../get_bom_with_patents-1.5.json.bin | 41 + .../get_bom_with_patents-1.5.xml.bin | 24 + .../get_bom_with_patents-1.6.json.bin | 52 + .../get_bom_with_patents-1.6.xml.bin | 34 + .../get_bom_with_patents-1.7.json.bin | 176 ++++ .../get_bom_with_patents-1.7.xml.bin | 150 +++ tests/test_enums.py | 46 + tests/test_model_patent.py | 214 ++++ 41 files changed, 2693 insertions(+), 27 deletions(-) create mode 100644 cyclonedx/model/patent.py create mode 100644 tests/_data/snapshots/enum_PatentAssertionType-1.7.json.bin create mode 100644 tests/_data/snapshots/enum_PatentAssertionType-1.7.xml.bin create mode 100644 tests/_data/snapshots/enum_PatentLegalStatus-1.7.json.bin create mode 100644 tests/_data/snapshots/enum_PatentLegalStatus-1.7.xml.bin create mode 100644 tests/_data/snapshots/get_bom_with_definitions_patents_only-1.0.xml.bin create mode 100644 tests/_data/snapshots/get_bom_with_definitions_patents_only-1.1.xml.bin create mode 100644 tests/_data/snapshots/get_bom_with_definitions_patents_only-1.2.json.bin create mode 100644 tests/_data/snapshots/get_bom_with_definitions_patents_only-1.2.xml.bin create mode 100644 tests/_data/snapshots/get_bom_with_definitions_patents_only-1.3.json.bin create mode 100644 tests/_data/snapshots/get_bom_with_definitions_patents_only-1.3.xml.bin create mode 100644 tests/_data/snapshots/get_bom_with_definitions_patents_only-1.4.json.bin create mode 100644 tests/_data/snapshots/get_bom_with_definitions_patents_only-1.4.xml.bin create mode 100644 tests/_data/snapshots/get_bom_with_definitions_patents_only-1.5.json.bin create mode 100644 tests/_data/snapshots/get_bom_with_definitions_patents_only-1.5.xml.bin create mode 100644 tests/_data/snapshots/get_bom_with_definitions_patents_only-1.6.json.bin create mode 100644 tests/_data/snapshots/get_bom_with_definitions_patents_only-1.6.xml.bin create mode 100644 tests/_data/snapshots/get_bom_with_definitions_patents_only-1.7.json.bin create mode 100644 tests/_data/snapshots/get_bom_with_definitions_patents_only-1.7.xml.bin create mode 100644 tests/_data/snapshots/get_bom_with_patents-1.0.xml.bin create mode 100644 tests/_data/snapshots/get_bom_with_patents-1.1.xml.bin create mode 100644 tests/_data/snapshots/get_bom_with_patents-1.2.json.bin create mode 100644 tests/_data/snapshots/get_bom_with_patents-1.2.xml.bin create mode 100644 tests/_data/snapshots/get_bom_with_patents-1.3.json.bin create mode 100644 tests/_data/snapshots/get_bom_with_patents-1.3.xml.bin create mode 100644 tests/_data/snapshots/get_bom_with_patents-1.4.json.bin create mode 100644 tests/_data/snapshots/get_bom_with_patents-1.4.xml.bin create mode 100644 tests/_data/snapshots/get_bom_with_patents-1.5.json.bin create mode 100644 tests/_data/snapshots/get_bom_with_patents-1.5.xml.bin create mode 100644 tests/_data/snapshots/get_bom_with_patents-1.6.json.bin create mode 100644 tests/_data/snapshots/get_bom_with_patents-1.6.xml.bin create mode 100644 tests/_data/snapshots/get_bom_with_patents-1.7.json.bin create mode 100644 tests/_data/snapshots/get_bom_with_patents-1.7.xml.bin create mode 100644 tests/test_model_patent.py diff --git a/cyclonedx/model/bom.py b/cyclonedx/model/bom.py index 3f76c94a..0e60f15e 100644 --- a/cyclonedx/model/bom.py +++ b/cyclonedx/model/bom.py @@ -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 @@ -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]: """ @@ -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: diff --git a/cyclonedx/model/component.py b/cyclonedx/model/component.py index 7a5136c3..d8f1996d 100644 --- a/cyclonedx/model/component.py +++ b/cyclonedx/model/component.py @@ -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 @@ -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: @@ -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 @@ -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. @@ -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). @@ -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 @@ -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 @@ -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. @@ -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 @@ -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, @@ -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 @@ -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 @@ -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 @@ -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]: """ @@ -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. @@ -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]: @@ -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 # @@ -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 @@ -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. @@ -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: diff --git a/cyclonedx/model/definition.py b/cyclonedx/model/definition.py index 639743e5..84ab17c4 100644 --- a/cyclonedx/model/definition.py +++ b/cyclonedx/model/definition.py @@ -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 @@ -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 @@ -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') @@ -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): @@ -637,4 +666,39 @@ def __hash__(self) -> int: return hash(self.__comparable_tuple()) def __repr__(self) -> str: - return f'' + return f'' + + +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] diff --git a/cyclonedx/model/patent.py b/cyclonedx/model/patent.py new file mode 100644 index 00000000..5ffa8938 --- /dev/null +++ b/cyclonedx/model/patent.py @@ -0,0 +1,979 @@ +# This file is part of CycloneDX Python Library +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) OWASP Foundation. All Rights Reserved. + + +""" +This set of classes represents the patent-related data types in the CycloneDX standard. + +.. note:: + Introduced in CycloneDX v1.7 + +.. note:: + See the CycloneDX Schema for patents: https://cyclonedx.org/docs/1.7/json/#definitions_patents +""" + +from collections.abc import Iterable +from datetime import date +from enum import Enum +from json import loads as json_loads +from typing import Any, Optional, Union +from xml.etree.ElementTree import Element # nosec B405 + +import py_serializable as serializable +from sortedcontainers import SortedSet + +from .._internal.bom_ref import bom_ref_from_str as _bom_ref_from_str +from .._internal.compare import ComparableTuple as _ComparableTuple +from ..exception.serialization import CycloneDxDeserializationException, SerializationOfUnexpectedValueException +from . import ExternalReference +from .bom_ref import BomRef +from .contact import OrganizationalContact, OrganizationalEntity + + +def _xml_tag(tag: str, xmlns: Optional[str]) -> str: + return f'{{{xmlns}}}{tag}' if xmlns else tag + + +def _xml_local_tag(elem: Element, default_ns: Optional[str]) -> str: + return elem.tag if default_ns is None else elem.tag.replace(f'{{{default_ns}}}', '') + + +@serializable.serializable_enum +class PatentLegalStatus(str, Enum): + """ + Enum object that defines the legal status of a patent or patent application. + Aligned with concepts in WIPO ST.27. + + .. note:: + Introduced in CycloneDX v1.7 + + .. note:: + See the CycloneDX Schema: https://cyclonedx.org/docs/1.7/xml/#type_patentLegalStatusEnum + """ + + PENDING = 'pending' + GRANTED = 'granted' + REVOKED = 'revoked' + EXPIRED = 'expired' + LAPSED = 'lapsed' + WITHDRAWN = 'withdrawn' + ABANDONED = 'abandoned' + SUSPENDED = 'suspended' + REINSTATED = 'reinstated' + OPPOSED = 'opposed' + TERMINATED = 'terminated' + INVALIDATED = 'invalidated' + IN_FORCE = 'in-force' + + +@serializable.serializable_enum +class PatentAssertionType(str, Enum): + """ + Enum object that defines the type of assertion made about a patent or patent family. + + .. note:: + Introduced in CycloneDX v1.7 + + .. note:: + See the CycloneDX Schema: https://cyclonedx.org/docs/1.7/xml/#type_patentAssertionTypeEnum + """ + + OWNERSHIP = 'ownership' + LICENSE = 'license' + THIRD_PARTY_CLAIM = 'third-party-claim' + STANDARDS_INCLUSION = 'standards-inclusion' + PRIOR_ART = 'prior-art' + EXCLUSIVE_RIGHTS = 'exclusive-rights' + NON_ASSERTION = 'non-assertion' + RESEARCH_OR_EVALUATION = 'research-or-evaluation' + + +PatentAssignee = Union[OrganizationalEntity, OrganizationalContact] +"""Type alias: an assignee of a patent is either an organization or an individual.""" + +PatentAsserter = Union[OrganizationalEntity, OrganizationalContact, BomRef] +"""Type alias: the asserter of a patent claim is an organization, an individual, or a reference to either.""" + + +def _party_sort_key(o: Union[OrganizationalEntity, OrganizationalContact, BomRef]) -> tuple[int, Any]: + # mixed types are not comparable with each other - so sort by kind first, then by value. + if isinstance(o, OrganizationalEntity): + return 0, o + if isinstance(o, OrganizationalContact): + return 1, o + return 2, o + + +def _json_party_denormalize(o: dict[str, Any]) -> PatentAssignee: + # JSON does not tell entities and contacts apart explicitly; use the properties that only one of them has. + if any(k in o for k in ('email', 'phone')): + return OrganizationalContact.from_json(o) # type:ignore[attr-defined,no-any-return] + return OrganizationalEntity.from_json(o) # type:ignore[attr-defined,no-any-return] + + +class _PatentAssigneesSerializationHelper(serializable.helpers.BaseHelper): + """ THIS CLASS IS NON-PUBLIC API """ + + # In JSON, `patentAssignee` is a plain array of `organizationalEntity` or `organizationalContact`. + # In XML, `patentAssignee` is a repeated element of `organizationOrIndividualType`, + # which wraps its content in either `organization` or `individual`. + # Repeated elements cannot be rendered by a py-serializable helper, therefore XML is rendered in a temporary + # container element, that is unwrapped by :class:`_PatentsRepositorySerializationHelper`. + XML_CONTAINER = 'patentAssignees' + XML_ITEM = 'patentAssignee' + + @classmethod + def json_normalize(cls, o: Iterable[PatentAssignee], *, + view: Optional[type[serializable.ViewType]], + **__: Any) -> Optional[list[Any]]: + items = [json_loads(a.as_json(view_=view)) for a in o] # type:ignore[union-attr] + return items or None + + @classmethod + def json_denormalize(cls, o: list[dict[str, Any]], **__: Any) -> list[PatentAssignee]: + return [_json_party_denormalize(a) for a in o] + + @classmethod + def xml_normalize(cls, o: Iterable[PatentAssignee], *, + element_name: str, + view: Optional[type[serializable.ViewType]], + xmlns: Optional[str], + **__: Any) -> Optional[Element]: + container = Element(element_name) + for a in o: + item = Element(_xml_tag(cls.XML_ITEM, xmlns)) + if isinstance(a, OrganizationalEntity): + item.append(a.as_xml( # type:ignore[attr-defined] + view_=view, as_string=False, element_name='organization', xmlns=xmlns)) + elif isinstance(a, OrganizationalContact): + item.append(a.as_xml( # type:ignore[attr-defined] + view_=view, as_string=False, element_name='individual', xmlns=xmlns)) + else: + raise SerializationOfUnexpectedValueException(f'unexpected patent assignee: {a!r}') + container.append(item) + return container if len(container) > 0 else None + + @classmethod + def xml_denormalize(cls, o: Element, *, + default_ns: Optional[str], + **__: Any) -> list[PatentAssignee]: + assignees: list[PatentAssignee] = [] + for item in o: + for choice in item: + tag = _xml_local_tag(choice, default_ns) + if tag == 'organization': + assignees.append(OrganizationalEntity.from_xml( # type:ignore[attr-defined] + choice, default_ns)) + elif tag == 'individual': + assignees.append(OrganizationalContact.from_xml( # type:ignore[attr-defined] + choice, default_ns)) + else: + raise CycloneDxDeserializationException(f'unexpected patent assignee: {choice!r}') + return assignees + + +class _PatentAsserterSerializationHelper(serializable.helpers.BaseHelper): + """ THIS CLASS IS NON-PUBLIC API """ + + @classmethod + def json_normalize(cls, o: PatentAsserter, *, + view: Optional[type[serializable.ViewType]], + **__: Any) -> Any: + if isinstance(o, BomRef): + return o.value + if isinstance(o, (OrganizationalEntity, OrganizationalContact)): + return json_loads(o.as_json(view_=view)) # type:ignore[union-attr] + raise SerializationOfUnexpectedValueException(f'unexpected patent asserter: {o!r}') + + @classmethod + def json_denormalize(cls, o: Any, **__: Any) -> PatentAsserter: + if isinstance(o, str): + return BomRef(o) + if isinstance(o, dict): + return _json_party_denormalize(o) + raise CycloneDxDeserializationException(f'unexpected patent asserter: {o!r}') + + @classmethod + def xml_normalize(cls, o: PatentAsserter, *, + element_name: str, + view: Optional[type[serializable.ViewType]], + xmlns: Optional[str], + **__: Any) -> Element: + elem = Element(element_name) + if isinstance(o, BomRef): + ref = Element(_xml_tag('ref', xmlns)) + ref.text = o.value + elem.append(ref) + elif isinstance(o, OrganizationalEntity): + elem.append(o.as_xml( # type:ignore[attr-defined] + view_=view, as_string=False, element_name='organization', xmlns=xmlns)) + elif isinstance(o, OrganizationalContact): + elem.append(o.as_xml( # type:ignore[attr-defined] + view_=view, as_string=False, element_name='contact', xmlns=xmlns)) + else: + raise SerializationOfUnexpectedValueException(f'unexpected patent asserter: {o!r}') + return elem + + @classmethod + def xml_denormalize(cls, o: Element, *, + default_ns: Optional[str], + **__: Any) -> PatentAsserter: + for choice in o: + tag = _xml_local_tag(choice, default_ns) + if tag == 'ref': + return BomRef(choice.text) + if tag == 'organization': + return OrganizationalEntity.from_xml(choice, default_ns) # type:ignore[attr-defined,no-any-return] + if tag == 'contact': + return OrganizationalContact.from_xml(choice, default_ns) # type:ignore[attr-defined,no-any-return] + raise CycloneDxDeserializationException(f'unexpected patent asserter: {o!r}') + + +@serializable.serializable_class(ignore_unknown_during_deserialization=True) +class PriorityApplication: + """ + The priority application contains the essential data necessary to identify and reference an earlier patent + filing for priority rights: the jurisdiction (office code), the application number, and the filing date. + + .. note:: + Introduced in CycloneDX v1.7 + + .. note:: + See the CycloneDX Schema: https://cyclonedx.org/docs/1.7/xml/#type_priorityApplicationType + """ + + def __init__( + self, *, + application_number: str, + jurisdiction: str, + filing_date: date, + ) -> None: + self.application_number = application_number + self.jurisdiction = jurisdiction + self.filing_date = filing_date + + @property + @serializable.xml_sequence(1) + def application_number(self) -> str: + """ + The unique number assigned to a patent application when it is filed with a patent office. + + Returns: + `str` + """ + return self._application_number + + @application_number.setter + def application_number(self, application_number: str) -> None: + self._application_number = application_number + + @property + @serializable.xml_sequence(2) + def jurisdiction(self) -> str: + """ + The jurisdiction or patent office where the priority application was filed, specified using WIPO ST.3 codes. + + Returns: + `str` + """ + return self._jurisdiction + + @jurisdiction.setter + def jurisdiction(self, jurisdiction: str) -> None: + self._jurisdiction = jurisdiction + + @property + @serializable.type_mapping(serializable.helpers.XsdDate) + @serializable.xml_sequence(3) + def filing_date(self) -> date: + """ + The date the priority application was filed. + + Returns: + `date` + """ + return self._filing_date + + @filing_date.setter + def filing_date(self, filing_date: date) -> None: + self._filing_date = filing_date + + def __comparable_tuple(self) -> _ComparableTuple: + return _ComparableTuple((self.jurisdiction, self.application_number, self.filing_date)) + + def __eq__(self, other: object) -> bool: + if isinstance(other, PriorityApplication): + return self.__comparable_tuple() == other.__comparable_tuple() + return False + + def __lt__(self, other: Any) -> bool: + if isinstance(other, PriorityApplication): + return self.__comparable_tuple() < other.__comparable_tuple() + return NotImplemented + + def __hash__(self) -> int: + return hash(self.__comparable_tuple()) + + def __repr__(self) -> str: + return f'' + + +@serializable.serializable_class(ignore_unknown_during_deserialization=True) +class Patent: + """ + A patent is a legal instrument, granted by an authority, that confers certain rights over an invention for a + specified period, contingent on public disclosure and adherence to relevant legal requirements. + + .. note:: + Introduced in CycloneDX v1.7 + + .. note:: + See the CycloneDX Schema: https://cyclonedx.org/docs/1.7/xml/#type_patentType + """ + + def __init__( + self, *, + patent_number: str, + jurisdiction: str, + patent_legal_status: PatentLegalStatus, + bom_ref: Optional[Union[str, BomRef]] = None, + application_number: Optional[str] = None, + priority_application: Optional[PriorityApplication] = None, + publication_number: Optional[str] = None, + title: Optional[str] = None, + abstract: Optional[str] = None, + filing_date: Optional[date] = None, + grant_date: Optional[date] = None, + patent_expiration_date: Optional[date] = None, + patent_assignees: Optional[Iterable[PatentAssignee]] = None, + external_references: Optional[Iterable[ExternalReference]] = None, + ) -> None: + self._bom_ref = _bom_ref_from_str(bom_ref) + self.patent_number = patent_number + self.application_number = application_number + self.jurisdiction = jurisdiction + self.priority_application = priority_application + self.publication_number = publication_number + self.title = title + self.abstract = abstract + self.filing_date = filing_date + self.grant_date = grant_date + self.patent_expiration_date = patent_expiration_date + self.patent_legal_status = patent_legal_status + self.patent_assignees = patent_assignees or () + self.external_references = external_references or () + + @property + @serializable.type_mapping(BomRef) + @serializable.json_name('bom-ref') + @serializable.xml_name('bom-ref') + @serializable.xml_attribute() + def bom_ref(self) -> BomRef: + """ + An optional identifier which can be used to reference the patent elsewhere in the BOM. + Every `bom-ref` MUST be unique within the BOM. + + Returns: + `BomRef` + """ + return self._bom_ref + + @property + @serializable.xml_sequence(1) + def patent_number(self) -> str: + """ + The unique number assigned to the granted patent by the issuing authority. + + Returns: + `str` + """ + return self._patent_number + + @patent_number.setter + def patent_number(self, patent_number: str) -> None: + self._patent_number = patent_number + + @property + @serializable.xml_sequence(2) + def application_number(self) -> Optional[str]: + """ + The unique number assigned to the patent application when it was filed with a patent office. + + Returns: + `str` or `None` + """ + return self._application_number + + @application_number.setter + def application_number(self, application_number: Optional[str]) -> None: + self._application_number = application_number + + @property + @serializable.xml_sequence(3) + def jurisdiction(self) -> str: + """ + The jurisdiction or patent office, specified using WIPO ST.3 codes. + + Returns: + `str` + """ + return self._jurisdiction + + @jurisdiction.setter + def jurisdiction(self, jurisdiction: str) -> None: + self._jurisdiction = jurisdiction + + @property + @serializable.xml_sequence(4) + def priority_application(self) -> Optional[PriorityApplication]: + """ + The priority application of this patent. + + Returns: + `PriorityApplication` or `None` + """ + return self._priority_application + + @priority_application.setter + def priority_application(self, priority_application: Optional[PriorityApplication]) -> None: + self._priority_application = priority_application + + @property + @serializable.xml_sequence(5) + def publication_number(self) -> Optional[str]: + """ + The number assigned to the patent application once it is published. + + Returns: + `str` or `None` + """ + return self._publication_number + + @publication_number.setter + def publication_number(self, publication_number: Optional[str]) -> None: + self._publication_number = publication_number + + @property + @serializable.xml_sequence(6) + def title(self) -> Optional[str]: + """ + The title of the patent, summarising the invention it protects. + + Returns: + `str` or `None` + """ + return self._title + + @title.setter + def title(self, title: Optional[str]) -> None: + self._title = title + + @property + @serializable.xml_sequence(7) + def abstract(self) -> Optional[str]: + """ + A brief summary of the invention described in the patent. + + Returns: + `str` or `None` + """ + return self._abstract + + @abstract.setter + def abstract(self, abstract: Optional[str]) -> None: + self._abstract = abstract + + @property + @serializable.type_mapping(serializable.helpers.XsdDate) + @serializable.xml_sequence(8) + def filing_date(self) -> Optional[date]: + """ + The date the patent application was filed with the jurisdiction. + + Returns: + `date` or `None` + """ + return self._filing_date + + @filing_date.setter + def filing_date(self, filing_date: Optional[date]) -> None: + self._filing_date = filing_date + + @property + @serializable.type_mapping(serializable.helpers.XsdDate) + @serializable.xml_sequence(9) + def grant_date(self) -> Optional[date]: + """ + The date the patent was granted by the jurisdiction. + + Returns: + `date` or `None` + """ + return self._grant_date + + @grant_date.setter + def grant_date(self, grant_date: Optional[date]) -> None: + self._grant_date = grant_date + + @property + @serializable.type_mapping(serializable.helpers.XsdDate) + @serializable.xml_sequence(10) + def patent_expiration_date(self) -> Optional[date]: + """ + The date the patent expires. + + Returns: + `date` or `None` + """ + return self._patent_expiration_date + + @patent_expiration_date.setter + def patent_expiration_date(self, patent_expiration_date: Optional[date]) -> None: + self._patent_expiration_date = patent_expiration_date + + @property + @serializable.xml_sequence(11) + def patent_legal_status(self) -> PatentLegalStatus: + """ + The current legal status of the patent or patent application, based on the WIPO ST.27 standard. + + Returns: + `PatentLegalStatus` + """ + return self._patent_legal_status + + @patent_legal_status.setter + def patent_legal_status(self, patent_legal_status: PatentLegalStatus) -> None: + self._patent_legal_status = patent_legal_status + + @property + @serializable.json_name(_PatentAssigneesSerializationHelper.XML_ITEM) + @serializable.xml_name(_PatentAssigneesSerializationHelper.XML_CONTAINER) + @serializable.type_mapping(_PatentAssigneesSerializationHelper) + @serializable.xml_sequence(12) + def patent_assignees(self) -> 'SortedSet[PatentAssignee]': + """ + Organisations or individuals to whom the patent rights are assigned. Supports joint ownership. + + Returns: + Set of `OrganizationalEntity` or `OrganizationalContact` + """ + return self._patent_assignees + + @patent_assignees.setter + def patent_assignees(self, patent_assignees: Iterable[PatentAssignee]) -> None: + self._patent_assignees = SortedSet(patent_assignees, key=_party_sort_key) + + @property + @serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'reference') + @serializable.xml_sequence(13) + def external_references(self) -> 'SortedSet[ExternalReference]': + """ + External references related to the patent. + + Returns: + Set of `ExternalReference` + """ + return self._external_references + + @external_references.setter + def external_references(self, external_references: Iterable[ExternalReference]) -> None: + self._external_references = SortedSet(external_references) + + def __comparable_tuple(self) -> _ComparableTuple: + return _ComparableTuple(( + self.jurisdiction, self.patent_number, self.bom_ref.value, + self.application_number, self.publication_number, self.patent_legal_status, + self.title, self.abstract, self.filing_date, self.grant_date, self.patent_expiration_date, + self.priority_application, + _ComparableTuple(map(_party_sort_key, self.patent_assignees)), + _ComparableTuple(self.external_references), + )) + + def __eq__(self, other: object) -> bool: + if isinstance(other, Patent): + return self.__comparable_tuple() == other.__comparable_tuple() + return False + + def __lt__(self, other: Any) -> bool: + if isinstance(other, Patent): + return self.__comparable_tuple() < other.__comparable_tuple() + if isinstance(other, PatentFamily): + return True # self before any PatentFamily + return NotImplemented + + def __hash__(self) -> int: + return hash(self.__comparable_tuple()) + + def __repr__(self) -> str: + return f'' + + +@serializable.serializable_class(ignore_unknown_during_deserialization=True) +class PatentFamily: + """ + A patent family is a group of related patent applications or granted patents that cover the same or similar + invention. + + .. note:: + Introduced in CycloneDX v1.7 + + .. note:: + See the CycloneDX Schema: https://cyclonedx.org/docs/1.7/xml/#type_patentFamilyType + """ + + def __init__( + self, *, + family_id: str, + bom_ref: Optional[Union[str, BomRef]] = None, + priority_application: Optional[PriorityApplication] = None, + members: Optional[Iterable[BomRef]] = None, + external_references: Optional[Iterable[ExternalReference]] = None, + ) -> None: + self._bom_ref = _bom_ref_from_str(bom_ref) + self.family_id = family_id + self.priority_application = priority_application + self.members = members or () + self.external_references = external_references or () + + @property + @serializable.type_mapping(BomRef) + @serializable.json_name('bom-ref') + @serializable.xml_name('bom-ref') + @serializable.xml_attribute() + def bom_ref(self) -> BomRef: + """ + An optional identifier which can be used to reference the patent family elsewhere in the BOM. + Every `bom-ref` MUST be unique within the BOM. + + Returns: + `BomRef` + """ + return self._bom_ref + + @property + @serializable.json_name('familyId') + @serializable.xml_name('familyId') + @serializable.xml_sequence(1) + def family_id(self) -> str: + """ + The unique identifier for the patent family. + + Returns: + `str` + """ + return self._family_id + + @family_id.setter + def family_id(self, family_id: str) -> None: + self._family_id = family_id + + @property + @serializable.xml_sequence(2) + def priority_application(self) -> Optional[PriorityApplication]: + """ + The priority application of this patent family. + + Returns: + `PriorityApplication` or `None` + """ + return self._priority_application + + @priority_application.setter + def priority_application(self, priority_application: Optional[PriorityApplication]) -> None: + self._priority_application = priority_application + + @property + @serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'ref') + @serializable.xml_sequence(3) + def members(self) -> 'SortedSet[BomRef]': + """ + Patents or applications that belong to this family, each identified by a `bom-ref` pointing to a + :class:`Patent` defined elsewhere in the BOM. + + Returns: + Set of `BomRef` + """ + return self._members + + @members.setter + def members(self, members: Iterable[BomRef]) -> None: + self._members = SortedSet(members) + + @property + @serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'reference') + @serializable.xml_sequence(4) + def external_references(self) -> 'SortedSet[ExternalReference]': + """ + External references related to the patent family. + + Returns: + Set of `ExternalReference` + """ + return self._external_references + + @external_references.setter + def external_references(self, external_references: Iterable[ExternalReference]) -> None: + self._external_references = SortedSet(external_references) + + def __comparable_tuple(self) -> _ComparableTuple: + return _ComparableTuple(( + self.family_id, self.bom_ref.value, + self.priority_application, + _ComparableTuple(self.members), + _ComparableTuple(self.external_references), + )) + + def __eq__(self, other: object) -> bool: + if isinstance(other, PatentFamily): + return self.__comparable_tuple() == other.__comparable_tuple() + return False + + def __lt__(self, other: Any) -> bool: + if isinstance(other, PatentFamily): + return self.__comparable_tuple() < other.__comparable_tuple() + if isinstance(other, Patent): + return False # self after any Patent + return NotImplemented + + def __hash__(self) -> int: + return hash(self.__comparable_tuple()) + + def __repr__(self) -> str: + return f'' + + +@serializable.serializable_class(ignore_unknown_during_deserialization=True) +class PatentAssertion: + """ + An assertion linking a patent or patent family to a component or service. + This allows expression of ownership, licensing, third-party claims, and other legal relationships. + + .. note:: + Introduced in CycloneDX v1.7 + + .. note:: + See the CycloneDX Schema: https://cyclonedx.org/docs/1.7/xml/#type_patentAssertionType + """ + + def __init__( + self, *, + assertion_type: PatentAssertionType, + asserter: PatentAsserter, + bom_ref: Optional[Union[str, BomRef]] = None, + patent_refs: Optional[Iterable[BomRef]] = None, + notes: Optional[str] = None, + ) -> None: + self._bom_ref = _bom_ref_from_str(bom_ref) + self.assertion_type = assertion_type + self.patent_refs = patent_refs or () + self.asserter = asserter + self.notes = notes + + @property + @serializable.type_mapping(BomRef) + @serializable.json_name('bom-ref') + @serializable.xml_name('bom-ref') + @serializable.xml_attribute() + def bom_ref(self) -> BomRef: + """ + An optional identifier which can be used to reference the assertion elsewhere in the BOM. + Every `bom-ref` MUST be unique within the BOM. + + Returns: + `BomRef` + """ + return self._bom_ref + + @property + @serializable.xml_sequence(1) + def assertion_type(self) -> PatentAssertionType: + """ + The type of the assertion. + + Returns: + `PatentAssertionType` + """ + return self._assertion_type + + @assertion_type.setter + def assertion_type(self, assertion_type: PatentAssertionType) -> None: + self._assertion_type = assertion_type + + @property + @serializable.json_name('patentRefs') + @serializable.xml_name('patentRefs') + @serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'bom-ref') + @serializable.xml_sequence(2) + def patent_refs(self) -> 'SortedSet[BomRef]': + """ + References (`bom-ref`) to the :class:`Patent` or :class:`PatentFamily` associated with this assertion. + + Returns: + Set of `BomRef` + """ + return self._patent_refs + + @patent_refs.setter + def patent_refs(self, patent_refs: Iterable[BomRef]) -> None: + self._patent_refs = SortedSet(patent_refs) + + @property + @serializable.type_mapping(_PatentAsserterSerializationHelper) + @serializable.xml_sequence(3) + def asserter(self) -> PatentAsserter: + """ + The organisation, individual, or a reference to either, asserting the patent claim. + + Returns: + `OrganizationalEntity`, `OrganizationalContact` or `BomRef` + """ + return self._asserter + + @asserter.setter + def asserter(self, asserter: PatentAsserter) -> None: + self._asserter = asserter + + @property + @serializable.xml_sequence(4) + def notes(self) -> Optional[str]: + """ + Additional notes or clarifications regarding the assertion. + + Returns: + `str` or `None` + """ + return self._notes + + @notes.setter + def notes(self, notes: Optional[str]) -> None: + self._notes = notes + + def __comparable_tuple(self) -> _ComparableTuple: + return _ComparableTuple(( + self.assertion_type, self.bom_ref.value, + _ComparableTuple(self.patent_refs), + _party_sort_key(self.asserter), + self.notes, + )) + + def __eq__(self, other: object) -> bool: + if isinstance(other, PatentAssertion): + return self.__comparable_tuple() == other.__comparable_tuple() + return False + + def __lt__(self, other: Any) -> bool: + if isinstance(other, PatentAssertion): + return self.__comparable_tuple() < other.__comparable_tuple() + return NotImplemented + + def __hash__(self) -> int: + return hash(self.__comparable_tuple()) + + def __repr__(self) -> str: + return f'' + + +PatentOrFamily = Union[Patent, PatentFamily] +"""Type alias: an item of :attr:`cyclonedx.model.definition.Definitions.patents`.""" + + +class _PatentsRepositorySerializationHelper(serializable.helpers.BaseHelper): + """ THIS CLASS IS NON-PUBLIC API """ + + @classmethod + def json_normalize(cls, o: Iterable[PatentOrFamily], *, + view: Optional[type[serializable.ViewType]], + **__: Any) -> Optional[list[Any]]: + items = [json_loads(p.as_json(view_=view)) for p in o] # type:ignore[union-attr] + return items or None + + @classmethod + def json_denormalize(cls, o: list[dict[str, Any]], **__: Any) -> list[PatentOrFamily]: + patents: list[PatentOrFamily] = [] + for p in o: + if 'familyId' in p: + patents.append(PatentFamily.from_json(p)) # type:ignore[attr-defined] + elif 'patentNumber' in p: + patents.append(Patent.from_json(p)) # type:ignore[attr-defined] + else: + raise CycloneDxDeserializationException(f'unexpected patent: {p!r}') + return patents + + @classmethod + def xml_normalize(cls, o: Iterable[PatentOrFamily], *, + element_name: str, + view: Optional[type[serializable.ViewType]], + xmlns: Optional[str], + **__: Any) -> Optional[Element]: + elem = Element(element_name) + assignees_container = _xml_tag(_PatentAssigneesSerializationHelper.XML_CONTAINER, xmlns) + for p in o: + if isinstance(p, Patent): + pe: Element = p.as_xml( # type:ignore[attr-defined] + view_=view, as_string=False, element_name='patent', xmlns=xmlns) + # unwrap the temporary container - see `_PatentAssigneesSerializationHelper` + for i, child in enumerate(list(pe)): + if child.tag == assignees_container: + pe.remove(child) + for j, assignee in enumerate(child): + pe.insert(i + j, assignee) + break + elem.append(pe) + elif isinstance(p, PatentFamily): + elem.append(p.as_xml( # type:ignore[attr-defined] + view_=view, as_string=False, element_name='patentFamily', xmlns=xmlns)) + else: + raise SerializationOfUnexpectedValueException(f'unexpected patent: {p!r}') + return elem if len(elem) > 0 else None + + @staticmethod + def __xml_wrap_assignees(p: Element, default_ns: Optional[str]) -> Element: + # wrap the repeated assignees in a temporary container - see `_PatentAssigneesSerializationHelper` + item_tag = _PatentAssigneesSerializationHelper.XML_ITEM + if not any(_xml_local_tag(c, default_ns) == item_tag for c in p): + return p + wrapped = Element(p.tag, p.attrib) + container: Optional[Element] = None + for c in p: + if _xml_local_tag(c, default_ns) == item_tag: + if container is None: + container = Element(_xml_tag(_PatentAssigneesSerializationHelper.XML_CONTAINER, default_ns)) + wrapped.append(container) + container.append(c) + else: + wrapped.append(c) + return wrapped + + @classmethod + def xml_denormalize(cls, o: Element, *, + default_ns: Optional[str], + **__: Any) -> list[PatentOrFamily]: + patents: list[PatentOrFamily] = [] + for p in o: + tag = _xml_local_tag(p, default_ns) + if tag == 'patent': + patents.append(Patent.from_xml( # type:ignore[attr-defined] + cls.__xml_wrap_assignees(p, default_ns), default_ns)) + elif tag == 'patentFamily': + patents.append(PatentFamily.from_xml(p, default_ns)) # type:ignore[attr-defined] + else: + raise CycloneDxDeserializationException(f'unexpected patent: {p!r}') + return patents diff --git a/cyclonedx/model/service.py b/cyclonedx/model/service.py index 10597370..52cf16fd 100644 --- a/cyclonedx/model/service.py +++ b/cyclonedx/model/service.py @@ -44,6 +44,7 @@ from .contact import OrganizationalEntity from .dependency import Dependable from .license import License, LicenseRepository, _LicenseRepositorySerializationHelper +from .patent import PatentAssertion from .release_note import ReleaseNotes @@ -73,6 +74,7 @@ def __init__( properties: Optional[Iterable[Property]] = None, services: Optional[Iterable['Service']] = None, release_notes: Optional[ReleaseNotes] = None, + patent_assertions: Optional[Iterable[PatentAssertion]] = None, ) -> None: self._bom_ref = _bom_ref_from_str(bom_ref) self.provider = provider @@ -89,6 +91,7 @@ def __init__( self.services = services or [] self.release_notes = release_notes self.properties = properties or [] + self.patent_assertions = patent_assertions or [] @property @serializable.json_name('bom-ref') @@ -285,8 +288,29 @@ def licenses(self, licenses: Iterable[License]) -> None: self._licenses = LicenseRepository(licenses) @property - @serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'reference') + @serializable.view(SchemaVersion1Dot7) + @serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'patentAssertion') @serializable.xml_sequence(12) + def patent_assertions(self) -> 'SortedSet[PatentAssertion]': + """ + A list of assertions made regarding patents associated with this service. + 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_array(serializable.XmlArraySerializationType.NESTED, 'reference') + @serializable.xml_sequence(13) def external_references(self) -> 'SortedSet[ExternalReference]': """ Provides the ability to document external references related to the Service. @@ -307,7 +331,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(13) + @serializable.xml_sequence(14) def properties(self) -> 'SortedSet[Property]': """ Provides the ability to document properties in a key/value store. This provides flexibility to include data not @@ -324,7 +348,7 @@ def properties(self, properties: Iterable[Property]) -> None: @property @serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'service') - @serializable.xml_sequence(14) + @serializable.xml_sequence(15) def services(self) -> "SortedSet['Service']": """ A list of services included or deployed behind the parent service. @@ -347,7 +371,7 @@ def services(self, services: Iterable['Service']) -> None: @serializable.view(SchemaVersion1Dot5) @serializable.view(SchemaVersion1Dot6) @serializable.view(SchemaVersion1Dot7) - @serializable.xml_sequence(15) + @serializable.xml_sequence(16) def release_notes(self) -> Optional[ReleaseNotes]: """ Specifies optional release notes. @@ -369,7 +393,7 @@ def __comparable_tuple(self) -> _ComparableTuple: self.authenticated, _ComparableTuple(self.data), _ComparableTuple(self.endpoints), _ComparableTuple(self.external_references), _ComparableTuple(self.licenses), _ComparableTuple(self.properties), self.release_notes, _ComparableTuple(self.services), - self.x_trust_boundary + self.x_trust_boundary, _ComparableTuple(self.patent_assertions), )) def __eq__(self, other: object) -> bool: diff --git a/docs/responsibilities_capabilities.rst b/docs/responsibilities_capabilities.rst index 6a773585..2b3ea566 100644 --- a/docs/responsibilities_capabilities.rst +++ b/docs/responsibilities_capabilities.rst @@ -43,6 +43,7 @@ Capabilities * :mod:`License Named, SPDX, Expression, and more ` * :mod:`Lifecycle ` * :mod:`Model Card and Environmental Considerations ` + * :mod:`Patent, Patent Family and Patent Assertion ` * :mod:`Release Notes ` * :mod:`Service ` * :mod:`Tool ` diff --git a/tests/_data/models.py b/tests/_data/models.py index ebae62ca..603a2d23 100644 --- a/tests/_data/models.py +++ b/tests/_data/models.py @@ -18,7 +18,7 @@ import base64 import sys from collections.abc import Iterable -from datetime import datetime, timezone +from datetime import date, datetime, timezone from decimal import Decimal from inspect import getmembers, isfunction from typing import Any, Optional @@ -126,6 +126,14 @@ PerformanceMetric, QuantitativeAnalysis, ) +from cyclonedx.model.patent import ( + Patent, + PatentAssertion, + PatentAssertionType, + PatentFamily, + PatentLegalStatus, + PriorityApplication, +) from cyclonedx.model.release_note import ReleaseNotes from cyclonedx.model.service import Service from cyclonedx.model.tool import Tool, ToolRepository @@ -1708,6 +1716,113 @@ def get_bom_with_definitions_and_detailed_standards() -> Bom: ])) +def get_bom_with_patents() -> Bom: + """ + Returns a BOM with patents, patent families and patent assertions on components and services. + Patent information is available since CycloneDX 1.7. + """ + patent1 = Patent( + bom_ref='patent-1', + patent_number='US11000001B2', + application_number='US16000001', + jurisdiction='US', + priority_application=PriorityApplication( + application_number='US62000001', + jurisdiction='US', + filing_date=date(2018, 3, 1), + ), + publication_number='US20190000001A1', + title='Method for describing software bills of materials', + abstract='A method for describing software components and their relationships.', + filing_date=date(2019, 2, 28), + grant_date=date(2021, 5, 11), + patent_expiration_date=date(2039, 2, 28), + patent_legal_status=PatentLegalStatus.IN_FORCE, + patent_assignees=[get_org_entity_1(), get_org_contact_2()], + external_references=[get_external_reference_1()], + ) + patent2 = Patent( + bom_ref='patent-2', + patent_number='EP3000001', + jurisdiction='EP', + patent_legal_status=PatentLegalStatus.PENDING, + ) + family = PatentFamily( + bom_ref='patent-family-1', + family_id='FAM-0001', + priority_application=PriorityApplication( + application_number='US62000001', + jurisdiction='US', + filing_date=date(2018, 3, 1), + ), + members=[patent1.bom_ref, patent2.bom_ref], + external_references=[get_external_reference_2()], + ) + return _make_bom( + definitions=Definitions( + standards=[Standard( + bom_ref='some-standard', name='Some Standard', version='1.2.3', + description='Some description', owner='Some Owner', + )], + patents=[family, patent2, patent1], + ), + components=[ + Component( + name='patented-component', type=ComponentType.LIBRARY, bom_ref='patented-component', + patent_assertions=[ + PatentAssertion( + bom_ref='patent-assertion-1', + assertion_type=PatentAssertionType.OWNERSHIP, + patent_refs=[patent1.bom_ref, family.bom_ref], + asserter=OrganizationalEntity( + bom_ref='org-patent-owner', name='Patent Owner Inc.', + urls=[XsUri('https://patent-owner.example')], + ), + notes='Owned by the manufacturer.', + ), + PatentAssertion( + assertion_type=PatentAssertionType.LICENSE, + patent_refs=[patent2.bom_ref], + asserter=get_org_contact_2(), + ), + ], + ), + ], + services=[ + Service( + name='patented-service', bom_ref='patented-service', + patent_assertions=[ + PatentAssertion( + assertion_type=PatentAssertionType.NON_ASSERTION, + asserter=BomRef('org-patent-owner'), + ), + ], + ), + ], + dependencies=[ + Dependency(ref=BomRef('patented-component')), + Dependency(ref=BomRef('patented-service')), + ], + ) + + +def get_bom_with_definitions_patents_only() -> Bom: + """ + Returns a BOM with definitions that hold patents only. + Prior to CycloneDX 1.7, no `definitions` must be rendered at all. + """ + return _make_bom( + definitions=Definitions(patents=[ + Patent( + bom_ref='patent-1', + patent_number='US11000001B2', + jurisdiction='US', + patent_legal_status=PatentLegalStatus.GRANTED, + ), + ]), + ) + + def get_bom_for_issue540_duplicate_components() -> Bom: # tests https://github.com/CycloneDX/cyclonedx-python-lib/issues/540 bom = _make_bom() diff --git a/tests/_data/snapshots/enum_PatentAssertionType-1.7.json.bin b/tests/_data/snapshots/enum_PatentAssertionType-1.7.json.bin new file mode 100644 index 00000000..7f9935d0 --- /dev/null +++ b/tests/_data/snapshots/enum_PatentAssertionType-1.7.json.bin @@ -0,0 +1,106 @@ +{ + "components": [ + { + "bom-ref": "dummy", + "name": "dummy", + "patentAssertions": [ + { + "asserter": { + "name": "test-org", + "url": [ + "https://example.com" + ] + }, + "assertionType": "exclusive-rights" + }, + { + "asserter": { + "name": "test-org", + "url": [ + "https://example.com" + ] + }, + "assertionType": "license" + }, + { + "asserter": { + "name": "test-org", + "url": [ + "https://example.com" + ] + }, + "assertionType": "non-assertion" + }, + { + "asserter": { + "name": "test-org", + "url": [ + "https://example.com" + ] + }, + "assertionType": "ownership" + }, + { + "asserter": { + "name": "test-org", + "url": [ + "https://example.com" + ] + }, + "assertionType": "prior-art" + }, + { + "asserter": { + "name": "test-org", + "url": [ + "https://example.com" + ] + }, + "assertionType": "research-or-evaluation" + }, + { + "asserter": { + "name": "test-org", + "url": [ + "https://example.com" + ] + }, + "assertionType": "standards-inclusion" + }, + { + "asserter": { + "name": "test-org", + "url": [ + "https://example.com" + ] + }, + "assertionType": "third-party-claim" + } + ], + "type": "library" + } + ], + "dependencies": [ + { + "ref": "dummy" + } + ], + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "properties": [ + { + "name": "key1", + "value": "val1" + }, + { + "name": "key2", + "value": "val2" + } + ], + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.7.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.7" +} \ No newline at end of file diff --git a/tests/_data/snapshots/enum_PatentAssertionType-1.7.xml.bin b/tests/_data/snapshots/enum_PatentAssertionType-1.7.xml.bin new file mode 100644 index 00000000..88af9830 --- /dev/null +++ b/tests/_data/snapshots/enum_PatentAssertionType-1.7.xml.bin @@ -0,0 +1,92 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + + dummy + + + exclusive-rights + + + test-org + https://example.com + + + + + license + + + test-org + https://example.com + + + + + non-assertion + + + test-org + https://example.com + + + + + ownership + + + test-org + https://example.com + + + + + prior-art + + + test-org + https://example.com + + + + + research-or-evaluation + + + test-org + https://example.com + + + + + standards-inclusion + + + test-org + https://example.com + + + + + third-party-claim + + + test-org + https://example.com + + + + + + + + + + + val1 + val2 + + diff --git a/tests/_data/snapshots/enum_PatentLegalStatus-1.7.json.bin b/tests/_data/snapshots/enum_PatentLegalStatus-1.7.json.bin new file mode 100644 index 00000000..9e10f238 --- /dev/null +++ b/tests/_data/snapshots/enum_PatentLegalStatus-1.7.json.bin @@ -0,0 +1,102 @@ +{ + "definitions": { + "patents": [ + { + "bom-ref": "dummy-PLS:PENDING", + "jurisdiction": "US", + "patentLegalStatus": "pending", + "patentNumber": "US0000000" + }, + { + "bom-ref": "dummy-PLS:GRANTED", + "jurisdiction": "US", + "patentLegalStatus": "granted", + "patentNumber": "US0000001" + }, + { + "bom-ref": "dummy-PLS:REVOKED", + "jurisdiction": "US", + "patentLegalStatus": "revoked", + "patentNumber": "US0000002" + }, + { + "bom-ref": "dummy-PLS:EXPIRED", + "jurisdiction": "US", + "patentLegalStatus": "expired", + "patentNumber": "US0000003" + }, + { + "bom-ref": "dummy-PLS:LAPSED", + "jurisdiction": "US", + "patentLegalStatus": "lapsed", + "patentNumber": "US0000004" + }, + { + "bom-ref": "dummy-PLS:WITHDRAWN", + "jurisdiction": "US", + "patentLegalStatus": "withdrawn", + "patentNumber": "US0000005" + }, + { + "bom-ref": "dummy-PLS:ABANDONED", + "jurisdiction": "US", + "patentLegalStatus": "abandoned", + "patentNumber": "US0000006" + }, + { + "bom-ref": "dummy-PLS:SUSPENDED", + "jurisdiction": "US", + "patentLegalStatus": "suspended", + "patentNumber": "US0000007" + }, + { + "bom-ref": "dummy-PLS:REINSTATED", + "jurisdiction": "US", + "patentLegalStatus": "reinstated", + "patentNumber": "US0000008" + }, + { + "bom-ref": "dummy-PLS:OPPOSED", + "jurisdiction": "US", + "patentLegalStatus": "opposed", + "patentNumber": "US0000009" + }, + { + "bom-ref": "dummy-PLS:TERMINATED", + "jurisdiction": "US", + "patentLegalStatus": "terminated", + "patentNumber": "US0000010" + }, + { + "bom-ref": "dummy-PLS:INVALIDATED", + "jurisdiction": "US", + "patentLegalStatus": "invalidated", + "patentNumber": "US0000011" + }, + { + "bom-ref": "dummy-PLS:IN_FORCE", + "jurisdiction": "US", + "patentLegalStatus": "in-force", + "patentNumber": "US0000012" + } + ] + }, + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "properties": [ + { + "name": "key1", + "value": "val1" + }, + { + "name": "key2", + "value": "val2" + } + ], + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.7.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.7" +} \ No newline at end of file diff --git a/tests/_data/snapshots/enum_PatentLegalStatus-1.7.xml.bin b/tests/_data/snapshots/enum_PatentLegalStatus-1.7.xml.bin new file mode 100644 index 00000000..b215eec1 --- /dev/null +++ b/tests/_data/snapshots/enum_PatentLegalStatus-1.7.xml.bin @@ -0,0 +1,79 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + val1 + val2 + + + + + US0000000 + US + pending + + + US0000001 + US + granted + + + US0000002 + US + revoked + + + US0000003 + US + expired + + + US0000004 + US + lapsed + + + US0000005 + US + withdrawn + + + US0000006 + US + abandoned + + + US0000007 + US + suspended + + + US0000008 + US + reinstated + + + US0000009 + US + opposed + + + US0000010 + US + terminated + + + US0000011 + US + invalidated + + + US0000012 + US + in-force + + + + diff --git a/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.0.xml.bin b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.0.xml.bin new file mode 100644 index 00000000..acb06612 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.0.xml.bin @@ -0,0 +1,4 @@ + + + + diff --git a/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.1.xml.bin b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.1.xml.bin new file mode 100644 index 00000000..55ef5cda --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.1.xml.bin @@ -0,0 +1,4 @@ + + + + diff --git a/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.2.json.bin b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.2.json.bin new file mode 100644 index 00000000..8f473bd3 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.2.json.bin @@ -0,0 +1,10 @@ +{ + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.2b.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.2" +} \ No newline at end of file diff --git a/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.2.xml.bin b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.2.xml.bin new file mode 100644 index 00000000..df1938ec --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.2.xml.bin @@ -0,0 +1,6 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + diff --git a/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.3.json.bin b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.3.json.bin new file mode 100644 index 00000000..02943890 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.3.json.bin @@ -0,0 +1,10 @@ +{ + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.3a.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.3" +} \ No newline at end of file diff --git a/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.3.xml.bin b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.3.xml.bin new file mode 100644 index 00000000..8341ff60 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.3.xml.bin @@ -0,0 +1,6 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + diff --git a/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.4.json.bin b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.4.json.bin new file mode 100644 index 00000000..48f1745d --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.4.json.bin @@ -0,0 +1,10 @@ +{ + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.4.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.4" +} \ No newline at end of file diff --git a/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.4.xml.bin b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.4.xml.bin new file mode 100644 index 00000000..d0a7d4c9 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.4.xml.bin @@ -0,0 +1,6 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + diff --git a/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.5.json.bin b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.5.json.bin new file mode 100644 index 00000000..57b5e590 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.5.json.bin @@ -0,0 +1,20 @@ +{ + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "properties": [ + { + "name": "key1", + "value": "val1" + }, + { + "name": "key2", + "value": "val2" + } + ], + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.5.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.5" +} \ No newline at end of file diff --git a/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.5.xml.bin b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.5.xml.bin new file mode 100644 index 00000000..f952637c --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.5.xml.bin @@ -0,0 +1,10 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + val1 + val2 + + diff --git a/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.6.json.bin b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.6.json.bin new file mode 100644 index 00000000..b93790d4 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.6.json.bin @@ -0,0 +1,20 @@ +{ + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "properties": [ + { + "name": "key1", + "value": "val1" + }, + { + "name": "key2", + "value": "val2" + } + ], + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.6" +} \ No newline at end of file diff --git a/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.6.xml.bin b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.6.xml.bin new file mode 100644 index 00000000..6faca474 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.6.xml.bin @@ -0,0 +1,10 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + val1 + val2 + + diff --git a/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.7.json.bin b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.7.json.bin new file mode 100644 index 00000000..bd165c55 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.7.json.bin @@ -0,0 +1,30 @@ +{ + "definitions": { + "patents": [ + { + "bom-ref": "patent-1", + "jurisdiction": "US", + "patentLegalStatus": "granted", + "patentNumber": "US11000001B2" + } + ] + }, + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "properties": [ + { + "name": "key1", + "value": "val1" + }, + { + "name": "key2", + "value": "val2" + } + ], + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.7.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.7" +} \ No newline at end of file diff --git a/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.7.xml.bin b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.7.xml.bin new file mode 100644 index 00000000..a001e290 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_definitions_patents_only-1.7.xml.bin @@ -0,0 +1,19 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + val1 + val2 + + + + + US11000001B2 + US + granted + + + + diff --git a/tests/_data/snapshots/get_bom_with_patents-1.0.xml.bin b/tests/_data/snapshots/get_bom_with_patents-1.0.xml.bin new file mode 100644 index 00000000..cb7f9dc5 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_patents-1.0.xml.bin @@ -0,0 +1,10 @@ + + + + + patented-component + + false + + + diff --git a/tests/_data/snapshots/get_bom_with_patents-1.1.xml.bin b/tests/_data/snapshots/get_bom_with_patents-1.1.xml.bin new file mode 100644 index 00000000..c6191d5c --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_patents-1.1.xml.bin @@ -0,0 +1,9 @@ + + + + + patented-component + + + + diff --git a/tests/_data/snapshots/get_bom_with_patents-1.2.json.bin b/tests/_data/snapshots/get_bom_with_patents-1.2.json.bin new file mode 100644 index 00000000..3c8d2e80 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_patents-1.2.json.bin @@ -0,0 +1,32 @@ +{ + "components": [ + { + "bom-ref": "patented-component", + "name": "patented-component", + "type": "library", + "version": "" + } + ], + "dependencies": [ + { + "ref": "patented-component" + }, + { + "ref": "patented-service" + } + ], + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "services": [ + { + "bom-ref": "patented-service", + "name": "patented-service" + } + ], + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.2b.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.2" +} \ No newline at end of file diff --git a/tests/_data/snapshots/get_bom_with_patents-1.2.xml.bin b/tests/_data/snapshots/get_bom_with_patents-1.2.xml.bin new file mode 100644 index 00000000..98aaa0ba --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_patents-1.2.xml.bin @@ -0,0 +1,21 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + + patented-component + + + + + + patented-service + + + + + + + diff --git a/tests/_data/snapshots/get_bom_with_patents-1.3.json.bin b/tests/_data/snapshots/get_bom_with_patents-1.3.json.bin new file mode 100644 index 00000000..9b62d185 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_patents-1.3.json.bin @@ -0,0 +1,32 @@ +{ + "components": [ + { + "bom-ref": "patented-component", + "name": "patented-component", + "type": "library", + "version": "" + } + ], + "dependencies": [ + { + "ref": "patented-component" + }, + { + "ref": "patented-service" + } + ], + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "services": [ + { + "bom-ref": "patented-service", + "name": "patented-service" + } + ], + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.3a.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.3" +} \ No newline at end of file diff --git a/tests/_data/snapshots/get_bom_with_patents-1.3.xml.bin b/tests/_data/snapshots/get_bom_with_patents-1.3.xml.bin new file mode 100644 index 00000000..405ff8d9 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_patents-1.3.xml.bin @@ -0,0 +1,21 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + + patented-component + + + + + + patented-service + + + + + + + diff --git a/tests/_data/snapshots/get_bom_with_patents-1.4.json.bin b/tests/_data/snapshots/get_bom_with_patents-1.4.json.bin new file mode 100644 index 00000000..b3e44239 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_patents-1.4.json.bin @@ -0,0 +1,31 @@ +{ + "components": [ + { + "bom-ref": "patented-component", + "name": "patented-component", + "type": "library" + } + ], + "dependencies": [ + { + "ref": "patented-component" + }, + { + "ref": "patented-service" + } + ], + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "services": [ + { + "bom-ref": "patented-service", + "name": "patented-service" + } + ], + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.4.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.4" +} \ No newline at end of file diff --git a/tests/_data/snapshots/get_bom_with_patents-1.4.xml.bin b/tests/_data/snapshots/get_bom_with_patents-1.4.xml.bin new file mode 100644 index 00000000..facf47bc --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_patents-1.4.xml.bin @@ -0,0 +1,20 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + + patented-component + + + + + patented-service + + + + + + + diff --git a/tests/_data/snapshots/get_bom_with_patents-1.5.json.bin b/tests/_data/snapshots/get_bom_with_patents-1.5.json.bin new file mode 100644 index 00000000..7666ab1e --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_patents-1.5.json.bin @@ -0,0 +1,41 @@ +{ + "components": [ + { + "bom-ref": "patented-component", + "name": "patented-component", + "type": "library" + } + ], + "dependencies": [ + { + "ref": "patented-component" + }, + { + "ref": "patented-service" + } + ], + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "properties": [ + { + "name": "key1", + "value": "val1" + }, + { + "name": "key2", + "value": "val2" + } + ], + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "services": [ + { + "bom-ref": "patented-service", + "name": "patented-service" + } + ], + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.5.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.5" +} \ No newline at end of file diff --git a/tests/_data/snapshots/get_bom_with_patents-1.5.xml.bin b/tests/_data/snapshots/get_bom_with_patents-1.5.xml.bin new file mode 100644 index 00000000..942e0958 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_patents-1.5.xml.bin @@ -0,0 +1,24 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + + patented-component + + + + + patented-service + + + + + + + + val1 + val2 + + diff --git a/tests/_data/snapshots/get_bom_with_patents-1.6.json.bin b/tests/_data/snapshots/get_bom_with_patents-1.6.json.bin new file mode 100644 index 00000000..9c030a4f --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_patents-1.6.json.bin @@ -0,0 +1,52 @@ +{ + "components": [ + { + "bom-ref": "patented-component", + "name": "patented-component", + "type": "library" + } + ], + "definitions": { + "standards": [ + { + "bom-ref": "some-standard", + "description": "Some description", + "name": "Some Standard", + "owner": "Some Owner", + "version": "1.2.3" + } + ] + }, + "dependencies": [ + { + "ref": "patented-component" + }, + { + "ref": "patented-service" + } + ], + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "properties": [ + { + "name": "key1", + "value": "val1" + }, + { + "name": "key2", + "value": "val2" + } + ], + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "services": [ + { + "bom-ref": "patented-service", + "name": "patented-service" + } + ], + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.6" +} \ No newline at end of file diff --git a/tests/_data/snapshots/get_bom_with_patents-1.6.xml.bin b/tests/_data/snapshots/get_bom_with_patents-1.6.xml.bin new file mode 100644 index 00000000..bc7ca25e --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_patents-1.6.xml.bin @@ -0,0 +1,34 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + + patented-component + + + + + patented-service + + + + + + + + val1 + val2 + + + + + Some Standard + 1.2.3 + Some description + Some Owner + + + + diff --git a/tests/_data/snapshots/get_bom_with_patents-1.7.json.bin b/tests/_data/snapshots/get_bom_with_patents-1.7.json.bin new file mode 100644 index 00000000..c182f594 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_patents-1.7.json.bin @@ -0,0 +1,176 @@ +{ + "components": [ + { + "bom-ref": "patented-component", + "name": "patented-component", + "patentAssertions": [ + { + "asserter": { + "email": "someone@somewhere.tld", + "name": "A N Other", + "phone": "+44 (0)1234 567890" + }, + "assertionType": "license", + "patentRefs": [ + "patent-2" + ] + }, + { + "asserter": { + "bom-ref": "org-patent-owner", + "name": "Patent Owner Inc.", + "url": [ + "https://patent-owner.example" + ] + }, + "assertionType": "ownership", + "bom-ref": "patent-assertion-1", + "notes": "Owned by the manufacturer.", + "patentRefs": [ + "patent-1", + "patent-family-1" + ] + } + ], + "type": "library" + } + ], + "definitions": { + "patents": [ + { + "bom-ref": "patent-2", + "jurisdiction": "EP", + "patentLegalStatus": "pending", + "patentNumber": "EP3000001" + }, + { + "abstract": "A method for describing software components and their relationships.", + "applicationNumber": "US16000001", + "bom-ref": "patent-1", + "externalReferences": [ + { + "comment": "No comment", + "hashes": [ + { + "alg": "SHA-256", + "content": "806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b" + } + ], + "type": "distribution", + "url": "https://cyclonedx.org" + } + ], + "filingDate": "2019-02-28", + "grantDate": "2021-05-11", + "jurisdiction": "US", + "patentAssignee": [ + { + "address": { + "country": "GB", + "locality": "Cheshire", + "region": "England", + "streetAddress": "100 Main Street" + }, + "contact": [ + { + "email": "someone@somewhere.tld", + "name": "A N Other", + "phone": "+44 (0)1234 567890" + }, + { + "email": "paul.horton@owasp.org", + "name": "Paul Horton" + } + ], + "name": "CycloneDX", + "url": [ + "https://cyclonedx.org", + "https://cyclonedx.org/docs" + ] + }, + { + "email": "someone@somewhere.tld", + "name": "A N Other", + "phone": "+44 (0)1234 567890" + } + ], + "patentExpirationDate": "2039-02-28", + "patentLegalStatus": "in-force", + "patentNumber": "US11000001B2", + "priorityApplication": { + "applicationNumber": "US62000001", + "filingDate": "2018-03-01", + "jurisdiction": "US" + }, + "publicationNumber": "US20190000001A1", + "title": "Method for describing software bills of materials" + }, + { + "bom-ref": "patent-family-1", + "externalReferences": [ + { + "type": "website", + "url": "https://cyclonedx.org" + } + ], + "familyId": "FAM-0001", + "members": [ + "patent-1", + "patent-2" + ], + "priorityApplication": { + "applicationNumber": "US62000001", + "filingDate": "2018-03-01", + "jurisdiction": "US" + } + } + ], + "standards": [ + { + "bom-ref": "some-standard", + "description": "Some description", + "name": "Some Standard", + "owner": "Some Owner", + "version": "1.2.3" + } + ] + }, + "dependencies": [ + { + "ref": "patented-component" + }, + { + "ref": "patented-service" + } + ], + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "properties": [ + { + "name": "key1", + "value": "val1" + }, + { + "name": "key2", + "value": "val2" + } + ], + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "services": [ + { + "bom-ref": "patented-service", + "name": "patented-service", + "patentAssertions": [ + { + "asserter": "org-patent-owner", + "assertionType": "non-assertion" + } + ] + } + ], + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.7.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.7" +} \ No newline at end of file diff --git a/tests/_data/snapshots/get_bom_with_patents-1.7.xml.bin b/tests/_data/snapshots/get_bom_with_patents-1.7.xml.bin new file mode 100644 index 00000000..e76a5206 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_patents-1.7.xml.bin @@ -0,0 +1,150 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + + patented-component + + + license + + patent-2 + + + + A N Other + someone@somewhere.tld + +44 (0)1234 567890 + + + + + ownership + + patent-1 + patent-family-1 + + + + Patent Owner Inc. + https://patent-owner.example + + + Owned by the manufacturer. + + + + + + + patented-service + + + non-assertion + + org-patent-owner + + + + + + + + + + + val1 + val2 + + + + + Some Standard + 1.2.3 + Some description + Some Owner + + + + + EP3000001 + EP + pending + + + US11000001B2 + US16000001 + US + + US62000001 + US + 2018-03-01 + + US20190000001A1 + Method for describing software bills of materials + A method for describing software components and their relationships. + 2019-02-28 + 2021-05-11 + 2039-02-28 + in-force + + + CycloneDX +
+ GB + England + Cheshire + 100 Main Street +
+ https://cyclonedx.org + https://cyclonedx.org/docs + + A N Other + someone@somewhere.tld + +44 (0)1234 567890 + + + Paul Horton + paul.horton@owasp.org + +
+
+ + + A N Other + someone@somewhere.tld + +44 (0)1234 567890 + + + + + https://cyclonedx.org + No comment + + 806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b + + + +
+ + FAM-0001 + + US62000001 + US + 2018-03-01 + + + patent-1 + patent-2 + + + + https://cyclonedx.org + + + +
+
+
diff --git a/tests/test_enums.py b/tests/test_enums.py index ae050bab..f7c731ae 100644 --- a/tests/test_enums.py +++ b/tests/test_enums.py @@ -43,6 +43,7 @@ ProtocolProperties, RelatedCryptoMaterialProperties, ) +from cyclonedx.model.definition import Definitions from cyclonedx.model.issue import IssueType from cyclonedx.model.license import DisjunctiveLicense from cyclonedx.model.lifecycle import LifecyclePhase, PredefinedLifecycle @@ -57,6 +58,7 @@ ModelCard, ModelParameters, ) +from cyclonedx.model.patent import Patent, PatentAssertion from cyclonedx.model.service import DataClassification, Service from cyclonedx.model.vulnerability import ( BomTarget, @@ -125,6 +127,10 @@ EnergySource, MachineLearningApproach, ) +from cyclonedx.model.patent import ( # isort:skip + PatentAssertionType, + PatentLegalStatus, +) # endregion SUT @@ -1197,6 +1203,46 @@ def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, super()._test_cases_render(bom, of, sv) +@ddt +class TestEnumPatentLegalStatus(_EnumTestCase): + + @idata(set(chain( + dp_cases_from_xml_schemas(f"./{SCHEMA_NS}simpleType[@name='patentLegalStatusEnum']"), + dp_cases_from_json_schemas('definitions', 'patent', 'properties', 'patentLegalStatus'), + ))) + def test_knows_value(self, value: str) -> None: + super()._test_knows_value(PatentLegalStatus, value) + + @named_data(*(d for d in NAMED_OF_SV if d[2] >= SchemaVersion.V1_7)) + def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, **__: Any) -> None: + bom = _make_bom(definitions=Definitions(patents=( + Patent(bom_ref=f'dummy-PLS:{pls.name}', patent_number=f'US{i:07d}', jurisdiction='US', + patent_legal_status=pls) + for i, pls in enumerate(PatentLegalStatus) + ))) + super()._test_cases_render(bom, of, sv) + + +@ddt +class TestEnumPatentAssertionType(_EnumTestCase): + + @idata(set(chain( + dp_cases_from_xml_schemas(f"./{SCHEMA_NS}simpleType[@name='patentAssertionTypeEnum']"), + dp_cases_from_json_schemas('definitions', 'patentAssertions', 'items', 'properties', 'assertionType'), + ))) + def test_knows_value(self, value: str) -> None: + super()._test_knows_value(PatentAssertionType, value) + + @named_data(*(d for d in NAMED_OF_SV if d[2] >= SchemaVersion.V1_7)) + def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, **__: Any) -> None: + bom = _make_bom(components=[Component(name='dummy', bom_ref='dummy', patent_assertions=( + PatentAssertion(assertion_type=pat, + asserter=OrganizationalEntity(name='test-org', urls=[XsUri('https://example.com')])) + for pat in PatentAssertionType + ))]) + super()._test_cases_render(bom, of, sv) + + # add new test cases above this line diff --git a/tests/test_model_patent.py b/tests/test_model_patent.py new file mode 100644 index 00000000..2da02e88 --- /dev/null +++ b/tests/test_model_patent.py @@ -0,0 +1,214 @@ +# This file is part of CycloneDX Python Library +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) OWASP Foundation. All Rights Reserved. + + +from datetime import date +from unittest import TestCase +from xml.etree.ElementTree import Element, SubElement # nosec B405 + +from cyclonedx.exception.serialization import CycloneDxDeserializationException, SerializationOfUnexpectedValueException +from cyclonedx.model import XsUri +from cyclonedx.model.bom_ref import BomRef +from cyclonedx.model.contact import OrganizationalContact, OrganizationalEntity +from cyclonedx.model.definition import Definitions +from cyclonedx.model.patent import ( + Patent, + PatentAssertion, + PatentAssertionType, + PatentFamily, + PatentLegalStatus, + PriorityApplication, + _PatentAsserterSerializationHelper, + _PatentAssigneesSerializationHelper, + _PatentsRepositorySerializationHelper, +) +from cyclonedx.schema.schema import SchemaVersion1Dot7 + + +def _patent(number: str = 'US1234567', **kwargs: object) -> Patent: + return Patent(patent_number=number, jurisdiction='US', patent_legal_status=PatentLegalStatus.GRANTED, + **kwargs) # type:ignore[arg-type] + + +class TestModelPriorityApplication(TestCase): + + def test_equal_and_hash(self) -> None: + pa1 = PriorityApplication(application_number='US62000001', jurisdiction='US', filing_date=date(2018, 3, 1)) + pa2 = PriorityApplication(application_number='US62000001', jurisdiction='US', filing_date=date(2018, 3, 1)) + self.assertEqual(pa1, pa2) + self.assertEqual(hash(pa1), hash(pa2)) + self.assertNotEqual(pa1, 'other') + + def test_sort(self) -> None: + pa1 = PriorityApplication(application_number='A1', jurisdiction='EP', filing_date=date(2018, 3, 1)) + pa2 = PriorityApplication(application_number='A1', jurisdiction='US', filing_date=date(2018, 3, 1)) + pa3 = PriorityApplication(application_number='A2', jurisdiction='US', filing_date=date(2018, 3, 1)) + self.assertListEqual([pa1, pa2, pa3], sorted([pa3, pa1, pa2])) + + +class TestModelPatent(TestCase): + + def test_minimal(self) -> None: + p = _patent() + self.assertEqual('US1234567', p.patent_number) + self.assertEqual('US', p.jurisdiction) + self.assertIs(PatentLegalStatus.GRANTED, p.patent_legal_status) + self.assertFalse(p.bom_ref) + self.assertEqual(0, len(p.patent_assignees)) + self.assertEqual(0, len(p.external_references)) + self.assertIsNone(p.patent_expiration_date) + + def test_equal_and_hash(self) -> None: + p1 = _patent(bom_ref='p', title='t') + p2 = _patent(bom_ref='p', title='t') + self.assertEqual(p1, p2) + self.assertEqual(hash(p1), hash(p2)) + self.assertNotEqual(p1, _patent(bom_ref='p', title='other')) + self.assertNotEqual(p1, 'other') + + def test_mixed_assignees_are_sortable(self) -> None: + org = OrganizationalEntity(name='ACME') + person = OrganizationalContact(name='Jane', email='jane@example.com') + p = _patent(patent_assignees=[person, org]) + self.assertListEqual([org, person], list(p.patent_assignees)) + self.assertEqual(p, _patent(patent_assignees=[org, person])) + + def test_sort_patents_before_families(self) -> None: + f1 = PatentFamily(family_id='F1') + p1 = _patent('US1') + p2 = _patent('US2') + self.assertListEqual([p1, p2, f1], sorted([f1, p2, p1])) + self.assertListEqual([p1, p2, f1], list(Definitions(patents=[f1, p2, p1]).patents)) + + def test_compare_other_type(self) -> None: + with self.assertRaises(TypeError): + _ = _patent() < 'other' # type:ignore[operator] + + +class TestModelPatentFamily(TestCase): + + def test_members(self) -> None: + p1 = _patent('US1', bom_ref='p1') + p2 = _patent('US2', bom_ref='p2') + f = PatentFamily(family_id='F1', members=[p2.bom_ref, p1.bom_ref]) + self.assertListEqual([p1.bom_ref, p2.bom_ref], list(f.members)) + + def test_equal_and_hash(self) -> None: + f1 = PatentFamily(family_id='F1', members=[BomRef('a')]) + f2 = PatentFamily(family_id='F1', members=[BomRef('a')]) + self.assertEqual(f1, f2) + self.assertEqual(hash(f1), hash(f2)) + self.assertNotEqual(f1, PatentFamily(family_id='F2')) + self.assertNotEqual(f1, 'other') + + +class TestModelPatentAssertion(TestCase): + + def test_mixed_asserters_are_sortable(self) -> None: + a1 = PatentAssertion(assertion_type=PatentAssertionType.LICENSE, asserter=BomRef('org')) + a2 = PatentAssertion(assertion_type=PatentAssertionType.LICENSE, asserter=OrganizationalEntity(name='ACME')) + a3 = PatentAssertion(assertion_type=PatentAssertionType.LICENSE, + asserter=OrganizationalContact(name='Jane')) + self.assertListEqual([a2, a3, a1], sorted([a1, a3, a2])) + + def test_equal_and_hash(self) -> None: + a1 = PatentAssertion(assertion_type=PatentAssertionType.OWNERSHIP, asserter=BomRef('org'), + patent_refs=[BomRef('p1')], notes='n') + a2 = PatentAssertion(assertion_type=PatentAssertionType.OWNERSHIP, asserter=BomRef('org'), + patent_refs=[BomRef('p1')], notes='n') + self.assertEqual(a1, a2) + self.assertEqual(hash(a1), hash(a2)) + self.assertNotEqual(a1, 'other') + + +class TestModelDefinitionsWithPatents(TestCase): + + def test_patents_only_is_truthy(self) -> None: + self.assertFalse(Definitions()) + self.assertTrue(Definitions(patents=[_patent()])) + + def test_unequal_patents(self) -> None: + self.assertNotEqual(Definitions(patents=[_patent('US1')]), Definitions(patents=[_patent('US2')])) + + +class TestPatentSerializationHelpers(TestCase): + + def test_json_party_denormalize_heuristic(self) -> None: + h = _PatentAsserterSerializationHelper + self.assertIsInstance(h.json_denormalize({'name': 'Jane', 'email': 'jane@example.com'}), + OrganizationalContact) + self.assertIsInstance(h.json_denormalize({'name': 'Jane', 'phone': '123'}), OrganizationalContact) + self.assertIsInstance(h.json_denormalize({'name': 'ACME', 'url': ['https://acme.example']}), + OrganizationalEntity) + # ambiguous: falls back to organization + self.assertIsInstance(h.json_denormalize({'name': 'ACME'}), OrganizationalEntity) + self.assertEqual(BomRef('org'), h.json_denormalize('org')) + + def test_asserter_unexpected(self) -> None: + h = _PatentAsserterSerializationHelper + with self.assertRaises(CycloneDxDeserializationException): + h.json_denormalize(42) + with self.assertRaises(SerializationOfUnexpectedValueException): + h.json_normalize('foo', view=SchemaVersion1Dot7) # type:ignore[arg-type] + with self.assertRaises(SerializationOfUnexpectedValueException): + h.xml_normalize('foo', element_name='asserter', view=SchemaVersion1Dot7, # type:ignore[arg-type] + xmlns=None) + with self.assertRaises(CycloneDxDeserializationException): + h.xml_denormalize(Element('asserter'), default_ns=None) + + def test_assignees_unexpected(self) -> None: + h = _PatentAssigneesSerializationHelper + with self.assertRaises(SerializationOfUnexpectedValueException): + h.xml_normalize([BomRef('foo')], element_name='x', view=SchemaVersion1Dot7, # type:ignore[list-item] + xmlns=None) + container = Element('patentAssignees') + SubElement(SubElement(container, 'patentAssignee'), 'foo') + with self.assertRaises(CycloneDxDeserializationException): + h.xml_denormalize(container, default_ns=None) + + def test_assignees_xml_roundtrip_without_namespace(self) -> None: + org = OrganizationalEntity(name='ACME', urls=[XsUri('https://acme.example')]) + person = OrganizationalContact(name='Jane', email='jane@example.com') + h = _PatentsRepositorySerializationHelper + elem = h.xml_normalize([_patent(patent_assignees=[org, person])], + element_name='patents', view=SchemaVersion1Dot7, xmlns=None) + assert elem is not None + patent_elem = elem[0] + self.assertListEqual( + ['patentNumber', 'jurisdiction', 'patentLegalStatus', 'patentAssignee', 'patentAssignee'], + [c.tag for c in patent_elem]) + self.assertListEqual(['organization', 'individual'], [c[0].tag for c in patent_elem[3:]]) + patents = h.xml_denormalize(elem, default_ns=None) + self.assertListEqual([_patent(patent_assignees=[org, person])], patents) + + def test_patents_unexpected(self) -> None: + h = _PatentsRepositorySerializationHelper + with self.assertRaises(CycloneDxDeserializationException): + h.json_denormalize([{'foo': 'bar'}]) + with self.assertRaises(SerializationOfUnexpectedValueException): + h.xml_normalize(['foo'], element_name='patents', view=SchemaVersion1Dot7, # type:ignore[list-item] + xmlns=None) + patents = Element('patents') + SubElement(patents, 'foo') + with self.assertRaises(CycloneDxDeserializationException): + h.xml_denormalize(patents, default_ns=None) + + def test_empty(self) -> None: + self.assertIsNone(_PatentsRepositorySerializationHelper.json_normalize([], view=SchemaVersion1Dot7)) + self.assertIsNone(_PatentsRepositorySerializationHelper.xml_normalize( + [], element_name='patents', view=SchemaVersion1Dot7, xmlns=None)) + self.assertIsNone(_PatentAssigneesSerializationHelper.json_normalize([], view=SchemaVersion1Dot7))