Skip to content

Commit

Permalink
prepare for JSON bom.properies (#231)
Browse files Browse the repository at this point in the history

Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck committed Feb 18, 2023
1 parent 4db6a6c commit c4e1711
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 25 deletions.
3 changes: 2 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ All notable changes to this project will be documented in this file.
* Enumeration-like classes were converted to native [PHP Enumerations](https://www.php.net/manual/en/language.types.enumerations.php) ([#140] via [#204])
* Added
* Support for CycloneDX schema/spec v1.4 ([#57] via [#65], [#118], [#123])
* Support for [properties](https://cyclonedx.org/use-cases/#properties--name-value-store) ([#228] via [#165], [#229])
* Support for [properties](https://cyclonedx.org/use-cases/#properties--name-value-store) ([#228] via [#165], [#229], [#231])
* Misc
* All class properties now enforce the correct types ([#6], [#114] via [#125])
This is considered a non-breaking change, because the types were already correctly annotated.
Expand Down Expand Up @@ -210,6 +210,7 @@ All notable changes to this project will be documented in this file.
[#204]: https://github.com/CycloneDX/cyclonedx-php-library/pull/204
[#228]: https://github.com/CycloneDX/cyclonedx-php-library/issues/228
[#229]: https://github.com/CycloneDX/cyclonedx-php-library/pull/229
[#231]: https://github.com/CycloneDX/cyclonedx-php-library/pull/231

## 1.6.3 - 2022-09-15

Expand Down
3 changes: 2 additions & 1 deletion src/Core/Serialization/DOM/Normalizers/BomNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use CycloneDX\Core\Models\Bom;
use CycloneDX\Core\Models\Metadata;
use CycloneDX\Core\Serialization\DOM\_BaseNormalizer;
use CycloneDX\Core\Spec\Format;
use DOMElement;

/**
Expand Down Expand Up @@ -145,7 +146,7 @@ private function normalizeDependencies(Bom $bom): ?DOMElement

private function normalizeProperties(PropertyRepository $properties): ?DOMElement
{
if (false === $this->getNormalizerFactory()->getSpec()->supportsBomProperties()) {
if (false === $this->getNormalizerFactory()->getSpec()->supportsBomProperties(Format::XML)) {
return null;
}

Expand Down
19 changes: 18 additions & 1 deletion src/Core/Serialization/JSON/Normalizers/BomNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
namespace CycloneDX\Core\Serialization\JSON\Normalizers;

use CycloneDX\Core\_helpers\Assert;
use CycloneDX\Core\Collections\PropertyRepository;
use CycloneDX\Core\Models\Bom;
use CycloneDX\Core\Models\Metadata;
use CycloneDX\Core\Serialization\JSON\_BaseNormalizer;
use CycloneDX\Core\Spec\Format;
use CycloneDX\Core\Spec\Version;

/**
Expand Down Expand Up @@ -64,9 +66,13 @@ public function normalize(Bom $bom): array
'version' => $bom->getVersion(),
'metadata' => $this->normalizeMetadata($bom->getMetadata()),
'components' => $factory->makeForComponentRepository()->normalize($bom->getComponents()),
// services
'externalReferences' => $this->normalizeExternalReferences($bom),
'dependencies' => $this->normalizeDependencies($bom),
// 'properties' => not supported, yet - see https://github.com/CycloneDX/specification/issues/130
// compositions
// vulnerabilities
'properties' => $this->normalizeProperties($bom->getProperties()),
// signature
],
Assert::isNotNull(...)
);
Expand Down Expand Up @@ -127,4 +133,15 @@ private function normalizeDependencies(Bom $bom): ?array
? null
: $data;
}

private function normalizeProperties(PropertyRepository $properties): ?array
{
if (false === $this->getNormalizerFactory()->getSpec()->supportsBomProperties(Format::JSON)) {
return null;
}

return 0 === \count($properties)
? null
: $this->getNormalizerFactory()->makeForPropertyRepository()->normalize($properties);
}
}
2 changes: 1 addition & 1 deletion src/Core/Spec/Spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ public function supportsComponentAuthor(): bool;

public function supportsComponentProperties(): bool;

public function supportsBomProperties(): bool;
public function supportsBomProperties(Format $format): bool;
}
14 changes: 8 additions & 6 deletions src/Core/Spec/SpecFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ abstract class SpecFactory
/**
* Create the appropriate {@see \CycloneDX\Core\Spec\Spec Specification} based on {@see \CycloneDX\Core\Spec\Version}.
*
* @psalm-assert Version::* $version
*
* @throws DomainException when $version was unsupported
*/
public static function makeForVersion(Version $version): Spec
Expand Down Expand Up @@ -120,7 +118,7 @@ public static function make1dot1(): Spec
false,
false,
false,
false,
[],
);
}

Expand Down Expand Up @@ -187,7 +185,7 @@ public static function make1dot2(): Spec
false,
true,
false,
false,
[],
);
}

Expand Down Expand Up @@ -254,7 +252,9 @@ public static function make1dot3(): Spec
true,
true,
true,
true,
[
Format::XML,
],
);
}

Expand Down Expand Up @@ -322,7 +322,9 @@ public static function make1dot4(): Spec
true,
true,
true,
true,
[
Format::XML,
],
);
}
}
7 changes: 4 additions & 3 deletions src/Core/Spec/_Spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class _Spec implements Spec
* @psalm-param list<ComponentType> $lComponentTypes
* @psalm-param list<HashAlgorithm> $lHashAlgorithms
* @psalm-param list<ExternalReferenceType> $lExternalReferenceTypes
* @psalm-param list<Format> $lFormatsSupportingBomProperties
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand All @@ -66,7 +67,7 @@ public function __construct(
private readonly bool $bMetadataProperties,
private readonly bool $bComponentAuthor,
private readonly bool $bComponentProperties,
private readonly bool $bBomProperties,
private readonly array $lFormatsSupportingBomProperties,
) {
}

Expand Down Expand Up @@ -150,8 +151,8 @@ public function supportsComponentProperties(): bool
return $this->bComponentProperties;
}

public function supportsBomProperties(): bool
public function supportsBomProperties(Format $format): bool
{
return $this->bBomProperties;
return \in_array($format, $this->lFormatsSupportingBomProperties, true);
}
}
4 changes: 2 additions & 2 deletions tests/Core/Spec/Spec1dot1Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ protected static function shouldSupportComponentProperties(): bool
return false;
}

