Skip to content

Commit 2221b7f

Browse files
committed
encoding/jsonschema: ignore empty fragment when comparing schema versions
It's a very common error for the presence or absence of an empty fragment in schema version URI not to match the presence or absence of the empty fragment in the canonical version URI, particularly as earlier versions included that empty fragment but later versions do not. From a technical point of view, a URI with an empty fragment _should_ be considered identical to a URI with no fragment (as witness Go's `net/url` package which always omits an empty fragment). The specification says [1] of `$schema`: > The value of this keyword MUST be a URI (containing a scheme) and this > URI MUST be normalized. The current schema MUST be valid against the > meta-schema identified by this URI. I think it can reasonably be argued that a URI with a mismatching empty fragment "identifies" the same URL its counterpart. So compare versions while ignoring a trailing empty fragment. [1]: https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-00#rfc.section.8.1.1 Signed-off-by: Roger Peppe <[email protected]> Change-Id: I28c6e767f7be7a2832b42124ffe5f0ec37f83635 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201677 Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 1ea9b25 commit 2221b7f

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

encoding/jsonschema/testdata/txtar/extrafragment_version.txtar

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
"type": "string"
88
}
99
-- out/decode/extract --
10-
ERROR:
11-
invalid $schema URL "https://json-schema.org/draft/2019-09/schema#": $schema URI not recognized:
12-
schema.json:2:3
10+
@jsonschema(schema="https://json-schema.org/draft/2019-09/schema")
11+
@jsonschema(id="http://example.test")
12+
string

encoding/jsonschema/testdata/txtar/missingfragment_version.txtar

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
"type": "string"
88
}
99
-- out/decode/extract --
10-
ERROR:
11-
invalid $schema URL "http://json-schema.org/draft-04/schema": $schema URI not recognized:
12-
schema.json:2:3
10+
@jsonschema(schema="http://json-schema.org/draft-04/schema#")
11+
@jsonschema(id="http://example.test")
12+
string

encoding/jsonschema/version.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package jsonschema
1616

1717
import (
1818
"fmt"
19+
"strings"
1920
)
2021

2122
//go:generate go run golang.org/x/tools/cmd/stringer -type=Version -linecomment
@@ -74,10 +75,15 @@ func vto(v Version) versionSet {
7475

7576
// ParseVersion parses a version URI that defines a JSON Schema version.
7677
func ParseVersion(sv string) (Version, error) {
78+
// Ignore a trailing empty fragment: it's a common error
79+
// to omit or supply such a fragment and it's not entirely
80+
// clear whether comparison should or should not
81+
// be sensitive to its presence or absence.
82+
sv = strings.TrimSuffix(sv, "#")
7783
// If this linear search is ever a performance issue, we could
7884
// build a map, but it doesn't seem worthwhile for now.
7985
for i := Version(1); i < numJSONSchemaVersions; i++ {
80-
if sv == i.String() {
86+
if sv == strings.TrimSuffix(i.String(), "#") {
8187
return i, nil
8288
}
8389
}

0 commit comments

Comments
 (0)