Skip to content

Commit

Permalink
feat: add JSON Schema to JSON output (#79)
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilian Combüchen <[email protected]>
  • Loading branch information
mcombuechen committed Mar 2, 2023
1 parent f1af9c9 commit a1db675
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (b *BOM) convert(specVersion SpecVersion) {

b.SpecVersion = specVersion
b.XMLNS = xmlNamespaces[specVersion]
b.JSONSchema = jsonSchemas[specVersion]
}

// componentConverter modifies a Component such that it adheres to a given SpecVersion.
Expand Down
2 changes: 2 additions & 0 deletions cyclonedx.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type BOM struct {
XMLNS string `json:"-" xml:"xmlns,attr"`

// JSON specific fields
JSONSchema string `json:"$schema,omitempty" xml:"-"`
BOMFormat string `json:"bomFormat" xml:"-"`
SpecVersion SpecVersion `json:"specVersion" xml:"-"`

Expand All @@ -77,6 +78,7 @@ type BOM struct {

func NewBOM() *BOM {
return &BOM{
JSONSchema: jsonSchemas[SpecVersion1_4],
XMLNS: xmlNamespaces[SpecVersion1_4],
BOMFormat: BOMFormat,
SpecVersion: SpecVersion1_4,
Expand Down
8 changes: 8 additions & 0 deletions cyclonedx_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,11 @@ func (sv *SpecVersion) UnmarshalJSON(bytes []byte) error {

return nil
}

var jsonSchemas = map[SpecVersion]string{
SpecVersion1_0: "",
SpecVersion1_1: "",
SpecVersion1_2: "http://cyclonedx.org/schema/bom-1.2.schema.json",
SpecVersion1_3: "http://cyclonedx.org/schema/bom-1.3.schema.json",
SpecVersion1_4: "http://cyclonedx.org/schema/bom-1.4.schema.json",
}
3 changes: 3 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestJsonBOMEncoder_SetPretty(t *testing.T) {
require.NoError(t, encoder.Encode(bom))

assert.Equal(t, `{
"$schema": "http://cyclonedx.org/schema/bom-1.4.schema.json",
"bomFormat": "CycloneDX",
"specVersion": "1.4",
"version": 1,
Expand Down Expand Up @@ -83,6 +84,7 @@ func TestJsonBOMEncoder_SetEscapeHTML_true(t *testing.T) {
require.NoError(t, encoder.Encode(bom))

assert.Equal(t, `{
"$schema": "http://cyclonedx.org/schema/bom-1.4.schema.json",
"bomFormat": "CycloneDX",
"specVersion": "1.4",
"version": 1,
Expand Down Expand Up @@ -115,6 +117,7 @@ func TestJsonBOMEncoder_SetEscapeHTML_false(t *testing.T) {
require.NoError(t, encoder.Encode(bom))

assert.Equal(t, `{
"$schema": "http://cyclonedx.org/schema/bom-1.4.schema.json",
"bomFormat": "CycloneDX",
"specVersion": "1.4",
"version": 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://cyclonedx.org/schema/bom-1.2.schema.json",
"bomFormat": "CycloneDX",
"specVersion": "1.2",
"serialNumber": "urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://cyclonedx.org/schema/bom-1.3.schema.json",
"bomFormat": "CycloneDX",
"specVersion": "1.3",
"serialNumber": "urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://cyclonedx.org/schema/bom-1.4.schema.json",
"bomFormat": "CycloneDX",
"specVersion": "1.4",
"serialNumber": "urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79",
Expand Down

0 comments on commit a1db675

Please sign in to comment.