Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serialization filter: any one expressen preferred #371

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cyclonedx/model/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
SchemaVersion1Dot3,
SchemaVersion1Dot4,
)
from ..serialization import BomRefHelper, PackageUrl
from ..serialization import BomRefHelper, LicensesHelper, PackageUrl
from . import (
AttachedText,
ComparableTuple,
Expand Down Expand Up @@ -1028,11 +1028,12 @@ def hashes(self, hashes: Iterable[HashType]) -> None:
self._hashes = SortedSet(hashes)

@property # type: ignore[misc]
@serializable.type_mapping(LicensesHelper)
@serializable.view(SchemaVersion1Dot1)
@serializable.view(SchemaVersion1Dot2)
@serializable.view(SchemaVersion1Dot3)
@serializable.view(SchemaVersion1Dot4)
@serializable.xml_array(serializable.XmlArraySerializationType.FLAT, 'licenses')
# @serializable.xml_array(serializable.XmlArraySerializationType.NESTED, '')
@serializable.xml_sequence(10)
def licenses(self) -> "SortedSet[LicenseChoice]":
"""
Expand Down
23 changes: 23 additions & 0 deletions cyclonedx/serialization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
Set of helper classes for use with ``serializable`` when conducting (de-)serialization.
"""

import warnings
from typing import List
from uuid import UUID

# See https://github.com/package-url/packageurl-python/issues/65
from packageurl import PackageURL
from serializable.helpers import BaseHelper

from ..model import LicenseChoice
from ..model.bom_ref import BomRef


Expand Down Expand Up @@ -76,3 +79,23 @@ def deserialize(cls, o: object) -> UUID:
return UUID(str(o))
except ValueError:
raise ValueError(f'UUID string supplied ({o}) does not parse!')


class LicensesHelper(BaseHelper):

@classmethod
def serialize(cls, o: object) -> List[LicenseChoice]:
# need to call `list(o)`, because `o` could be any iterable.
licenses: List[LicenseChoice] = list(o) # type: ignore[call-overload]
if len(licenses) > 1:
expression = next((license for license in licenses if license.expression), None)
if expression:
warnings.warn(f'Licenses: found an expression {expression!r}, dropping the rest of: {licenses!r}',
RuntimeWarning)
return [expression]
return licenses

@classmethod
def deserialize(cls, o: object) -> List[LicenseChoice]:
raise NotImplementedError
return [LicenseChoice.from_json(data) for data in o]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

32% of developers fix this issue

E1101: Class 'LicenseChoice' has no 'from_json' member


ℹ️ Expand to see all @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

15% of developers fix this issue

vulture-100: unreachable code after 'raise'


ℹ️ Expand to see all @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.

49 changes: 5 additions & 44 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading