Skip to content

Commit

Permalink
encoding/jsonschema: use latest schema version by default
Browse files Browse the repository at this point in the history
An upcoming change exposes the fact that we use non-draft-07 features in
schemas that don't declare their version. Using the latest schema
version by default seems like it's probably the right thing to do.

Also actually _use_ the `DefaultVersion` constant that we purported to
use already.

We should probably change all our schemas to use an explicit `$schema`
keyword so they don't need changing if we support a new version, but
that won't happen for a good while, so we're good for now.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: Ibb25421fdf3d26194920b221611570630019af45
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202559
Reviewed-by: Daniel Martí <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
rogpeppe committed Oct 15, 2024
1 parent ded6fc3 commit 1a4b0d0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
4 changes: 4 additions & 0 deletions encoding/jsonschema/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func TestDecode(t *testing.T) {
}

if versStr, ok := t.Value("version"); ok {
// TODO most schemas have neither an explicit $schema or a #version
// tag, so when we update the default version, they could break.
// We should probably change most of the tests to use an explicit $schema
// field apart from when we're explicitly testing the default version logic.
if versStr == "openapi" {
// OpenAPI doesn't have a JSON Schema URI so it gets a special case.
cfg.DefaultVersion = jsonschema.VersionOpenAPI
Expand Down
4 changes: 2 additions & 2 deletions encoding/jsonschema/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Extract(data cue.InstanceOrValue, cfg *Config) (f *ast.File, err error) {
cfg.MapURL = DefaultMapURL
}
if cfg.DefaultVersion == VersionUnknown {
cfg.DefaultVersion = VersionDraft7
cfg.DefaultVersion = DefaultVersion
}
if cfg.Strict {
cfg.StrictKeywords = true
Expand All @@ -69,7 +69,7 @@ func Extract(data cue.InstanceOrValue, cfg *Config) (f *ast.File, err error) {

// DefaultVersion defines the default schema version used when
// there is no $schema field and no explicit [Config.DefaultVersion].
const DefaultVersion = VersionDraft7
const DefaultVersion = VersionDraft2020_12

// A Config configures a JSON Schema encoding or decoding.
type Config struct {
Expand Down
6 changes: 4 additions & 2 deletions encoding/jsonschema/testdata/txtar/list.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- schema.yaml --
$schema: http://json-schema.org/draft-07/schema#
type: object

properties:
Expand Down Expand Up @@ -43,6 +44,7 @@ additionalProperties: false
-- out/decode/extract --
import "list"

@jsonschema(schema="http://json-schema.org/draft-07/schema#")
foo?: [...string]
tuple?: [string, int, 2]
has?: list.MatchN(>=1, 3)
Expand All @@ -51,8 +53,8 @@ size?: list.UniqueItems() & list.MaxItems(9) & [_, _, _, ...]
additional?: [int, int, ...string]
-- out/decode/testerr/err-foo-not-string --
foo.0: conflicting values true and string (mismatched types bool and string):
generated.cue:3:8
generated.cue:3:11
generated.cue:4:8
generated.cue:4:11
test/err-foo-not-string.json:2:10
-- test/empty.json --
{}
Expand Down

0 comments on commit 1a4b0d0

Please sign in to comment.