Skip to content

Commit 0f0ca80

Browse files
authored
Merge pull request #86 from CycloneDX/fix-schema-packaging
Fix schema packaging
2 parents 4f12f69 + d87623f commit 0f0ca80

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

cyclonedx/Commands/ValidateCommand.cs

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static async Task<int> Validate(Options options)
7070
if (options.InputFormat.ToString().StartsWith("json"))
7171
{
7272
Console.WriteLine("Validating JSON SBOM...");
73-
validated = ValidateJson(options, inputBom);
73+
validated = await ValidateJson(options, inputBom);
7474
}
7575
else if (options.InputFormat.ToString().StartsWith("xml"))
7676
{
@@ -136,13 +136,13 @@ static bool ValidateXml(Options options, string sbomContents)
136136
XmlReaderSettings settings = new XmlReaderSettings();
137137
var schemaVersion = options.InputFormat.ToString().Substring(5).Replace('_', '.');
138138
Console.WriteLine($"Using schema v{schemaVersion}");
139-
var schemaDirectory = Path.Join(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "Schemas");
140-
settings.Schemas.Add(
141-
$"http://cyclonedx.org/schema/bom/{schemaVersion}",
142-
Path.Join(schemaDirectory, $"bom-{schemaVersion}.xsd"));
143-
settings.Schemas.Add(
144-
"http://cyclonedx.org/schema/spdx",
145-
Path.Join(schemaDirectory, "spdx.xsd"));
139+
140+
var schemaContent = Assembly.GetExecutingAssembly().GetManifestResourceStream($"cyclonedx.Schemas.bom-{schemaVersion}.xsd");
141+
var spdxSchemaContent = Assembly.GetExecutingAssembly().GetManifestResourceStream($"cyclonedx.Schemas.spdx.xsd");
142+
143+
settings.Schemas.Add(XmlSchema.Read(schemaContent, null));
144+
settings.Schemas.Add(XmlSchema.Read(spdxSchemaContent, null));
145+
146146
settings.ValidationType = ValidationType.Schema;
147147

148148
var stream = new MemoryStream();
@@ -175,18 +175,15 @@ static bool ValidateXml(Options options, string sbomContents)
175175
}
176176
}
177177

178-
static bool ValidateJson(Options options, string sbomContents)
178+
static async Task<bool> ValidateJson(Options options, string sbomContents)
179179
{
180180
var schemaVersion = options.InputFormat.ToString().Substring(6).Replace('_', '.');
181181
Console.WriteLine($"Using schema v{schemaVersion}");
182-
var schemaDirectory = Path.Join(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "Schemas");
183-
var schemaFilename = Path.Join(schemaDirectory, $"bom-{schemaVersion}.schema.json");
184-
var spdxFilename = Path.Join(schemaDirectory, $"spdx.schema.json");
185-
var schemaContent = File.ReadAllText(schemaFilename);
186-
var spdxSchemaContent = File.ReadAllText(spdxFilename);
182+
var schemaContent = Assembly.GetExecutingAssembly().GetManifestResourceStream($"cyclonedx.Schemas.bom-{schemaVersion}.schema.json");
183+
var spdxSchemaContent = Assembly.GetExecutingAssembly().GetManifestResourceStream($"cyclonedx.Schemas.spdx.schema.json");
187184

188-
var schema = JsonSchema.FromText(schemaContent);
189-
var spdxSchema = JsonSchema.FromText(spdxSchemaContent);
185+
var schema = await JsonSchema.FromStream(schemaContent);
186+
var spdxSchema = await JsonSchema.FromStream(spdxSchemaContent);
190187

191188
SchemaRegistry.Global.Register(new Uri("file://spdx.schema.json"), spdxSchema);
192189

@@ -204,8 +201,35 @@ static bool ValidateJson(Options options, string sbomContents)
204201
}
205202
else
206203
{
207-
Console.WriteLine(result.Message);
204+
Console.WriteLine($"Validation failed: {result.Message}");
208205
Console.WriteLine(result.SchemaLocation);
206+
207+
if (result.NestedResults != null)
208+
{
209+
var nestedResults = new Queue<ValidationResults>(result.NestedResults);
210+
211+
while (nestedResults.Count > 0)
212+
{
213+
var nestedResult = nestedResults.Dequeue();
214+
215+
if (
216+
!string.IsNullOrEmpty(nestedResult.Message)
217+
&& nestedResult.NestedResults != null
218+
&& nestedResult.NestedResults.Count > 0)
219+
{
220+
Console.WriteLine($"{nestedResult.InstanceLocation}: {nestedResult.Message}");
221+
}
222+
223+
if (nestedResult.NestedResults != null)
224+
{
225+
foreach (var newNestedResult in nestedResult.NestedResults)
226+
{
227+
nestedResults.Enqueue(newNestedResult);
228+
}
229+
}
230+
}
231+
}
232+
209233
return false;
210234
}
211235
}

cyclonedx/cyclonedx.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</ItemGroup>
2828

2929
<ItemGroup>
30-
<None Update="Schemas/*" CopyToOutputDirectory="Always" />
30+
<EmbeddedResource Include="Schemas/*" />
3131
</ItemGroup>
3232

3333
</Project>

0 commit comments

Comments
 (0)