Skip to content

Commit b5f47ac

Browse files
authored
Switch json validator to opis/json-schema (#151)
* depend on `opis/json-schema` (see #80) * implement according to new validator * remove some helpers that are not needed anymore * made JSON url normalization more constistent Signed-off-by: Jan Kowalleck <[email protected]>
1 parent c7b5362 commit b5f47ac

22 files changed

+157
-224
lines changed

HISTORY.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,22 @@ API changes
107107
This is considered a non-breaking change, because the behaviour was already documented in the API, even though there was no need for an implementation before.
108108
* `ExternalReferenceNormalizer` classes
109109
* Changed, so that it tries to convert unsupported types to "other", before it throws an `\DomainException` ([#137] via [#147])
110+
* `JSON\Normalizers\ExternalReferenceNormalizer` class
111+
* BREAKING: method `normalize` may throw `\UnexpectedValueException` when the url is invalid to format "ini-reference" (via [#151])
110112
* `\CycloneDX\Core\Spdx` namespace
111113
* BREAKING: renamed the class `License` -> `LicenseValidator` ([#133] via [#143])
112114
* `\CycloneDX\Core\Spec` namespace
113115
* BREAKING: completely reworked everything ([#139] via [#142])
114116
See the code base for references
115117
* `\CycloneDX\Core\Validation` namespace
116-
* `{Json,Xml}Validator` classes
117-
* Added support for CycloneDX v1.4` ([#57] via [#65])
118+
* `XmlValidator` classes
119+
* Added support for CycloneDX v1.4 ([#57] via [#65])
120+
* `JsonValidator` classes
121+
* Added support for CycloneDX v1.4 ([#57] via [#65])
122+
* Utilizes a much more competent validation library than before ([#80] via [#151])
118123
* `JsonStrictValidator` class
119124
* Added support for CycloneDX v1.4 ([#57] via [#65])
125+
* Utilizes a much more competent validation library than before ([#80] via [#151])
120126
* `ValidatorInterface` interface
121127
* BREAKING: renamed interface to `Validator` ([#133] via [#143])
122128

@@ -126,6 +132,7 @@ API changes
126132
[#57]: https://github.com/CycloneDX/cyclonedx-php-library/issues/57
127133
[#65]: https://github.com/CycloneDX/cyclonedx-php-library/pull/65
128134
[#66]: https://github.com/CycloneDX/cyclonedx-php-library/issues/66
135+
[#80]: https://github.com/CycloneDX/cyclonedx-php-library/issues/80
129136
[#114]: https://github.com/CycloneDX/cyclonedx-php-library/issues/114
130137
[#118]: https://github.com/CycloneDX/cyclonedx-php-library/pull/118
131138
[#123]: https://github.com/CycloneDX/cyclonedx-php-library/pull/123
@@ -140,6 +147,7 @@ API changes
140147
[#144]: https://github.com/CycloneDX/cyclonedx-php-library/pull/144
141148
[#146]: https://github.com/CycloneDX/cyclonedx-php-library/pull/146
142149
[#149]: https://github.com/CycloneDX/cyclonedx-php-library/pull/149
150+
[#151]: https://github.com/CycloneDX/cyclonedx-php-library/pull/151
143151

144152
## 1.6.3 - 2022-09-15
145153

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
"ext-dom": "*",
3535
"ext-json": "*",
3636
"ext-libxml": "*",
37-
"package-url/packageurl-php": "^1.0",
38-
"swaggest/json-schema": "^0.12.35"
37+
"opis/json-schema": "^2.0",
38+
"package-url/packageurl-php": "^1.0"
3939
},
4040
"require-dev": {
4141
"ext-simplexml": "*",

res/bom-1.2-strict.SNAPSHOT.schema.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
"additionalProperties": false,
1313
"properties": {
1414
"$schema": {
15-
"type": "string",
16-
"enum": [
17-
"http://cyclonedx.org/schema/bom-1.2a.schema.json"
18-
]
15+
"type": "string"
1916
},
2017
"bomFormat": {
2118
"$id": "#/properties/bomFormat",

res/bom-1.3-strict.SNAPSHOT.schema.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
"additionalProperties": false,
1313
"properties": {
1414
"$schema": {
15-
"type": "string",
16-
"enum": [
17-
"http://cyclonedx.org/schema/bom-1.3.schema.json"
18-
]
15+
"type": "string"
1916
},
2017
"bomFormat": {
2118
"$id": "#/properties/bomFormat",

res/bom-1.4.SNAPSHOT.schema.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
"additionalProperties": false,
1313
"properties": {
1414
"$schema": {
15-
"type": "string",
16-
"enum": [
17-
"http://cyclonedx.org/schema/bom-1.4.schema.json"
18-
]
15+
"type": "string"
1916
},
2017
"bomFormat": {
2118
"type": "string",

src/Core/Serialization/DOM/Normalizers/ExternalReferenceNormalizer.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ class ExternalReferenceNormalizer extends _BaseNormalizer
4747
*/
4848
public function normalize(ExternalReference $externalReference): DOMElement
4949
{
50+
$refURI = $externalReference->getUrl();
51+
$anyURI = $this->encodeAnyUriBE($refURI);
52+
if (null === $anyURI) {
53+
throw new UnexpectedValueException("unable to make 'anyURI' from: $refURI");
54+
}
55+
5056
$factory = $this->getNormalizerFactory();
5157
$spec = $factory->getSpec();
5258

@@ -59,14 +65,6 @@ public function normalize(ExternalReference $externalReference): DOMElement
5965
}
6066
}
6167

62-
$refURI = $externalReference->getUrl();
63-
$anyURI = $this->encodeAnyUriBE($refURI);
64-
if (null === $anyURI) {
65-
// @codeCoverageIgnoreStart
66-
throw new UnexpectedValueException("unable to make anyURI from: $refURI");
67-
// @codeCoverageIgnoreEnd
68-
}
69-
7068
$doc = $factory->getDocument();
7169

7270
return $this->simpleDomAppendChildren(

src/Core/Serialization/JSON/Normalizers/ExternalReferenceNormalizer.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
use CycloneDX\Core\Models\ExternalReference;
3030
use CycloneDX\Core\Serialization\JSON\_BaseNormalizer;
3131
use DomainException;
32+
use Opis\JsonSchema\Formats\IriFormats;
33+
use UnexpectedValueException;
3234

3335
/**
3436
* @author jkowalleck
@@ -38,10 +40,18 @@ class ExternalReferenceNormalizer extends _BaseNormalizer
3840
use NullAssertionTrait;
3941

4042
/**
41-
* @throws DomainException when the type was not supported by the spec
43+
* @throws UnexpectedValueException when the url is invalid to IriReference format
44+
* @throws DomainException when the type was not supported by the spec
45+
*
46+
* @SuppressWarnings(PHPMD.StaticAccess)
4247
*/
4348
public function normalize(ExternalReference $externalReference): array
4449
{
50+
$url = $externalReference->getUrl();
51+
if (false === IriFormats::iriReference($url)) {
52+
throw new UnexpectedValueException("invalid to format 'IriReference': $url");
53+
}
54+
4555
$spec = $this->getNormalizerFactory()->getSpec();
4656
$type = $externalReference->getType();
4757
if (false === $spec->isSupportedExternalReferenceType($type)) {
@@ -55,7 +65,7 @@ public function normalize(ExternalReference $externalReference): array
5565
return array_filter(
5666
[
5767
'type' => $type,
58-
'url' => $externalReference->getUrl(),
68+
'url' => $url,
5969
'comment' => $externalReference->getComment(),
6070
'hashes' => $this->normalizeHashes($externalReference->getHashes()),
6171
],

src/Core/Serialization/JSON/Normalizers/ExternalReferenceRepositoryNormalizer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
use CycloneDX\Core\Collections\ExternalReferenceRepository;
2727
use CycloneDX\Core\Serialization\JSON\_BaseNormalizer;
28+
use DomainException;
29+
use UnexpectedValueException;
2830

2931
/**
3032
* @author jkowalleck
@@ -44,7 +46,7 @@ public function normalize(ExternalReferenceRepository $repo): array
4446
foreach ($repo->getItems() as $externalReference) {
4547
try {
4648
$item = $normalizer->normalize($externalReference);
47-
} catch (\DomainException) {
49+
} catch (DomainException|UnexpectedValueException) {
4850
continue;
4951
}
5052
if (false === empty($item)) {

src/Core/Serialization/JSON/Normalizers/LicenseNormalizer.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use CycloneDX\Core\Models\License\DisjunctiveLicenseWithName;
2929
use CycloneDX\Core\Models\License\LicenseExpression;
3030
use CycloneDX\Core\Serialization\JSON\_BaseNormalizer;
31+
use Opis\JsonSchema\Formats\IriFormats;
3132

3233
/**
3334
* @author jkowalleck
@@ -52,19 +53,23 @@ private function normalizeExpression(LicenseExpression $license): array
5253
}
5354

5455
/**
55-
* @SuppressWarnings(PHPMD.ShortVariable)
56+
* @SuppressWarnings(PHPMD.ShortVariable) $id
57+
* @SuppressWarnings(PHPMD.StaticAccess)
5658
*/
5759
private function normalizeDisjunctive(DisjunctiveLicenseWithId|DisjunctiveLicenseWithName $license): array
5860
{
5961
[$id, $name] = $license instanceof DisjunctiveLicenseWithId
6062
? [$license->getId(), null]
6163
: [null, $license->getName()];
64+
$url = $license->getUrl();
6265

6366
return ['license' => array_filter(
6467
[
6568
'id' => $id,
6669
'name' => $name,
67-
'url' => $license->getUrl(),
70+
'url' => null !== $url && IriFormats::iriReference($url)
71+
? $url
72+
: null,
6873
],
6974
[$this, 'isNotNull']
7075
)];

src/Core/Serialization/JsonSerializer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class JsonSerializer extends BaseSerializer
4949
*/
5050
private const SCHEMA = [
5151
Version::v1dot1 => null, // unsupported version
52-
Version::v1dot2 => 'http://cyclonedx.org/schema/bom-1.2a.schema.json',
53-
Version::v1dot3 => 'http://cyclonedx.org/schema/bom-1.3.schema.json',
52+
Version::v1dot2 => 'http://cyclonedx.org/schema/bom-1.2b.schema.json',
53+
Version::v1dot3 => 'http://cyclonedx.org/schema/bom-1.3a.schema.json',
5454
Version::v1dot4 => 'http://cyclonedx.org/schema/bom-1.4.schema.json',
5555
];
5656

0 commit comments

Comments
 (0)