File tree Expand file tree Collapse file tree 6 files changed +44
-1
lines changed Expand file tree Collapse file tree 6 files changed +44
-1
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
4
4
5
5
## unreleased
6
6
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
+
7
13
## 1.3.1 - 2021-12-03
8
14
9
15
* Fixed
Original file line number Diff line number Diff line change 5
5
"title" : " CycloneDX Software Bill-of-Material Specification" ,
6
6
"$comment" : " CycloneDX JSON schema is published under the terms of the Apache License 2.0." ,
7
7
"required" : [
8
+ " $schema" ,
8
9
" bomFormat" ,
9
10
" specVersion" ,
10
11
" version"
11
12
],
12
13
"additionalProperties" : false ,
13
14
"properties" : {
15
+ "$schema" : {
16
+ "type" : " string" ,
17
+ "enum" : [
18
+ " http://cyclonedx.org/schema/bom-1.2a.schema.json"
19
+ ]
20
+ },
14
21
"bomFormat" : {
15
22
"$id" : " #/properties/bomFormat" ,
16
23
"type" : " string" ,
Original file line number Diff line number Diff line change 5
5
"title" : " CycloneDX Software Bill-of-Material Specification" ,
6
6
"$comment" : " CycloneDX JSON schema is published under the terms of the Apache License 2.0." ,
7
7
"required" : [
8
+ " $schema" ,
8
9
" bomFormat" ,
9
10
" specVersion" ,
10
11
" version"
11
12
],
12
13
"additionalProperties" : false ,
13
14
"properties" : {
15
+ "$schema" : {
16
+ "type" : " string" ,
17
+ "enum" : [
18
+ " http://cyclonedx.org/schema/bom-1.3.schema.json"
19
+ ]
20
+ },
14
21
"bomFormat" : {
15
22
"$id" : " #/properties/bomFormat" ,
16
23
"type" : " string" ,
Original file line number Diff line number Diff line change 24
24
namespace CycloneDX \Core \Serialize ;
25
25
26
26
use CycloneDX \Core \Models \Bom ;
27
+ use CycloneDX \Core \Spec \Version ;
27
28
use DomainException ;
28
29
29
30
/**
@@ -39,6 +40,25 @@ class JsonSerializer extends BaseSerializer
39
40
| \JSON_UNESCAPED_SLASHES // urls become shorter
40
41
| \JSON_PRETTY_PRINT ;
41
42
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
+
42
62
/**
43
63
* @throws DomainException if something was not supported
44
64
*/
@@ -48,7 +68,7 @@ protected function normalize(Bom $bom): string
48
68
->makeForBom ()
49
69
->normalize ($ bom );
50
70
51
- $ json = json_encode ($ data , self ::NORMALIZE_OPTIONS );
71
+ $ json = json_encode ($ this -> getSchemaBase () + $ data , self ::NORMALIZE_OPTIONS );
52
72
\assert (false !== $ json ); // as option JSON_THROW_ON_ERROR is expected to be set
53
73
54
74
return $ json ;
Original file line number Diff line number Diff line change @@ -60,6 +60,7 @@ public function testSerialize(): void
60
60
self ::assertJsonStringEqualsJsonString (
61
61
<<<'JSON'
62
62
{
63
+ "$schema": "http://cyclonedx.org/schema/bom-1.2a.schema.json",
63
64
"bomFormat": "CycloneDX",
64
65
"specVersion": "1.2",
65
66
"version": 0,
Original file line number Diff line number Diff line change @@ -106,6 +106,7 @@ public function testValidateDataPasses(): void
106
106
$ spec = $ this ->createConfiguredMock (SpecInterface::class, ['getVersion ' => '1.2 ' ]);
107
107
$ validator = new JsonStrictValidator ($ spec );
108
108
$ data = (object ) [
109
+ '$schema ' => 'http://cyclonedx.org/schema/bom-1.2a.schema.json ' ,
109
110
'bomFormat ' => 'CycloneDX ' ,
110
111
'specVersion ' => '1.2 ' ,
111
112
'version ' => 1 ,
@@ -137,6 +138,7 @@ public function testValidateDataFails(): void
137
138
$ spec = $ this ->createConfiguredMock (SpecInterface::class, ['getVersion ' => '1.2 ' ]);
138
139
$ validator = new JsonStrictValidator ($ spec );
139
140
$ data = (object ) [
141
+ '$schema ' => 'http://cyclonedx.org/schema/bom-1.2a.schema.json ' ,
140
142
'bomFormat ' => 'CycloneDX ' ,
141
143
'specVersion ' => '1.2 ' ,
142
144
'version ' => 1 ,
You can’t perform that action at this time.
0 commit comments