Skip to content

Commit fdf92d1

Browse files
committed
encoding/jsonschema: do not generate defaults
The `default` keyword in JSON Schema should be an annotation, not a constraint, and in general schemas in CUE should be pure, so having the defaults in there seems wrong, especially as: - it precludes people from adding their own defaults - defaults don't work when they're inside `matchN` calls - defaults will not work as expected inside optional or required fields, which are the only kind of fields that encoding/jsonschema produces. In the future, we might wish to provide template-like values that do include defaults (that's issue #3473), but for now, this keeps things simple and avoids issues with collisions with defaults in user code. So we remove support for `default`, and also remove the unused code for `examples`: we can add it back later in the same or another form as desired. Fixes #3472 Signed-off-by: Roger Peppe <[email protected]> Change-Id: I544734d3361c7b2eff410dae8f27d831ec8c0e97 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1207146 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
1 parent 09c9cca commit fdf92d1

File tree

9 files changed

+12
-59
lines changed

9 files changed

+12
-59
lines changed

encoding/jsonschema/constraints_generic.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,8 @@ func constraintConst(key string, n cue.Value, s *state) {
5050
}
5151

5252
func constraintDefault(key string, n cue.Value, s *state) {
53-
sc := *s
54-
s.default_ = sc.value(n)
55-
// TODO: must validate that the default is subsumed by the normal value,
56-
// as CUE will otherwise broaden the accepted values with the default.
57-
s.examples = append(s.examples, s.default_)
53+
// TODO make the default value available in a separate
54+
// template-like CUE value outside of the usual schema output.
5855
}
5956

6057
func constraintDeprecated(key string, n cue.Value, s *state) {
@@ -90,12 +87,6 @@ func constraintExamples(key string, n cue.Value, s *state) {
9087
if n.Kind() != cue.ListKind {
9188
s.errf(n, `value of "examples" must be an array, found %v`, n.Kind())
9289
}
93-
// TODO: implement examples properly.
94-
// for _, n := range s.listItems("examples", n, true) {
95-
// if ex := s.value(n); !isAny(ex) {
96-
// s.examples = append(s.examples, ex)
97-
// }
98-
// }
9990
}
10091

10192
func constraintNullable(key string, n cue.Value, s *state) {

encoding/jsonschema/decode.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,6 @@ type state struct {
451451
all constraintInfo // values and oneOf etc.
452452
nullable *ast.BasicLit // nullable
453453

454-
default_ ast.Expr
455-
examples []ast.Expr
456454
exclusiveMin bool // For OpenAPI and legacy support.
457455
exclusiveMax bool // For OpenAPI and legacy support.
458456

@@ -680,19 +678,6 @@ func (s *state) finalize() (e ast.Expr) {
680678
a = []ast.Expr{s.nullable, e}
681679
}
682680

683-
outer:
684-
switch {
685-
case s.default_ != nil:
686-
// check conditions where default can be skipped.
687-
switch x := s.default_.(type) {
688-
case *ast.ListLit:
689-
if s.allowedTypes == cue.ListKind && len(x.Elts) == 0 {
690-
break outer
691-
}
692-
}
693-
a = append(a, &ast.UnaryExpr{Op: token.MUL, X: s.default_})
694-
}
695-
696681
e = ast.NewBinExpr(token.OR, a...)
697682

698683
if len(s.definitions) > 0 {

encoding/jsonschema/external_teststats.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Generated by CUE_UPDATE=1 go test. DO NOT EDIT
22
v2:
33
schema extract (pass / total): 1054 / 1363 = 77.3%
4-
tests (pass / total): 3788 / 4803 = 78.9%
5-
tests on extracted schemas (pass / total): 3788 / 3955 = 95.8%
4+
tests (pass / total): 3793 / 4803 = 79.0%
5+
tests on extracted schemas (pass / total): 3793 / 3955 = 95.9%
66

77
v3:
88
schema extract (pass / total): 1054 / 1363 = 77.3%
9-
tests (pass / total): 3778 / 4803 = 78.7%
10-
tests on extracted schemas (pass / total): 3778 / 3955 = 95.5%
9+
tests (pass / total): 3783 / 4803 = 78.8%
10+
tests on extracted schemas (pass / total): 3783 / 3955 = 95.7%
1111

1212
Optional tests
1313

encoding/jsonschema/testdata/external/tests/draft2019-09/default.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@
7878
"data": {
7979
"alpha": 5
8080
},
81-
"valid": false,
82-
"skip": {
83-
"v2": "unexpected success",
84-
"v3": "unexpected success"
85-
}
81+
"valid": false
8682
},
8783
{
8884
"description": "missing properties are not filled in with the default",

encoding/jsonschema/testdata/external/tests/draft2020-12/default.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@
7878
"data": {
7979
"alpha": 5
8080
},
81-
"valid": false,
82-
"skip": {
83-
"v2": "unexpected success",
84-
"v3": "unexpected success"
85-
}
81+
"valid": false
8682
},
8783
{
8884
"description": "missing properties are not filled in with the default",

encoding/jsonschema/testdata/external/tests/draft4/default.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,7 @@
7575
"data": {
7676
"alpha": 5
7777
},
78-
"valid": false,
79-
"skip": {
80-
"v2": "unexpected success",
81-
"v3": "unexpected success"
82-
}
78+
"valid": false
8379
},
8480
{
8581
"description": "missing properties are not filled in with the default",

encoding/jsonschema/testdata/external/tests/draft6/default.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,7 @@
7575
"data": {
7676
"alpha": 5
7777
},
78-
"valid": false,
79-
"skip": {
80-
"v2": "unexpected success",
81-
"v3": "unexpected success"
82-
}
78+
"valid": false
8379
},
8480
{
8581
"description": "missing properties are not filled in with the default",

encoding/jsonschema/testdata/external/tests/draft7/default.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,7 @@
7575
"data": {
7676
"alpha": 5
7777
},
78-
"valid": false,
79-
"skip": {
80-
"v2": "unexpected success",
81-
"v3": "unexpected success"
82-
}
78+
"valid": false
8379
},
8480
{
8581
"description": "missing properties are not filled in with the default",

encoding/jsonschema/testdata/txtar/type.txtar

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ close({
3737
intString?: null | bool | int | string | [...]
3838
object?: {
3939
...
40-
} | *{
41-
foo: "bar"
42-
baz: 1.3
4340
}
44-
numOrList?: matchN(1, [number, [...number]]) | *[1, 2, 3]
41+
numOrList?: matchN(1, [number, [...number]])
4542
})

0 commit comments

Comments
 (0)