Skip to content

Commit

Permalink
internal/filetypes: default jsonschema strictFeatures to false
Browse files Browse the repository at this point in the history
This means that there's less of a sharp edge when people
upgrade their CUE version.

We can change the default to true at a later date.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: Ide85fda509b40c863bf64093d06398630e7d0d0a
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202635
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
  • Loading branch information
rogpeppe committed Oct 16, 2024
1 parent 16986b2 commit 3c40786
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 33 deletions.
8 changes: 1 addition & 7 deletions cmd/cue/cmd/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,16 +615,10 @@ The following tags are only valid in combination with other tags,
and influence the functioning of the codec. The tag they are
valid with is mentioned in parentheses at the end.
strictFeatures report errors for lossy mappings. Enabled by default (jsonschema)
strictFeatures report errors for lossy mappings. (jsonschema)
strictKeywords report errors for unknown keywords (jsonschema)
strict report errors for either of the above (jsonschema)
The above flags also accept a boolean flag value (e.g. true, 1, false, 0)
to set them explicitly. For example, to ignore unimplemented JSON Schema
features rather than giving an error:
jsonschema+strictFeatures=0
Many commands also support the --out and --outfile/-o flags.
The --out flag specifies the output type using a qualifier
(without the ':'). The -o flag specifies an output file
Expand Down
13 changes: 6 additions & 7 deletions cmd/cue/cmd/testdata/script/def_jsonschema.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ cmp stdout expect-stdout
exec cue def schema.json -p schema -l '#Person:'
cmp stdout expect-stdout

! exec cue def jsonschema: bad.json
cmp stderr expect-stderr-strict-features
exec cue def jsonschema: bad.json

# The default strictness modes should apply even when
# we haven't explicitly specifed the jsonschema file type.
! exec cue def bad.json
! exec cue def jsonschema+strictFeatures: bad.json
cmp stderr expect-stderr-strict-features

exec cue def jsonschema+strictFeatures=0: bad.json
exec cue def jsonschema: bad.json

! exec cue def jsonschema: bad.json --strict
cmp stderr expect-stderr
cmp stderr expect-stderr-strict

! exec cue def jsonschema+strict: bad.json
cmp stderr expect-stderr
cmp stderr expect-stderr-strict

# With only strict features, it should error only for the
# unimplemented feature and not for the unknown keyword.
Expand Down Expand Up @@ -83,7 +82,7 @@ import "strings"
"$dynamicAnchor": "bar",
"foo": true
}
-- expect-stderr --
-- expect-stderr-strict --
keyword "$dynamicAnchor" not yet implemented:
./bad.json:4:3
unknown keyword "foo":
Expand Down
14 changes: 6 additions & 8 deletions cmd/cue/cmd/testdata/script/def_openapi_strict.txtar
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
! exec cue def openapi: openapi.json
cmp stderr expect-stderr-1
exec cue def openapi: openapi.json

! exec cue def openapi.json
cmp stderr expect-stderr-1
# Default is to best-effort mode.
exec cue def openapi.json
cmp stdout expect-out.cue

# We can enable strictFeatures to give an error when
# unimplemented features are used.
! exec cue def openapi+strictFeatures: openapi.json
cmp stderr expect-stderr-1

# We can disable strictFeatures to enable best effort mode.
exec cue def openapi+strictFeatures=0: openapi.json
cmp stdout expect-out.cue

# Bad keywords are still always bad with OpenAPI 3.0.
! exec cue def openapi-badkeyword.json
cmp stderr expect-stderr-2
Expand Down
18 changes: 9 additions & 9 deletions internal/filetypes/filetypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestFromFile(t *testing.T) {
Form: build.Schema,
BoolTags: map[string]bool{
"strict": false,
"strictFeatures": true,
"strictFeatures": false,
"strictKeywords": false,
},
},
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestFromFile(t *testing.T) {
Form: build.Schema,
BoolTags: map[string]bool{
"strict": false,
"strictFeatures": true,
"strictFeatures": false,
"strictKeywords": false,
},
},
Expand Down Expand Up @@ -218,7 +218,7 @@ func TestFromFile(t *testing.T) {
Form: build.Schema,
BoolTags: map[string]bool{
"strict": false,
"strictFeatures": true,
"strictFeatures": false,
"strictKeywords": false,
},
},
Expand Down Expand Up @@ -249,7 +249,7 @@ func TestFromFile(t *testing.T) {
Form: build.Schema,
BoolTags: map[string]bool{
"strict": false,
"strictFeatures": true,
"strictFeatures": false,
"strictKeywords": false,
},
},
Expand Down Expand Up @@ -349,7 +349,7 @@ func TestParseFile(t *testing.T) {
Form: build.Schema,
BoolTags: map[string]bool{
"strict": false,
"strictFeatures": true,
"strictFeatures": false,
"strictKeywords": false,
},
},
Expand Down Expand Up @@ -440,7 +440,7 @@ func TestParseArgs(t *testing.T) {
Interpretation: "jsonschema",
BoolTags: map[string]bool{
"strict": false,
"strictFeatures": true,
"strictFeatures": false,
"strictKeywords": false,
},
},
Expand All @@ -461,7 +461,7 @@ func TestParseArgs(t *testing.T) {
},
},
}, {
in: "jsonschema+strictFeatures=0: bar.schema",
in: "jsonschema+strictFeatures=1: bar.schema",
out: []*build.File{
{
Filename: "bar.schema",
Expand All @@ -470,7 +470,7 @@ func TestParseArgs(t *testing.T) {
Form: build.Schema,
BoolTags: map[string]bool{
"strict": false,
"strictFeatures": false,
"strictFeatures": true,
"strictKeywords": false,
},
},
Expand Down Expand Up @@ -503,7 +503,7 @@ func TestDefaultTagsForInterpretation(t *testing.T) {
tags := DefaultTagsForInterpretation(build.JSONSchema, Input)
qt.Assert(t, qt.DeepEquals(tags, map[string]bool{
"strict": false,
"strictFeatures": true,
"strictFeatures": false,
"strictKeywords": false,
}))
}
5 changes: 3 additions & 2 deletions internal/filetypes/types.cue
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ interpretations: jsonschema: {
boolTags: {
strict: *false | bool
strictKeywords: *strict | bool
strictFeatures: *true | bool
// TODO(v0.12): enable strictFeatures by default
strictFeatures: *strict | bool
}
}

Expand All @@ -307,7 +308,7 @@ interpretations: openapi: {
boolTags: {
strict: *false | bool
strictKeywords: *strict | bool
strictFeatures: *true | bool
strictFeatures: *strict | bool
}
}

Expand Down

0 comments on commit 3c40786

Please sign in to comment.