Skip to content

Commit 86f0f74

Browse files
(SCHEMA) Fix schema bundling for self-reference
Prior to this change, the bundling function for schemas would sometimes include a self-reference in the definitions, which is invalid per the JSON Schema specification. This change ensures self-references are skipped instead of inserted.
1 parent be38da0 commit 86f0f74

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

schemas/build.ps1

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using namespace System.Collections
44
<#
55
.SYNOPSIS
66
Build the DSC schema files from the source YAML files.
7-
7+
88
.DESCRIPTION
99
This build script composes the JSON Schema files from the source YAML files, creating new
1010
files in the specified output directory. It creates a schema registry to analyze the source
@@ -15,11 +15,11 @@ using namespace System.Collections
1515
param(
1616
[string]
1717
$OutputDirectory = "$PSScriptRoot",
18-
18+
1919
[Parameter(ParameterSetName='ByPath')]
2020
[string[]]
2121
$ConfigFilePath,
22-
22+
2323
[string[]]
2424
[ValidateSet('Json', 'JsonVSCode', 'Yaml', 'YamlVSCode')]
2525
$OutputFormat = @(
@@ -40,7 +40,7 @@ begin {
4040

4141
[Specialized.OrderedDictionary]
4242
$Map
43-
43+
4444
[Generic.List[Specialized.OrderedDictionary]]
4545
$List
4646

@@ -102,7 +102,7 @@ begin {
102102
# Need to ensure single-item returns get correctly handled as arays,
103103
# not munged into scalars.
104104
if (
105-
($MungedKeyValue.Count -eq 1) -or
105+
($MungedKeyValue.Count -eq 1) -or
106106
($MungedKeyValue -is [Specialized.OrderedDictionary])
107107
) {
108108
$MungedSchema.Add($_.Key, [object[]]$MungedKeyValue)
@@ -308,18 +308,18 @@ begin {
308308
[Parameter(ParameterSetName='FromPath', Mandatory)]
309309
[string]
310310
$Path,
311-
311+
312312
[Parameter(ParameterSetName='FromSchema', Mandatory)]
313313
[Specialized.OrderedDictionary]
314314
$Schema,
315-
315+
316316
[Parameter(ParameterSetName='FromPreset', Mandatory)]
317317
[ValidateSet('ConfigDocument', 'ResourceManifest')]
318318
[string]
319319
$Preset,
320320

321321
[LocalJsonSchemaRegistry] $SchemaRegistry,
322-
322+
323323
[switch]$ForVSCode,
324324
[switch]$WithoutComments,
325325
[switch]$WithoutExamples
@@ -379,9 +379,14 @@ begin {
379379
continue
380380
}
381381

382+
if ($ID -match "$Reference`$") {
383+
Write-Verbose "$ID`n`tSkipping adding self ($Reference) to `$defs"
384+
continue
385+
}
386+
382387
$ReferenceSegments = $Reference.Trim('/') -split '/'
383388
$Working = $MergedSchema.'$defs'
384-
389+
385390
for ($i = 0; $i -lt $ReferenceSegments.Count; $i++) {
386391
$Segment = $ReferenceSegments[$i]
387392

@@ -390,7 +395,7 @@ begin {
390395
$Working = $Working.$Segment
391396
continue
392397
}
393-
398+
394399
# Add an empty dictionary for non-final segments
395400
if ($i -ne ($ReferenceSegments.Count - 1)) {
396401
$Working.Add($Segment, [Specialized.OrderedDictionary]::new())
@@ -446,6 +451,11 @@ begin {
446451
continue
447452
}
448453

454+
if ($ID -match "$Reference`$") {
455+
Write-Verbose "$ID`n`tSkipping adding self ($Reference) to `$defs"
456+
continue
457+
}
458+
449459
if ($Reference -notin $Schema.'$defs'.Keys) {
450460
Write-Verbose "$ID`n`tAdding reference to `$defs: '$Reference'"
451461
$MergedSchema.'$defs'.Add($ReferenceSchema.'$id', $ReferenceSchema)
@@ -524,7 +534,7 @@ begin {
524534

525535
[string]
526536
$OutputDirectory = $PWD,
527-
537+
528538
[string[]]
529539
[ValidateSet('Json', 'JsonVSCode', 'Yaml', 'YamlVSCode')]
530540
$OutputFormat = @(
@@ -567,7 +577,7 @@ begin {
567577
Path = $ConfigFilePath
568578
SchemaRegistry = $SchemaRegistry
569579
}
570-
580+
571581
if ($MergeForNormal) {
572582
$Bundled = Merge-JsonSchema @SharedMergeParams
573583
| Set-BundledSchemaID -BundledName $Name
@@ -716,7 +726,7 @@ process {
716726
foreach ($VSCodeKeyword in $VSCodeKeywords) {
717727
$SchemaData = Remove-JsonSchemaKey -Schema $SchemaData -KeyName $VSCodeKeyword
718728
}
719-
729+
720730
$SchemaData
721731
| ConvertTo-Json -Depth 99
722732
| ForEach-Object { $_ -replace '\r\n', "`n" }

0 commit comments

Comments
 (0)