protected static function shouldSupportBomProperties(): bool
protected static function shouldSupportBomProperties(): array
{
return false;
return [];
}
}
4 changes: 2 additions & 2 deletions tests/Core/Spec/Spec1dot2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ protected static function shouldSupportComponentProperties(): bool
return false;
}

protected static function shouldSupportBomProperties(): bool
protected static function shouldSupportBomProperties(): array
{
return false;
return [];
}
}
4 changes: 2 additions & 2 deletions tests/Core/Spec/Spec1dot3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ protected static function shouldSupportComponentProperties(): bool
return true;
}

protected static function shouldSupportBomProperties(): bool
protected static function shouldSupportBomProperties(): array
{
return true;
return [Format::XML];
}
}
4 changes: 2 additions & 2 deletions tests/Core/Spec/Spec1dot4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ protected static function shouldSupportComponentProperties(): bool
return true;
}

protected static function shouldSupportBomProperties(): bool
protected static function shouldSupportBomProperties(): array
{
return true;
return [Format::XML];
}
}
18 changes: 14 additions & 4 deletions tests/Core/Spec/SpecBaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,21 @@ final public function testSupportsComponentProperties(): void

abstract protected static function shouldSupportComponentProperties(): bool;

final public function testSupportsBomProperties(): void
#[\PHPUnit\Framework\Attributes\DataProvider('dpSupportsBomProperties')]
final public function testSupportsBomProperties(Format $format, bool $shouldSupportBomProperties): void
{
$isSupported = static::getSpec()->supportsBomProperties();
self::assertSame(static::shouldSupportBomProperties(), $isSupported);
$isSupported = static::getSpec()->supportsBomProperties($format);
self::assertSame($shouldSupportBomProperties, $isSupported);
}

abstract protected static function shouldSupportBomProperties(): bool;
final public static function dpSupportsBomProperties(): Generator
{
$should = static::shouldSupportBomProperties();
foreach (Format::cases() as $format) {
yield $format->name => [$format, \in_array($format, $should, true)];
}
}

/** @return Format[] */
abstract protected static function shouldSupportBomProperties(): array;
}

0 comments on commit c4e1711

Please sign in to comment.