Skip to content

Commit b920af9

Browse files
committed
JSON result: add $schema
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent d9a98ca commit b920af9

File tree

6 files changed

+44
-1
lines changed

6 files changed

+44
-1
lines changed

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
## unreleased
66

7+
* Added
8+
* Resulting JSON files hold the correct `$schema`. ([#43] via [#42])
9+
10+
[#43]: https://github.com/CycloneDX/cyclonedx-php-library/issues/43
11+
[#42]: https://github.com/CycloneDX/cyclonedx-php-library/pull/42
12+
713
## 1.3.1 - 2021-12-03
814

915
* Fixed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@
55
"title": "CycloneDX Software Bill-of-Material Specification",
66
"$comment" : "CycloneDX JSON schema is published under the terms of the Apache License 2.0.",
77
"required": [
8+
"$schema",
89
"bomFormat",
910
"specVersion",
1011
"version"
1112
],
1213
"additionalProperties": false,
1314
"properties": {
15+
"$schema": {
16+
"type": "string",
17+
"enum": [
18+
"http://cyclonedx.org/schema/bom-1.2a.schema.json"
19+
]
20+
},
1421
"bomFormat": {
1522
"$id": "#/properties/bomFormat",
1623
"type": "string",

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@
55
"title": "CycloneDX Software Bill-of-Material Specification",
66
"$comment" : "CycloneDX JSON schema is published under the terms of the Apache License 2.0.",
77
"required": [
8+
"$schema",
89
"bomFormat",
910
"specVersion",
1011
"version"
1112
],
1213
"additionalProperties": false,
1314
"properties": {
15+
"$schema": {
16+
"type": "string",
17+
"enum": [
18+
"http://cyclonedx.org/schema/bom-1.3.schema.json"
19+
]
20+
},
1421
"bomFormat": {
1522
"$id": "#/properties/bomFormat",
1623
"type": "string",

src/Core/Serialize/JsonSerializer.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
namespace CycloneDX\Core\Serialize;
2525

2626
use CycloneDX\Core\Models\Bom;
27+
use CycloneDX\Core\Spec\Version;
2728
use DomainException;
2829

2930
/**
@@ -39,6 +40,25 @@ class JsonSerializer extends BaseSerializer
3940
| \JSON_UNESCAPED_SLASHES // urls become shorter
4041
| \JSON_PRETTY_PRINT;
4142

43+
/**
44+
* @var string[]|null[]
45+
* @psalm-var array<Version::V_*, ?string>
46+
*/
47+
private const SCHEMA = [
48+
Version::V_1_1 => null,
49+
Version::V_1_2 => 'http://cyclonedx.org/schema/bom-1.2a.schema.json',
50+
Version::V_1_3 => 'http://cyclonedx.org/schema/bom-1.3.schema.json',
51+
];
52+
53+
private function getSchemaBase(): array
54+
{
55+
$schema = self::SCHEMA[$this->getSpec()->getVersion()];
56+
57+
return null === $schema
58+
? [] // @codeCoverageIgnore
59+
: ['$schema' => $schema];
60+
}
61+
4262
/**
4363
* @throws DomainException if something was not supported
4464
*/
@@ -48,7 +68,7 @@ protected function normalize(Bom $bom): string
4868
->makeForBom()
4969
->normalize($bom);
5070

51-
$json = json_encode($data, self::NORMALIZE_OPTIONS);
71+
$json = json_encode($this->getSchemaBase() + $data, self::NORMALIZE_OPTIONS);
5272
\assert(false !== $json); // as option JSON_THROW_ON_ERROR is expected to be set
5373

5474
return $json;

tests/Core/Serialize/JsonSerializerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function testSerialize(): void
6060
self::assertJsonStringEqualsJsonString(
6161
<<<'JSON'
6262
{
63+
"$schema": "http://cyclonedx.org/schema/bom-1.2a.schema.json",
6364
"bomFormat": "CycloneDX",
6465
"specVersion": "1.2",
6566
"version": 0,

tests/Core/Validation/Validators/JsonStrictValidatorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public function testValidateDataPasses(): void
106106
$spec = $this->createConfiguredMock(SpecInterface::class, ['getVersion' => '1.2']);
107107
$validator = new JsonStrictValidator($spec);
108108
$data = (object) [
109+
'$schema' => 'http://cyclonedx.org/schema/bom-1.2a.schema.json',
109110
'bomFormat' => 'CycloneDX',
110111
'specVersion' => '1.2',
111112
'version' => 1,
@@ -137,6 +138,7 @@ public function testValidateDataFails(): void
137138
$spec = $this->createConfiguredMock(SpecInterface::class, ['getVersion' => '1.2']);
138139
$validator = new JsonStrictValidator($spec);
139140
$data = (object) [
141+
'$schema' => 'http://cyclonedx.org/schema/bom-1.2a.schema.json',
140142
'bomFormat' => 'CycloneDX',
141143
'specVersion' => '1.2',
142144
'version' => 1,

0 commit comments

Comments
 (0)